Page Speed Optimization Libraries
1.3.25.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/string_util.h" 00030 #include "net/instaweb/util/public/string.h" 00031 00032 namespace net_instaweb { 00033 00034 class CacheInterface; 00035 class Hasher; 00036 class LogRecord; 00037 class MessageHandler; 00038 class RequestHeaders; 00039 class Statistics; 00040 class Timer; 00041 class Variable; 00042 00045 class HTTPCache { 00046 public: 00048 static const char kCacheTimeUs[]; 00049 static const char kCacheHits[]; 00050 static const char kCacheMisses[]; 00051 static const char kCacheFallbacks[]; 00052 static const char kCacheExpirations[]; 00053 static const char kCacheInserts[]; 00054 static const char kCacheDeletes[]; 00055 00057 static const char kEtagPrefix[]; 00058 00060 static const char kEtagFormat[]; 00061 00063 HTTPCache(CacheInterface* cache, Timer* timer, Hasher* hasher, 00064 Statistics* stats); 00065 virtual ~HTTPCache(); 00066 00068 enum FindResult { 00069 kFound, 00070 kNotFound, 00073 kRecentFetchFailed, 00074 kRecentFetchNotCacheable, 00075 }; 00076 00077 virtual void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00078 00084 class Callback { 00085 public: 00086 explicit Callback(const RequestContextPtr& request_ctx) 00087 : response_headers_(NULL), 00088 owns_response_headers_(false), 00089 request_ctx_(request_ctx) { 00090 } 00091 virtual ~Callback(); 00092 virtual void Done(FindResult find_result) = 0; 00102 virtual bool IsCacheValid(const GoogleString& key, 00103 const ResponseHeaders& headers) = 0; 00104 00112 virtual bool IsFresh(const ResponseHeaders& headers) { return true; } 00113 00117 virtual int64 OverrideCacheTtlMs(const GoogleString& key) { return -1; } 00118 00121 HTTPValue* http_value() { return &http_value_; } 00122 ResponseHeaders* response_headers() { 00123 if (response_headers_ == NULL) { 00124 response_headers_ = new ResponseHeaders; 00125 owns_response_headers_ = true; 00126 } 00127 return response_headers_; 00128 } 00129 const ResponseHeaders* response_headers() const { 00130 return const_cast<Callback*>(this)->response_headers(); 00131 } 00132 void set_response_headers(ResponseHeaders* headers) { 00133 DCHECK(!owns_response_headers_); 00134 if (owns_response_headers_) { 00135 delete response_headers_; 00136 } 00137 response_headers_ = headers; 00138 owns_response_headers_ = false; 00139 } 00140 HTTPValue* fallback_http_value() { return &fallback_http_value_; } 00141 00142 LogRecord* log_record(); 00143 const RequestContextPtr& request_context() { return request_ctx_; } 00144 00145 virtual void SetTimingMs(int64 timing_value_ms); 00146 00147 private: 00148 HTTPValue http_value_; 00151 HTTPValue fallback_http_value_; 00152 ResponseHeaders* response_headers_; 00153 bool owns_response_headers_; 00154 RequestContextPtr request_ctx_; 00155 00156 DISALLOW_COPY_AND_ASSIGN(Callback); 00157 }; 00158 00160 virtual void SetIgnoreFailurePuts(); 00161 00164 virtual void Find(const GoogleString& key, MessageHandler* handler, 00165 Callback* callback); 00166 00169 virtual void Put(const GoogleString& key, HTTPValue* value, 00170 MessageHandler* handler); 00171 00176 virtual void Put(const GoogleString& key, ResponseHeaders* headers, 00177 const StringPiece& content, MessageHandler* handler); 00178 00180 virtual void Delete(const GoogleString& key); 00181 00182 virtual void set_force_caching(bool force) { force_caching_ = force; } 00183 bool force_caching() const { return force_caching_; } 00184 virtual void set_disable_html_caching_on_https(bool x) { 00185 disable_html_caching_on_https_ = x; 00186 } 00187 Timer* timer() const { return timer_; } 00188 00197 virtual void RememberNotCacheable(const GoogleString& key, 00198 bool is_200_status_code, 00199 MessageHandler* handler); 00200 00206 virtual void RememberFetchFailed(const GoogleString& key, 00207 MessageHandler* handler); 00208 00212 virtual void RememberFetchDropped(const GoogleString& key, 00213 MessageHandler* handler); 00214 00219 bool IsCacheableContentLength(ResponseHeaders* headers) const; 00224 bool IsCacheableBodySize(int64 body_size) const; 00225 00227 static void InitStats(Statistics* statistics); 00228 00238 bool IsAlreadyExpired(const RequestHeaders* request_headers, 00239 const ResponseHeaders& headers); 00240 00241 Variable* cache_time_us() { return cache_time_us_; } 00242 Variable* cache_hits() { return cache_hits_; } 00243 Variable* cache_misses() { return cache_misses_; } 00244 Variable* cache_fallbacks() { return cache_fallbacks_; } 00245 Variable* cache_expirations() { return cache_expirations_; } 00246 Variable* cache_inserts() { return cache_inserts_; } 00247 Variable* cache_deletes() { return cache_deletes_; } 00248 00249 int64 remember_not_cacheable_ttl_seconds() { 00250 return remember_not_cacheable_ttl_seconds_; 00251 } 00252 00253 virtual void set_remember_not_cacheable_ttl_seconds(int64 value) { 00254 DCHECK_LE(0, value); 00255 if (value >= 0) { 00256 remember_not_cacheable_ttl_seconds_ = value; 00257 } 00258 } 00259 00260 int64 remember_fetch_failed_ttl_seconds() { 00261 return remember_fetch_failed_ttl_seconds_; 00262 } 00263 00264 virtual void set_remember_fetch_failed_ttl_seconds(int64 value) { 00265 DCHECK_LE(0, value); 00266 if (value >= 0) { 00267 remember_fetch_failed_ttl_seconds_ = value; 00268 } 00269 } 00270 00271 int64 remember_fetch_dropped_ttl_seconds() { 00272 return remember_fetch_dropped_ttl_seconds_; 00273 } 00274 00275 virtual void set_remember_fetch_dropped_ttl_seconds(int64 value) { 00276 DCHECK_LE(0, value); 00277 if (value >= 0) { 00278 remember_fetch_dropped_ttl_seconds_ = value; 00279 } 00280 } 00281 00282 int max_cacheable_response_content_length() { 00283 return max_cacheable_response_content_length_; 00284 } 00285 00286 virtual void set_max_cacheable_response_content_length(int64 value); 00287 00288 virtual const char* Name() const { return name_.c_str(); } 00289 00290 protected: 00291 virtual void PutInternal(const GoogleString& key, int64 start_us, 00292 HTTPValue* value); 00293 00294 private: 00295 friend class HTTPCacheCallback; 00296 friend class WriteThroughHTTPCache; 00297 00298 bool IsCurrentlyValid(const RequestHeaders* request_headers, 00299 const ResponseHeaders& headers, int64 now_ms); 00300 00301 bool MayCacheUrl(const GoogleString& url, const ResponseHeaders& headers); 00306 HTTPValue* ApplyHeaderChangesForPut( 00307 const GoogleString& key, int64 start_us, const StringPiece* content, 00308 ResponseHeaders* headers, HTTPValue* value, MessageHandler* handler); 00309 void UpdateStats(FindResult result, bool has_fallback, int64 delta_us); 00310 void RememberFetchFailedorNotCacheableHelper( 00311 const GoogleString& key, MessageHandler* handler, HttpStatus::Code code, 00312 int64 ttl_sec); 00313 00314 CacheInterface* cache_; 00315 Timer* timer_; 00316 Hasher* hasher_; 00317 bool force_caching_; 00319 bool disable_html_caching_on_https_; 00320 Variable* cache_time_us_; 00321 Variable* cache_hits_; 00322 Variable* cache_misses_; 00323 Variable* cache_fallbacks_; 00324 Variable* cache_expirations_; 00325 Variable* cache_inserts_; 00326 Variable* cache_deletes_; 00327 GoogleString name_; 00328 int64 remember_not_cacheable_ttl_seconds_; 00329 int64 remember_fetch_failed_ttl_seconds_; 00330 int64 remember_fetch_dropped_ttl_seconds_; 00331 int64 max_cacheable_response_content_length_; 00332 AtomicBool ignore_failure_puts_; 00333 00334 DISALLOW_COPY_AND_ASSIGN(HTTPCache); 00335 }; 00336 00337 } 00338 00339 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_