Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/lru_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_LRU_CACHE_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_LRU_CACHE_H_
00021 
00022 #include <cstddef>
00023 #include <list>
00024 #include <map>
00025 #include <utility>  
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/cache_interface.h"
00028 #include "net/instaweb/util/public/shared_string.h"
00029 #include "net/instaweb/util/public/string.h"
00030 
00031 namespace net_instaweb {
00032 
00045 class LRUCache : public CacheInterface {
00046  public:
00047   explicit LRUCache(size_t max_size)
00048       : max_bytes_in_cache_(max_size),
00049         current_bytes_in_cache_(0),
00050         is_healthy_(true) {
00051     ClearStats();
00052   }
00053   virtual ~LRUCache();
00054 
00055   virtual void Get(const GoogleString& key, Callback* callback);
00056 
00063   virtual void Put(const GoogleString& key, SharedString* new_value);
00064   virtual void Delete(const GoogleString& key);
00065 
00067   size_t size_bytes() const { return current_bytes_in_cache_; }
00068 
00070   size_t num_elements() const { return map_.size(); }
00071 
00072   size_t num_evictions() const { return num_evictions_; }
00073   size_t num_hits() const { return num_hits_; }
00074   size_t num_misses() const { return num_misses_; }
00075   size_t num_inserts() const { return num_inserts_; }
00076   size_t num_identical_reinserts() const { return num_identical_reinserts_; }
00077   size_t num_deletes() const { return num_deletes_; }
00078 
00080   void SanityCheck();
00081 
00084   void Clear();
00085 
00087   void ClearStats();
00088 
00089   virtual const char* Name() const { return "LRUCache"; }
00090   virtual bool IsBlocking() const { return true; }
00091   virtual bool IsHealthy() const { return is_healthy_; }
00092   virtual void ShutDown() { set_is_healthy(false); }
00093 
00094   void set_is_healthy(bool x) { is_healthy_ = x; }
00095 
00096  private:
00097   typedef std::pair<const GoogleString*, SharedString> KeyValuePair;
00098   typedef std::list<KeyValuePair*> EntryList;
00100   typedef EntryList::iterator ListNode;
00101   typedef std::map<GoogleString, ListNode> Map;
00102   inline size_t entry_size(KeyValuePair* kvp) const;
00103   inline ListNode Freshen(KeyValuePair* key_value);
00104   bool EvictIfNecessary(size_t bytes_needed);
00105 
00106   size_t max_bytes_in_cache_;
00107   size_t current_bytes_in_cache_;
00108   size_t num_evictions_;
00109   size_t num_hits_;
00110   size_t num_misses_;
00111   size_t num_inserts_;
00112   size_t num_identical_reinserts_;
00113   size_t num_deletes_;
00114   bool is_healthy_;
00115   EntryList lru_ordered_list_;
00116   Map map_;
00117 
00118   DISALLOW_COPY_AND_ASSIGN(LRUCache);
00119 };
00120 
00121 }  
00122 
00123 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_LRU_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines