Page Speed Optimization Libraries  1.5.27.2
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   static GoogleString FormatName() { return "FileCache"; }
00070   virtual GoogleString Name() const { return FormatName(); }
00071 
00072   virtual bool IsBlocking() const { return true; }
00073   virtual bool IsHealthy() const { return true; }
00074   virtual void ShutDown() {}  
00075 
00076   const CachePolicy* cache_policy() const { return cache_policy_.get(); }
00077   const GoogleString& path() const { return path_; }
00078 
00081   static const char kDiskChecks[];
00083   static const char kCleanups[];
00085   static const char kEvictions[];
00086   static const char kBytesFreedInCleanup[];
00087 
00088  private:
00089   class CacheCleanFunction;
00090   friend class FileCacheTest;
00091   friend class CacheCleanFunction;
00092 
00098   bool Clean(int64 target_size, int64 target_inode_count);
00099 
00102   bool CleanWithLocking(int64 next_clean_time_ms);
00103 
00106   bool ShouldClean(int64* suggested_next_clean_time_ms);
00107 
00112   void CleanIfNeeded();
00113 
00114   bool EncodeFilename(const GoogleString& key, GoogleString* filename);
00115 
00116   const GoogleString path_;
00117   FileSystem* file_system_;
00118   SlowWorker* worker_;
00119   FilenameEncoder* filename_encoder_;
00120   MessageHandler* message_handler_;
00121   const scoped_ptr<CachePolicy> cache_policy_;
00122   int64 next_clean_ms_;
00123   int path_length_limit_; 
00124 
00125   GoogleString clean_time_path_;
00126   GoogleString clean_lock_path_;
00127   bool last_conditional_clean_result_;
00128 
00129   Variable* disk_checks_;
00130   Variable* cleanups_;
00131   Variable* evictions_;
00132   Variable* bytes_freed_in_cleanup_;
00133 
00135   static const char kCleanTimeName[];
00137   static const char kCleanLockName[];
00138 
00139   DISALLOW_COPY_AND_ASSIGN(FileCache);
00140 };
00141 
00142 }  
00143 
00144 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines