19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
22 #include "base/logging.h"
25 #include "net/instaweb/http/public/request_context.h"
32 #include "pagespeed/kernel/cache/cache_interface.h"
38 namespace net_instaweb {
71 static const char kCacheHits[];
72 static const char kCacheMisses[];
73 static const char kCacheBackendHits[];
74 static const char kCacheBackendMisses[];
75 static const char kCacheFallbacks[];
76 static const char kCacheExpirations[];
77 static const char kCacheInserts[];
78 static const char kCacheDeletes[];
91 enum FindResultClassification {
102 FindResult(FindResultClassification in_status,
106 bool operator==(
const FindResult& other)
const {
107 return status == other.status &&
111 bool operator!=(
const FindResult& other)
const {
112 return !(*
this == other);
115 FindResultClassification status;
125 void set_hasher(
Hasher* hasher) { hasher_ = hasher; }
142 : response_headers_(NULL),
143 owns_response_headers_(false),
144 request_ctx_(request_ctx),
146 is_background_(false) {
153 : response_headers_(NULL),
154 req_properties_(req_properties),
155 owns_response_headers_(false),
156 request_ctx_(request_ctx),
158 is_background_(false) {
162 virtual void Done(
FindResult find_result) = 0;
212 if (response_headers_ == NULL) {
214 owns_response_headers_ =
true;
216 return response_headers_;
218 const ResponseHeaders* response_headers()
const {
219 return const_cast<Callback*
>(
this)->response_headers();
221 void set_response_headers(ResponseHeaders* headers) {
222 DCHECK(!owns_response_headers_);
223 if (owns_response_headers_) {
224 delete response_headers_;
226 response_headers_ = headers;
227 owns_response_headers_ =
false;
229 HTTPValue* fallback_http_value() {
return &fallback_http_value_; }
231 const RequestContextPtr& request_context() {
return request_ctx_; }
232 void set_is_background(
bool is_background) {
233 is_background_ = is_background;
236 RequestHeaders::Properties req_properties()
const {
237 return req_properties_;
241 HTTPValue http_value_;
244 HTTPValue fallback_http_value_;
245 ResponseHeaders* response_headers_;
246 RequestHeaders::Properties req_properties_;
247 bool owns_response_headers_;
248 RequestContextPtr request_ctx_;
262 MessageHandler* handler,
269 RequestHeaders::Properties req_properties,
270 const HttpOptions& http_options,
272 MessageHandler* handler);
280 RequestHeaders::Properties req_properties,
282 ResponseHeaders::VaryOption respect_vary_on_resources,
283 ResponseHeaders* headers,
284 const StringPiece& content, MessageHandler* handler);
289 void set_force_caching(
bool force) { force_caching_ = force; }
290 bool force_caching()
const {
return force_caching_; }
291 void set_disable_html_caching_on_https(
bool x) {
292 disable_html_caching_on_https_ = x;
294 Timer* timer()
const {
return timer_; }
295 CacheInterface* cache() {
return cache_; }
304 MessageHandler* handler);
318 static void InitStats(Statistics* statistics);
327 bool IsExpired(
const ResponseHeaders& headers);
328 bool IsExpired(
const ResponseHeaders& headers, int64 now_ms);
332 Variable* cache_hits() {
return cache_hits_; }
333 Variable* cache_misses() {
return cache_misses_; }
334 Variable* cache_fallbacks() {
return cache_fallbacks_; }
335 Variable* cache_expirations() {
return cache_expirations_; }
336 Variable* cache_inserts() {
return cache_inserts_; }
337 Variable* cache_deletes() {
return cache_deletes_; }
347 int max_cacheable_response_content_length() {
348 return max_cacheable_response_content_length_;
351 void set_max_cacheable_response_content_length(int64 value);
356 int cache_levels()
const {
return cache_levels_; }
361 if (level >= -1 && level <= 9) {
362 compression_level_ = level;
364 LOG(INFO) <<
"Invalid compression level specified, defaulting to -1";
365 compression_level_ = -1;
368 int compression_level()
const {
return compression_level_; }
374 DCHECK(fragment.find(
"/") == StringPiece::npos);
378 return StrCat(version_prefix_, fragment, fragment.empty() ?
"" :
"/", key);
382 friend class HTTPCacheCallback;
383 FRIEND_TEST(HTTPCacheTest, UpdateVersion);
387 void PutInternal(
bool preserve_response_headers,
397 void SetVersion(
int version_number);
398 void set_version_prefix(StringPiece version_prefix) {
399 version_prefix.CopyToString(&version_prefix_);
402 bool MayCacheUrl(
const GoogleString& url,
const ResponseHeaders& headers);
407 HTTPValue* ApplyHeaderChangesForPut(
408 int64 start_us,
const StringPiece* content, ResponseHeaders* headers,
409 HTTPValue* value, MessageHandler* handler);
412 bool has_fallback,
bool is_expired, MessageHandler* handler);
413 void RememberFetchFailedOrNotCacheableHelper(
415 MessageHandler* handler, HttpStatus::Code code, int64 ttl_sec);
417 CacheInterface* cache_;
422 bool disable_html_caching_on_https_;
425 int compression_level_;
428 Variable* cache_time_us_;
430 Variable* cache_hits_;
432 Variable* cache_misses_;
435 Variable* cache_backend_hits_;
437 Variable* cache_backend_misses_;
438 Variable* cache_fallbacks_;
439 Variable* cache_expirations_;
440 Variable* cache_inserts_;
441 Variable* cache_deletes_;
444 HttpCacheFailurePolicy remember_failure_policy_;
445 int64 max_cacheable_response_content_length_;
446 AtomicBool ignore_failure_puts_;
Callback(const RequestContextPtr &request_ctx, RequestHeaders::Properties req_properties)
Definition: http_cache.h:151
Callback(const RequestContextPtr &request_ctx)
Definition: http_cache.h:141
virtual bool IsFresh(const ResponseHeaders &headers)
Definition: http_cache.h:184
Abstract interface for a cache.
Definition: cache_interface.h:32
When a lookup is done in the HTTP Cache, it returns one of these values.
Definition: http_cache.h:98
Definition: http_value.h:38
static const char kEtagPrefix[]
The prefix used for Etags.
Definition: http_cache.h:81
Definition: statistics.h:43
void SetCompressionLevel(int level)
Definition: http_cache.h:360
void RememberFailure(const GoogleString &key, const GoogleString &fragment, FetchResponseStatus the_failure, MessageHandler *handler)
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Variable * cache_time_us()
Stats for the HTTP cache.
Definition: http_cache.h:331
GoogleString CompositeKey(StringPiece key, StringPiece fragment) const
Definition: http_cache.h:373
FetchResponseStatus failure_details
Definition: http_cache.h:122
void SetIgnoreFailurePuts()
Makes the cache ignore put requests that do not record successes.
void Find(const GoogleString &key, const GoogleString &fragment, MessageHandler *handler, Callback *callback)
static const char kCacheTimeUs[]
Names of statistics variables: exported for tests.
Definition: http_cache.h:70
bool IsCacheableBodySize(int64 body_size) const
KeyState
Definition: cache_interface.h:34
bool IsExpired(const ResponseHeaders &headers)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
int ttl_sec_for_status[8]
Definition: http_cache_failure.h:51
void Delete(const GoogleString &key, const GoogleString &fragment)
Deletes an element in the cache.
virtual bool IsCacheValid(const GoogleString &key, const ResponseHeaders &headers)
Definition: http_cache.h:172
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:210
HTTPCache(CacheInterface *cache, Timer *timer, Hasher *hasher, Statistics *stats)
Does not take ownership of any inputs.
Definition: message_handler.h:39
Definition: http_cache.h:132
FetchResponseStatus
Definition: http_cache_failure.h:33
virtual GoogleString Name() const =0
virtual int64 OverrideCacheTtlMs(const GoogleString &key)
Definition: http_cache.h:189
void set_cache_levels(int levels)
Definition: http_cache.h:355
virtual ResponseHeaders::VaryOption RespectVaryOnResources() const =0
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
Definition: http_cache.h:67
static GoogleString FormatEtag(StringPiece hash)
Function to format etags.