Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
http_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
21 
22 #include "base/logging.h"
24 #include "net/instaweb/http/public/meta_data.h"
26 #include "net/instaweb/http/public/response_headers.h"
29 #include "net/instaweb/util/public/cache_interface.h"
32 #include "pagespeed/kernel/base/ref_counted_ptr.h"
33 #include "pagespeed/kernel/http/http_options.h"
34 #include "pagespeed/kernel/http/request_headers.h"
35 
36 namespace net_instaweb {
37 
38 class Hasher;
39 class MessageHandler;
40 class Statistics;
41 class Timer;
42 class Variable;
43 
46 class HTTPCache {
47  public:
49  static const char kCacheTimeUs[];
50  static const char kCacheHits[];
51  static const char kCacheMisses[];
52  static const char kCacheBackendHits[];
53  static const char kCacheBackendMisses[];
54  static const char kCacheFallbacks[];
55  static const char kCacheExpirations[];
56  static const char kCacheInserts[];
57  static const char kCacheDeletes[];
58 
60  static const char kEtagPrefix[];
61 
63  static GoogleString FormatEtag(StringPiece hash);
64 
66  HTTPCache(CacheInterface* cache, Timer* timer, Hasher* hasher,
67  Statistics* stats);
68  virtual ~HTTPCache();
69 
71  enum FindResult {
72  kFound,
73  kNotFound,
77  kRecentFetchNotCacheable,
78  };
79 
80  virtual void set_hasher(Hasher* hasher) { hasher_ = hasher; }
81 
87  class Callback {
88  public:
96  explicit Callback(const RequestContextPtr& request_ctx)
97  : response_headers_(NULL),
98  owns_response_headers_(false),
99  request_ctx_(request_ctx),
100  is_background_(false),
101  update_stats_on_failure_(true) {
102  }
103 
106  Callback(const RequestContextPtr& request_ctx,
107  RequestHeaders::Properties req_properties)
108  : response_headers_(NULL),
109  req_properties_(req_properties),
110  owns_response_headers_(false),
111  request_ctx_(request_ctx),
112  is_background_(false),
113  update_stats_on_failure_(true) {
114  }
115 
116  virtual ~Callback();
117  virtual void Done(FindResult find_result) = 0;
127  virtual bool IsCacheValid(const GoogleString& key,
128  const ResponseHeaders& headers) {
129  return true;
130  }
131 
139  virtual bool IsFresh(const ResponseHeaders& headers) { return true; }
140 
144  virtual int64 OverrideCacheTtlMs(const GoogleString& key) { return -1; }
145 
151  void ReportLatencyMs(int64 latency_ms);
152 
161  virtual ResponseHeaders::VaryOption RespectVaryOnResources() const = 0;
162 
165  HTTPValue* http_value() { return &http_value_; }
166  ResponseHeaders* response_headers() {
167  if (response_headers_ == NULL) {
168  response_headers_ = new ResponseHeaders(request_ctx_->options());
169  owns_response_headers_ = true;
170  }
171  return response_headers_;
172  }
173  const ResponseHeaders* response_headers() const {
174  return const_cast<Callback*>(this)->response_headers();
175  }
176  void set_response_headers(ResponseHeaders* headers) {
177  DCHECK(!owns_response_headers_);
178  if (owns_response_headers_) {
179  delete response_headers_;
180  }
181  response_headers_ = headers;
182  owns_response_headers_ = false;
183  }
184  HTTPValue* fallback_http_value() { return &fallback_http_value_; }
185 
186  const RequestContextPtr& request_context() { return request_ctx_; }
187  void set_is_background(bool is_background) {
188  is_background_ = is_background;
189  }
190 
191  RequestHeaders::Properties req_properties() const {
192  return req_properties_;
193  }
194 
197  bool update_stats_on_failure() const { return update_stats_on_failure_; }
198  void set_update_stats_on_failure(bool x) { update_stats_on_failure_ = x; }
199 
200  protected:
203  virtual void ReportLatencyMsImpl(int64 latency_ms);
204 
205  private:
206  HTTPValue http_value_;
209  HTTPValue fallback_http_value_;
210  ResponseHeaders* response_headers_;
211  RequestHeaders::Properties req_properties_;
212  bool owns_response_headers_;
213  RequestContextPtr request_ctx_;
214  bool is_background_;
215  bool update_stats_on_failure_;
216 
217  DISALLOW_COPY_AND_ASSIGN(Callback);
218  };
219 
221  virtual void SetIgnoreFailurePuts();
222 
225  virtual void Find(const GoogleString& key,
226  const GoogleString& fragment,
227  MessageHandler* handler,
228  Callback* callback);
229 
232  void Put(const GoogleString& key,
233  const GoogleString& fragment,
234  RequestHeaders::Properties req_properties,
235  const HttpOptions& http_options,
236  HTTPValue* value,
237  MessageHandler* handler);
238 
243  void Put(const GoogleString& key,
244  const GoogleString& fragment,
245  RequestHeaders::Properties req_properties,
247  ResponseHeaders::VaryOption respect_vary_on_resources,
248  ResponseHeaders* headers,
249  const StringPiece& content, MessageHandler* handler);
250 
252  virtual void Delete(const GoogleString& key, const GoogleString& fragment);
253 
254  virtual void set_force_caching(bool force) { force_caching_ = force; }
255  bool force_caching() const { return force_caching_; }
256  virtual void set_disable_html_caching_on_https(bool x) {
257  disable_html_caching_on_https_ = x;
258  }
259  Timer* timer() const { return timer_; }
260 
269  virtual void RememberNotCacheable(const GoogleString& key,
270  const GoogleString& fragment,
271  bool is_200_status_code,
272  MessageHandler* handler);
273 
279  virtual void RememberFetchFailed(const GoogleString& key,
280  const GoogleString& fragment,
281  MessageHandler* handler);
282 
286  virtual void RememberFetchDropped(const GoogleString& key,
287  const GoogleString& fragment,
288  MessageHandler* handler);
289 
294  bool IsCacheableContentLength(ResponseHeaders* headers) const;
299  bool IsCacheableBodySize(int64 body_size) const;
300 
302  static void InitStats(Statistics* statistics);
303 
311  bool IsExpired(const ResponseHeaders& headers);
312  bool IsExpired(const ResponseHeaders& headers, int64 now_ms);
313 
315  Variable* cache_time_us() { return cache_time_us_; }
316  Variable* cache_hits() { return cache_hits_; }
317  Variable* cache_misses() { return cache_misses_; }
318  Variable* cache_fallbacks() { return cache_fallbacks_; }
319  Variable* cache_expirations() { return cache_expirations_; }
320  Variable* cache_inserts() { return cache_inserts_; }
321  Variable* cache_deletes() { return cache_deletes_; }
322 
323  int64 remember_not_cacheable_ttl_seconds() {
324  return remember_not_cacheable_ttl_seconds_;
325  }
326 
327  virtual void set_remember_not_cacheable_ttl_seconds(int64 value) {
328  DCHECK_LE(0, value);
329  if (value >= 0) {
330  remember_not_cacheable_ttl_seconds_ = value;
331  }
332  }
333 
334  int64 remember_fetch_failed_ttl_seconds() {
335  return remember_fetch_failed_ttl_seconds_;
336  }
337 
338  virtual void set_remember_fetch_failed_ttl_seconds(int64 value) {
339  DCHECK_LE(0, value);
340  if (value >= 0) {
341  remember_fetch_failed_ttl_seconds_ = value;
342  }
343  }
344 
345  int64 remember_fetch_dropped_ttl_seconds() {
346  return remember_fetch_dropped_ttl_seconds_;
347  }
348 
349  virtual void set_remember_fetch_dropped_ttl_seconds(int64 value) {
350  DCHECK_LE(0, value);
351  if (value >= 0) {
352  remember_fetch_dropped_ttl_seconds_ = value;
353  }
354  }
355 
356  int max_cacheable_response_content_length() {
357  return max_cacheable_response_content_length_;
358  }
359 
360  virtual void set_max_cacheable_response_content_length(int64 value);
361 
362  virtual GoogleString Name() const { return FormatName(cache_->Name()); }
363  static GoogleString FormatName(StringPiece cache);
364 
365  static GoogleString CompositeKey(StringPiece key, StringPiece fragment) {
366  DCHECK(fragment.find("/") == StringPiece::npos);
367 
369  return StrCat(fragment, fragment.empty() ? "" : "/", key);
370  }
371 
372  protected:
373  virtual void PutInternal(const GoogleString& key,
374  const GoogleString& fragment,
375  int64 start_us,
376  HTTPValue* value);
377  virtual void DeleteInternal(const GoogleString& key_fragment);
378 
379  private:
380  friend class HTTPCacheCallback;
381  friend class WriteThroughHTTPCache;
382 
383  bool MayCacheUrl(const GoogleString& url, const ResponseHeaders& headers);
388  HTTPValue* ApplyHeaderChangesForPut(
389  int64 start_us, const StringPiece* content, ResponseHeaders* headers,
390  HTTPValue* value, MessageHandler* handler);
391  void UpdateStats(const GoogleString& key, const GoogleString& fragment,
392  CacheInterface::KeyState backend_state, FindResult result,
393  bool has_fallback, bool is_expired, MessageHandler* handler);
394  void RememberFetchFailedorNotCacheableHelper(
395  const GoogleString& key, const GoogleString& fragment,
396  MessageHandler* handler, HttpStatus::Code code, int64 ttl_sec);
397 
398  CacheInterface* cache_;
399  Timer* timer_;
400  Hasher* hasher_;
401  bool force_caching_;
403  bool disable_html_caching_on_https_;
404 
406  Variable* cache_time_us_;
408  Variable* cache_hits_;
410  Variable* cache_misses_;
413  Variable* cache_backend_hits_;
415  Variable* cache_backend_misses_;
416  Variable* cache_fallbacks_;
417  Variable* cache_expirations_;
418  Variable* cache_inserts_;
419  Variable* cache_deletes_;
420 
421  GoogleString name_;
422  int64 remember_not_cacheable_ttl_seconds_;
423  int64 remember_fetch_failed_ttl_seconds_;
424  int64 remember_fetch_dropped_ttl_seconds_;
425  int64 max_cacheable_response_content_length_;
426  AtomicBool ignore_failure_puts_;
427 
428  DISALLOW_COPY_AND_ASSIGN(HTTPCache);
429 };
430 
431 }
432 
433 #endif
Composes two cache interfaces to form a two level http cache.
Definition: write_through_http_cache.h:40
Callback(const RequestContextPtr &request_ctx, RequestHeaders::Properties req_properties)
Definition: http_cache.h:106
Callback(const RequestContextPtr &request_ctx)
Definition: http_cache.h:96
virtual bool IsFresh(const ResponseHeaders &headers)
Definition: http_cache.h:139
Definition: http_value.h:36
static const char kEtagPrefix[]
The prefix used for Etags.
Definition: http_cache.h:60
FindResult
When a lookup is done in the HTTP Cache, it returns one of these values.
Definition: http_cache.h:71
virtual void RememberNotCacheable(const GoogleString &key, const GoogleString &fragment, bool is_200_status_code, MessageHandler *handler)
Variable * cache_time_us()
Stats for the HTTP cache.
Definition: http_cache.h:315
virtual void RememberFetchDropped(const GoogleString &key, const GoogleString &fragment, MessageHandler *handler)
static const char kCacheTimeUs[]
Names of statistics variables: exported for tests.
Definition: http_cache.h:49
bool IsCacheableBodySize(int64 body_size) const
bool IsExpired(const ResponseHeaders &headers)
virtual void SetIgnoreFailurePuts()
Makes the cache ignore put requests that do not record successes.
bool update_stats_on_failure() const
Definition: http_cache.h:197
virtual bool IsCacheValid(const GoogleString &key, const ResponseHeaders &headers)
Definition: http_cache.h:127
static void InitStats(Statistics *statistics)
Initialize statistics variables for the cache.
bool IsCacheableContentLength(ResponseHeaders *headers) const
void ReportLatencyMs(int64 latency_ms)
void Put(const GoogleString &key, const GoogleString &fragment, RequestHeaders::Properties req_properties, const HttpOptions &http_options, HTTPValue *value, MessageHandler *handler)
HTTPValue * http_value()
Definition: http_cache.h:165
virtual void Find(const GoogleString &key, const GoogleString &fragment, MessageHandler *handler, Callback *callback)
HTTPCache(CacheInterface *cache, Timer *timer, Hasher *hasher, Statistics *stats)
Does not take ownership of any inputs.
Definition: http_cache.h:87
virtual void RememberFetchFailed(const GoogleString &key, const GoogleString &fragment, MessageHandler *handler)
virtual int64 OverrideCacheTtlMs(const GoogleString &key)
Definition: http_cache.h:144
virtual void Delete(const GoogleString &key, const GoogleString &fragment)
Deletes an element in the cache.
virtual void ReportLatencyMsImpl(int64 latency_ms)
virtual ResponseHeaders::VaryOption RespectVaryOnResources() const =0
static GoogleString CompositeKey(StringPiece key, StringPiece fragment)
Definition: http_cache.h:365
Definition: http_cache.h:46
static GoogleString FormatEtag(StringPiece hash)
Function to format etags.