00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 "base/scoped_ptr.h"
00024 #include "net/instaweb/util/public/cache_interface.h"
00025 #include "net/instaweb/util/public/string.h"
00026
00027 namespace net_instaweb {
00028 class FileSystem;
00029 class FilenameEncoder;
00030 class Hasher;
00031 class MessageHandler;
00032 class SharedString;
00033 class SlowWorker;
00034 class Timer;
00035
00037 class FileCache : public CacheInterface {
00038 public:
00039 struct CachePolicy {
00040 CachePolicy(Timer* timer, Hasher* hasher,
00041 int64 clean_interval_ms, int64 target_size)
00042 : timer(timer), hasher(hasher), clean_interval_ms(clean_interval_ms),
00043 target_size(target_size) {}
00044 const Timer* timer;
00045 const Hasher* hasher;
00046 const int64 clean_interval_ms;
00047 const int64 target_size;
00048 private:
00049 DISALLOW_COPY_AND_ASSIGN(CachePolicy);
00050 };
00051
00052 FileCache(const GoogleString& path, FileSystem* file_system,
00053 SlowWorker* worker, FilenameEncoder* filename_encoder,
00054 CachePolicy* policy, MessageHandler* handler);
00055 virtual ~FileCache();
00056
00057 virtual void Get(const GoogleString& key, Callback* callback);
00058 virtual void Put(const GoogleString& key, SharedString* value);
00059 virtual void Delete(const GoogleString& key);
00060 void set_worker(SlowWorker* worker) { worker_ = worker; }
00061
00062 virtual const char* Name() const { return "FileCache"; }
00063
00064 private:
00065 class CacheCleanFunction;
00066 friend class FileCacheTest;
00067 friend class CacheCleanFunction;
00068
00074 bool Clean(int64 target_size);
00075
00078 bool CleanWithLocking(int64 next_clean_time_ms);
00079
00082 bool ShouldClean(int64* suggested_next_clean_time_ms);
00083
00088 void CleanIfNeeded();
00089
00090 bool EncodeFilename(const GoogleString& key, GoogleString* filename);
00091
00092 const GoogleString path_;
00093 FileSystem* file_system_;
00094 SlowWorker* worker_;
00095 FilenameEncoder* filename_encoder_;
00096 MessageHandler* message_handler_;
00097 const scoped_ptr<CachePolicy> cache_policy_;
00098 int64 next_clean_ms_;
00099 int path_length_limit_;
00100
00101 GoogleString clean_time_path_;
00102 bool last_conditional_clean_result_;
00103
00105 static const char kCleanTimeName[];
00107 static const char kCleanLockName[];
00108 DISALLOW_COPY_AND_ASSIGN(FileCache);
00109 };
00110
00111 }
00112
00113 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_CACHE_H_