Page Speed Optimization Libraries  1.4.26.1
net/instaweb/http/public/http_cache.h
Go to the documentation of this file.
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           log_timing_(true) {
00091     }
00092     virtual ~Callback();
00093     virtual void Done(FindResult find_result) = 0;
00103     virtual bool IsCacheValid(const GoogleString& key,
00104                               const ResponseHeaders& headers) = 0;
00105 
00113     virtual bool IsFresh(const ResponseHeaders& headers) { return true; }
00114 
00118     virtual int64 OverrideCacheTtlMs(const GoogleString& key) { return -1; }
00119 
00122     HTTPValue* http_value() { return &http_value_; }
00123     ResponseHeaders* response_headers() {
00124       if (response_headers_ == NULL) {
00125         response_headers_ = new ResponseHeaders;
00126         owns_response_headers_ = true;
00127       }
00128       return response_headers_;
00129     }
00130     const ResponseHeaders* response_headers() const {
00131       return const_cast<Callback*>(this)->response_headers();
00132     }
00133     void set_response_headers(ResponseHeaders* headers) {
00134       DCHECK(!owns_response_headers_);
00135       if (owns_response_headers_) {
00136         delete response_headers_;
00137       }
00138       response_headers_ = headers;
00139       owns_response_headers_ = false;
00140     }
00141     HTTPValue* fallback_http_value() { return &fallback_http_value_; }
00142 
00143     LogRecord* log_record();
00144     const RequestContextPtr& request_context() { return request_ctx_; }
00145     void set_log_timing(bool t) { log_timing_ = t; }
00146     bool log_timing() const { return log_timing_; }
00147 
00148     virtual void SetTimingMs(int64 timing_value_ms);
00149 
00150    private:
00151     HTTPValue http_value_;
00154     HTTPValue fallback_http_value_;
00155     ResponseHeaders* response_headers_;
00156     bool owns_response_headers_;
00157     RequestContextPtr request_ctx_;
00158     bool log_timing_;
00159 
00160     DISALLOW_COPY_AND_ASSIGN(Callback);
00161   };
00162 
00164   virtual void SetIgnoreFailurePuts();
00165 
00168   virtual void Find(const GoogleString& key, MessageHandler* handler,
00169                     Callback* callback);
00170 
00173   virtual void Put(const GoogleString& key, HTTPValue* value,
00174                    MessageHandler* handler);
00175 
00180   virtual void Put(const GoogleString& key, ResponseHeaders* headers,
00181                    const StringPiece& content, MessageHandler* handler);
00182 
00184   virtual void Delete(const GoogleString& key);
00185 
00186   virtual void set_force_caching(bool force) { force_caching_ = force; }
00187   bool force_caching() const { return force_caching_; }
00188   virtual void set_disable_html_caching_on_https(bool x) {
00189     disable_html_caching_on_https_ = x;
00190   }
00191   Timer* timer() const { return timer_; }
00192 
00201   virtual void RememberNotCacheable(const GoogleString& key,
00202                                     bool is_200_status_code,
00203                                     MessageHandler* handler);
00204 
00210   virtual void RememberFetchFailed(const GoogleString& key,
00211                                    MessageHandler* handler);
00212 
00216   virtual void RememberFetchDropped(const GoogleString& key,
00217                                     MessageHandler* handler);
00218 
00223   bool IsCacheableContentLength(ResponseHeaders* headers) const;
00228   bool IsCacheableBodySize(int64 body_size) const;
00229 
00231   static void InitStats(Statistics* statistics);
00232 
00242   bool IsAlreadyExpired(const RequestHeaders* request_headers,
00243                         const ResponseHeaders& headers);
00244 
00245   Variable* cache_time_us()     { return cache_time_us_; }
00246   Variable* cache_hits()        { return cache_hits_; }
00247   Variable* cache_misses()      { return cache_misses_; }
00248   Variable* cache_fallbacks()   { return cache_fallbacks_; }
00249   Variable* cache_expirations() { return cache_expirations_; }
00250   Variable* cache_inserts()     { return cache_inserts_; }
00251   Variable* cache_deletes()     { return cache_deletes_; }
00252 
00253   int64 remember_not_cacheable_ttl_seconds() {
00254     return remember_not_cacheable_ttl_seconds_;
00255   }
00256 
00257   virtual void set_remember_not_cacheable_ttl_seconds(int64 value) {
00258     DCHECK_LE(0, value);
00259     if (value >= 0) {
00260       remember_not_cacheable_ttl_seconds_ = value;
00261     }
00262   }
00263 
00264   int64 remember_fetch_failed_ttl_seconds() {
00265     return remember_fetch_failed_ttl_seconds_;
00266   }
00267 
00268   virtual void set_remember_fetch_failed_ttl_seconds(int64 value) {
00269     DCHECK_LE(0, value);
00270     if (value >= 0) {
00271       remember_fetch_failed_ttl_seconds_ = value;
00272     }
00273   }
00274 
00275   int64 remember_fetch_dropped_ttl_seconds() {
00276     return remember_fetch_dropped_ttl_seconds_;
00277   }
00278 
00279   virtual void set_remember_fetch_dropped_ttl_seconds(int64 value) {
00280     DCHECK_LE(0, value);
00281     if (value >= 0) {
00282       remember_fetch_dropped_ttl_seconds_ = value;
00283     }
00284   }
00285 
00286   int max_cacheable_response_content_length() {
00287     return max_cacheable_response_content_length_;
00288   }
00289 
00290   virtual void set_max_cacheable_response_content_length(int64 value);
00291 
00292   virtual const char* Name() const { return name_.c_str(); }
00293 
00294  protected:
00295   virtual void PutInternal(const GoogleString& key, int64 start_us,
00296                            HTTPValue* value);
00297 
00298  private:
00299   friend class HTTPCacheCallback;
00300   friend class WriteThroughHTTPCache;
00301 
00302   bool IsCurrentlyValid(const RequestHeaders* request_headers,
00303                         const ResponseHeaders& headers, int64 now_ms);
00304 
00305   bool MayCacheUrl(const GoogleString& url, const ResponseHeaders& headers);
00310   HTTPValue* ApplyHeaderChangesForPut(
00311       const GoogleString& key, int64 start_us, const StringPiece* content,
00312       ResponseHeaders* headers, HTTPValue* value, MessageHandler* handler);
00313   void UpdateStats(FindResult result, bool has_fallback, int64 delta_us);
00314   void RememberFetchFailedorNotCacheableHelper(
00315       const GoogleString& key, MessageHandler* handler, HttpStatus::Code code,
00316       int64 ttl_sec);
00317 
00318   CacheInterface* cache_; 
00319   Timer* timer_;
00320   Hasher* hasher_;
00321   bool force_caching_;
00323   bool disable_html_caching_on_https_;
00324   Variable* cache_time_us_;
00325   Variable* cache_hits_;
00326   Variable* cache_misses_;
00327   Variable* cache_fallbacks_;
00328   Variable* cache_expirations_;
00329   Variable* cache_inserts_;
00330   Variable* cache_deletes_;
00331   GoogleString name_;
00332   int64 remember_not_cacheable_ttl_seconds_;
00333   int64 remember_fetch_failed_ttl_seconds_;
00334   int64 remember_fetch_dropped_ttl_seconds_;
00335   int64 max_cacheable_response_content_length_;
00336   AtomicBool ignore_failure_puts_;
00337 
00338   DISALLOW_COPY_AND_ASSIGN(HTTPCache);
00339 };
00340 
00341 }  
00342 
00343 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_HTTP_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines