Page Speed Optimization Libraries  1.2.24.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 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, int64 clean_interval_ms,
00041                 int64 target_size, int64 target_inode_count)
00042         : timer(timer), hasher(hasher), clean_interval_ms(clean_interval_ms),
00043           target_size(target_size), target_inode_count(target_inode_count) {}
00044     const Timer* timer;
00045     const Hasher* hasher;
00046     const int64 clean_interval_ms;
00047     const int64 target_size;
00048     const int64 target_inode_count;
00049    private:
00050     DISALLOW_COPY_AND_ASSIGN(CachePolicy);
00051   };
00052 
00053   FileCache(const GoogleString& path, FileSystem* file_system,
00054             SlowWorker* worker, FilenameEncoder* filename_encoder,
00055             CachePolicy* policy, MessageHandler* handler);
00056   virtual ~FileCache();
00057 
00058   virtual void Get(const GoogleString& key, Callback* callback);
00059   virtual void Put(const GoogleString& key, SharedString* value);
00060   virtual void Delete(const GoogleString& key);
00061   void set_worker(SlowWorker* worker) { worker_ = worker; }
00062 
00063   virtual const char* Name() const { return "FileCache"; }
00064   virtual bool IsBlocking() const { return true; }
00065   virtual bool IsHealthy() const { return true; }
00066   virtual void ShutDown() {}  
00067 
00068  private:
00069   class CacheCleanFunction;
00070   friend class FileCacheTest;
00071   friend class CacheCleanFunction;
00072 
00078   bool Clean(int64 target_size, int64 target_inode_count);
00079 
00082   bool CleanWithLocking(int64 next_clean_time_ms);
00083 
00086   bool ShouldClean(int64* suggested_next_clean_time_ms);
00087 
00092   void CleanIfNeeded();
00093 
00094   bool EncodeFilename(const GoogleString& key, GoogleString* filename);
00095 
00096   const GoogleString path_;
00097   FileSystem* file_system_;
00098   SlowWorker* worker_;
00099   FilenameEncoder* filename_encoder_;
00100   MessageHandler* message_handler_;
00101   const scoped_ptr<CachePolicy> cache_policy_;
00102   int64 next_clean_ms_;
00103   int path_length_limit_; 
00104 
00105   GoogleString clean_time_path_;
00106   bool last_conditional_clean_result_;
00107 
00109   static const char kCleanTimeName[];
00111   static const char kCleanLockName[];
00112   DISALLOW_COPY_AND_ASSIGN(FileCache);
00113 };
00114 
00115 }  
00116 
00117 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines