19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
22 #include "base/logging.h"
24 #include "net/instaweb/http/public/request_context.h"
25 #include "pagespeed/kernel/base/atomic_bool.h"
26 #include "pagespeed/kernel/base/basictypes.h"
27 #include "pagespeed/kernel/base/ref_counted_ptr.h"
28 #include "pagespeed/kernel/base/string.h"
29 #include "pagespeed/kernel/base/string_util.h"
30 #include "pagespeed/kernel/cache/cache_interface.h"
31 #include "pagespeed/kernel/http/http_names.h"
32 #include "pagespeed/kernel/http/http_options.h"
33 #include "pagespeed/kernel/http/request_headers.h"
34 #include "pagespeed/kernel/http/response_headers.h"
36 namespace net_instaweb {
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[];
63 static GoogleString
FormatEtag(StringPiece hash);
66 HTTPCache(CacheInterface* cache, Timer* timer, Hasher* hasher,
77 kRecentFetchNotCacheable,
80 virtual void set_hasher(Hasher* hasher) { hasher_ = hasher; }
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) {
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) {
117 virtual void Done(
FindResult find_result) = 0;
128 const ResponseHeaders& headers) {
139 virtual bool IsFresh(
const ResponseHeaders& headers) {
return true; }
166 ResponseHeaders* response_headers() {
167 if (response_headers_ == NULL) {
168 response_headers_ =
new ResponseHeaders(request_ctx_->options());
169 owns_response_headers_ =
true;
171 return response_headers_;
173 const ResponseHeaders* response_headers()
const {
174 return const_cast<Callback*
>(
this)->response_headers();
176 void set_response_headers(ResponseHeaders* headers) {
177 DCHECK(!owns_response_headers_);
178 if (owns_response_headers_) {
179 delete response_headers_;
181 response_headers_ = headers;
182 owns_response_headers_ =
false;
184 HTTPValue* fallback_http_value() {
return &fallback_http_value_; }
186 const RequestContextPtr& request_context() {
return request_ctx_; }
187 void set_is_background(
bool is_background) {
188 is_background_ = is_background;
191 RequestHeaders::Properties req_properties()
const {
192 return req_properties_;
198 void set_update_stats_on_failure(
bool x) { update_stats_on_failure_ = x; }
210 ResponseHeaders* response_headers_;
211 RequestHeaders::Properties req_properties_;
212 bool owns_response_headers_;
213 RequestContextPtr request_ctx_;
215 bool update_stats_on_failure_;
225 virtual void Find(
const GoogleString& key,
226 const GoogleString& fragment,
227 MessageHandler* handler,
232 void Put(
const GoogleString& key,
233 const GoogleString& fragment,
234 RequestHeaders::Properties req_properties,
235 const HttpOptions& http_options,
237 MessageHandler* handler);
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);
252 virtual void Delete(
const GoogleString& key,
const GoogleString& fragment);
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;
259 Timer* timer()
const {
return timer_; }
270 const GoogleString& fragment,
271 bool is_200_status_code,
272 MessageHandler* handler);
280 const GoogleString& fragment,
281 MessageHandler* handler);
287 const GoogleString& fragment,
288 MessageHandler* handler);
302 static void InitStats(Statistics* statistics);
311 bool IsExpired(
const ResponseHeaders& headers);
312 bool IsExpired(
const ResponseHeaders& headers, int64 now_ms);
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_; }
323 int64 remember_not_cacheable_ttl_seconds() {
324 return remember_not_cacheable_ttl_seconds_;
327 virtual void set_remember_not_cacheable_ttl_seconds(int64 value) {
330 remember_not_cacheable_ttl_seconds_ = value;
334 int64 remember_fetch_failed_ttl_seconds() {
335 return remember_fetch_failed_ttl_seconds_;
338 virtual void set_remember_fetch_failed_ttl_seconds(int64 value) {
341 remember_fetch_failed_ttl_seconds_ = value;
345 int64 remember_fetch_dropped_ttl_seconds() {
346 return remember_fetch_dropped_ttl_seconds_;
349 virtual void set_remember_fetch_dropped_ttl_seconds(int64 value) {
352 remember_fetch_dropped_ttl_seconds_ = value;
356 int max_cacheable_response_content_length() {
357 return max_cacheable_response_content_length_;
360 virtual void set_max_cacheable_response_content_length(int64 value);
362 virtual GoogleString Name()
const {
return FormatName(cache_->Name()); }
363 static GoogleString FormatName(StringPiece cache);
365 static GoogleString
CompositeKey(StringPiece key, StringPiece fragment) {
366 DCHECK(fragment.find(
"/") == StringPiece::npos);
369 return StrCat(fragment, fragment.empty() ?
"" :
"/", key);
373 virtual void PutInternal(
const GoogleString& key,
374 const GoogleString& fragment,
377 virtual void DeleteInternal(
const GoogleString& key_fragment);
380 friend class HTTPCacheCallback;
383 bool MayCacheUrl(
const GoogleString& url,
const ResponseHeaders& headers);
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);
398 CacheInterface* cache_;
403 bool disable_html_caching_on_https_;
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_;
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_;
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)
Definition: http_cache.h:76
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.