Page Speed Optimization Libraries  1.4.26.1
net/instaweb/util/public/file_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_UTIL_PUBLIC_FILE_CACHE_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_FILE_CACHE_H_
00021 
00022 #include "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/util/public/cache_interface.h"
00024 #include "net/instaweb/util/public/scoped_ptr.h"
00025 #include "net/instaweb/util/public/string.h"
00026 
00027 namespace net_instaweb {
00028 
00029 class FileSystem;
00030 class FilenameEncoder;
00031 class Hasher;
00032 class MessageHandler;
00033 class SharedString;
00034 class SlowWorker;
00035 class Statistics;
00036 class Timer;
00037 class Variable;
00038 
00040 class FileCache : public CacheInterface {
00041  public:
00042   struct CachePolicy {
00043     CachePolicy(Timer* timer, Hasher* hasher, int64 clean_interval_ms,
00044                 int64 target_size, int64 target_inode_count)
00045         : timer(timer), hasher(hasher), clean_interval_ms(clean_interval_ms),
00046           target_size(target_size), target_inode_count(target_inode_count) {}
00047     const Timer* timer;
00048     const Hasher* hasher;
00049     const int64 clean_interval_ms;
00050     const int64 target_size;
00051     const int64 target_inode_count;
00052    private:
00053     DISALLOW_COPY_AND_ASSIGN(CachePolicy);
00054   };
00055 
00056   FileCache(const GoogleString& path, FileSystem* file_system,
00057             SlowWorker* worker, FilenameEncoder* filename_encoder,
00058             CachePolicy* policy, Statistics* stats, MessageHandler* handler);
00059   virtual ~FileCache();
00060 
00061   static void InitStats(Statistics* statistics);
00062 
00063   virtual void Get(const GoogleString& key, Callback* callback);
00064   virtual void Put(const GoogleString& key, SharedString* value);
00065   virtual void Delete(const GoogleString& key);
00066   void set_worker(SlowWorker* worker) { worker_ = worker; }
00067   SlowWorker* worker() { return worker_; }
00068 
00069   virtual const char* Name() const { return "FileCache"; }
00070   virtual bool IsBlocking() const { return true; }
00071   virtual bool IsHealthy() const { return true; }
00072   virtual void ShutDown() {}  
00073 
00074   const CachePolicy* cache_policy() const { return cache_policy_.get(); }
00075   const GoogleString& path() const { return path_; }
00076 
00079   static const char kDiskChecks[];
00081   static const char kCleanups[];
00083   static const char kEvictions[];
00084   static const char kBytesFreedInCleanup[];
00085 
00086  private:
00087   class CacheCleanFunction;
00088   friend class FileCacheTest;
00089   friend class CacheCleanFunction;
00090 
00096   bool Clean(int64 target_size, int64 target_inode_count);
00097 
00100   bool CleanWithLocking(int64 next_clean_time_ms);
00101 
00104   bool ShouldClean(int64* suggested_next_clean_time_ms);
00105 
00110   void CleanIfNeeded();
00111 
00112   bool EncodeFilename(const GoogleString& key, GoogleString* filename);
00113 
00114   const GoogleString path_;
00115   FileSystem* file_system_;
00116   SlowWorker* worker_;
00117   FilenameEncoder* filename_encoder_;
00118   MessageHandler* message_handler_;
00119   const scoped_ptr<CachePolicy> cache_policy_;
00120   int64 next_clean_ms_;
00121   int path_length_limit_; 
00122 
00123   GoogleString clean_time_path_;
00124   GoogleString clean_lock_path_;
00125   bool last_conditional_clean_result_;
00126 
00127   Variable* disk_checks_;
00128   Variable* cleanups_;
00129   Variable* evictions_;
00130   Variable* bytes_freed_in_cleanup_;
00131 
00133   static const char kCleanTimeName[];
00135   static const char kCleanLockName[];
00136 
00137   DISALLOW_COPY_AND_ASSIGN(FileCache);
00138 };
00139 
00140 }  
00141 
00142 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines