Page Speed Optimization Libraries
1.7.30.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_ 00020 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_ 00021 00022 #include "base/logging.h" 00023 #include "net/instaweb/http/public/http_value.h" 00024 #include "net/instaweb/http/public/meta_data.h" 00025 #include "net/instaweb/http/public/request_context.h" 00026 #include "net/instaweb/http/public/response_headers.h" 00027 #include "net/instaweb/util/public/atomic_bool.h" 00028 #include "net/instaweb/util/public/basictypes.h" 00029 #include "net/instaweb/util/public/cache_interface.h" 00030 #include "net/instaweb/util/public/string_util.h" 00031 #include "net/instaweb/util/public/string.h" 00032 00033 namespace net_instaweb { 00034 00035 class Hasher; 00036 class MessageHandler; 00037 class RequestHeaders; 00038 class Statistics; 00039 class Timer; 00040 class Variable; 00041 00044 class HTTPCache { 00045 public: 00047 static const char kCacheTimeUs[]; 00048 static const char kCacheHits[]; 00049 static const char kCacheMisses[]; 00050 static const char kCacheBackendHits[]; 00051 static const char kCacheBackendMisses[]; 00052 static const char kCacheFallbacks[]; 00053 static const char kCacheExpirations[]; 00054 static const char kCacheInserts[]; 00055 static const char kCacheDeletes[]; 00056 00058 static const char kEtagPrefix[]; 00059 00061 static GoogleString FormatEtag(StringPiece hash); 00062 00064 HTTPCache(CacheInterface* cache, Timer* timer, Hasher* hasher, 00065 Statistics* stats); 00066 virtual ~HTTPCache(); 00067 00069 enum FindResult { 00070 kFound, 00071 kNotFound, 00074 kRecentFetchFailed, 00075 kRecentFetchNotCacheable, 00076 }; 00077 00078 virtual void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00079 00085 class Callback { 00086 public: 00087 explicit Callback(const RequestContextPtr& request_ctx) 00088 : response_headers_(NULL), 00089 owns_response_headers_(false), 00090 request_ctx_(request_ctx), 00091 is_background_(false) { 00092 } 00093 virtual ~Callback(); 00094 virtual void Done(FindResult find_result) = 0; 00104 virtual bool IsCacheValid(const GoogleString& key, 00105 const ResponseHeaders& headers) = 0; 00106 00114 virtual bool IsFresh(const ResponseHeaders& headers) { return true; } 00115 00119 virtual int64 OverrideCacheTtlMs(const GoogleString& key) { return -1; } 00120 00126 void ReportLatencyMs(int64 latency_ms); 00127 00130 HTTPValue* http_value() { return &http_value_; } 00131 ResponseHeaders* response_headers() { 00132 if (response_headers_ == NULL) { 00133 response_headers_ = new ResponseHeaders; 00134 owns_response_headers_ = true; 00135 } 00136 return response_headers_; 00137 } 00138 const ResponseHeaders* response_headers() const { 00139 return const_cast<Callback*>(this)->response_headers(); 00140 } 00141 void set_response_headers(ResponseHeaders* headers) { 00142 DCHECK(!owns_response_headers_); 00143 if (owns_response_headers_) { 00144 delete response_headers_; 00145 } 00146 response_headers_ = headers; 00147 owns_response_headers_ = false; 00148 } 00149 HTTPValue* fallback_http_value() { return &fallback_http_value_; } 00150 00151 const RequestContextPtr& request_context() { return request_ctx_; } 00152 void set_is_background(bool is_background) { 00153 is_background_ = is_background; 00154 } 00155 00156 protected: 00159 virtual void ReportLatencyMsImpl(int64 latency_ms); 00160 00161 private: 00162 HTTPValue http_value_; 00165 HTTPValue fallback_http_value_; 00166 ResponseHeaders* response_headers_; 00167 bool owns_response_headers_; 00168 RequestContextPtr request_ctx_; 00169 bool is_background_; 00170 00171 DISALLOW_COPY_AND_ASSIGN(Callback); 00172 }; 00173 00175 virtual void SetIgnoreFailurePuts(); 00176 00179 virtual void Find(const GoogleString& key, MessageHandler* handler, 00180 Callback* callback); 00181 00184 virtual void Put(const GoogleString& key, HTTPValue* value, 00185 MessageHandler* handler); 00186 00191 virtual void Put(const GoogleString& key, ResponseHeaders* headers, 00192 const StringPiece& content, MessageHandler* handler); 00193 00195 virtual void Delete(const GoogleString& key); 00196 00197 virtual void set_force_caching(bool force) { force_caching_ = force; } 00198 bool force_caching() const { return force_caching_; } 00199 virtual void set_disable_html_caching_on_https(bool x) { 00200 disable_html_caching_on_https_ = x; 00201 } 00202 Timer* timer() const { return timer_; } 00203 00212 virtual void RememberNotCacheable(const GoogleString& key, 00213 bool is_200_status_code, 00214 MessageHandler* handler); 00215 00221 virtual void RememberFetchFailed(const GoogleString& key, 00222 MessageHandler* handler); 00223 00227 virtual void RememberFetchDropped(const GoogleString& key, 00228 MessageHandler* handler); 00229 00234 bool IsCacheableContentLength(ResponseHeaders* headers) const; 00239 bool IsCacheableBodySize(int64 body_size) const; 00240 00242 static void InitStats(Statistics* statistics); 00243 00253 bool IsAlreadyExpired(const RequestHeaders* request_headers, 00254 const ResponseHeaders& headers); 00255 00256 Variable* cache_time_us() { return cache_time_us_; } 00257 Variable* cache_hits() { return cache_hits_; } 00258 Variable* cache_misses() { return cache_misses_; } 00259 Variable* cache_fallbacks() { return cache_fallbacks_; } 00260 Variable* cache_expirations() { return cache_expirations_; } 00261 Variable* cache_inserts() { return cache_inserts_; } 00262 Variable* cache_deletes() { return cache_deletes_; } 00263 00264 int64 remember_not_cacheable_ttl_seconds() { 00265 return remember_not_cacheable_ttl_seconds_; 00266 } 00267 00268 virtual void set_remember_not_cacheable_ttl_seconds(int64 value) { 00269 DCHECK_LE(0, value); 00270 if (value >= 0) { 00271 remember_not_cacheable_ttl_seconds_ = value; 00272 } 00273 } 00274 00275 int64 remember_fetch_failed_ttl_seconds() { 00276 return remember_fetch_failed_ttl_seconds_; 00277 } 00278 00279 virtual void set_remember_fetch_failed_ttl_seconds(int64 value) { 00280 DCHECK_LE(0, value); 00281 if (value >= 0) { 00282 remember_fetch_failed_ttl_seconds_ = value; 00283 } 00284 } 00285 00286 int64 remember_fetch_dropped_ttl_seconds() { 00287 return remember_fetch_dropped_ttl_seconds_; 00288 } 00289 00290 virtual void set_remember_fetch_dropped_ttl_seconds(int64 value) { 00291 DCHECK_LE(0, value); 00292 if (value >= 0) { 00293 remember_fetch_dropped_ttl_seconds_ = value; 00294 } 00295 } 00296 00297 int max_cacheable_response_content_length() { 00298 return max_cacheable_response_content_length_; 00299 } 00300 00301 virtual void set_max_cacheable_response_content_length(int64 value); 00302 00303 virtual GoogleString Name() const { return FormatName(cache_->Name()); } 00304 static GoogleString FormatName(StringPiece cache); 00305 00306 protected: 00307 virtual void PutInternal(const GoogleString& key, int64 start_us, 00308 HTTPValue* value); 00309 00310 private: 00311 friend class HTTPCacheCallback; 00312 friend class WriteThroughHTTPCache; 00313 00314 bool IsCurrentlyValid(const RequestHeaders* request_headers, 00315 const ResponseHeaders& headers, int64 now_ms); 00316 00317 bool MayCacheUrl(const GoogleString& url, const ResponseHeaders& headers); 00322 HTTPValue* ApplyHeaderChangesForPut( 00323 const GoogleString& key, int64 start_us, const StringPiece* content, 00324 ResponseHeaders* headers, HTTPValue* value, MessageHandler* handler); 00325 void UpdateStats(const GoogleString& key, 00326 CacheInterface::KeyState backend_state, FindResult result, 00327 bool has_fallback, bool is_expired, int64 delta_us, 00328 MessageHandler* handler); 00329 void RememberFetchFailedorNotCacheableHelper( 00330 const GoogleString& key, MessageHandler* handler, HttpStatus::Code code, 00331 int64 ttl_sec); 00332 00333 CacheInterface* cache_; 00334 Timer* timer_; 00335 Hasher* hasher_; 00336 bool force_caching_; 00338 bool disable_html_caching_on_https_; 00339 00341 Variable* cache_time_us_; 00343 Variable* cache_hits_; 00345 Variable* cache_misses_; 00348 Variable* cache_backend_hits_; 00350 Variable* cache_backend_misses_; 00351 Variable* cache_fallbacks_; 00352 Variable* cache_expirations_; 00353 Variable* cache_inserts_; 00354 Variable* cache_deletes_; 00355 00356 GoogleString name_; 00357 int64 remember_not_cacheable_ttl_seconds_; 00358 int64 remember_fetch_failed_ttl_seconds_; 00359 int64 remember_fetch_dropped_ttl_seconds_; 00360 int64 max_cacheable_response_content_length_; 00361 AtomicBool ignore_failure_puts_; 00362 00363 DISALLOW_COPY_AND_ASSIGN(HTTPCache); 00364 }; 00365 00366 } 00367 00368 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_