Page Speed Optimization Libraries
1.5.27.2
|
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 max_bytes_in_cache() const { return max_bytes_in_cache_; } 00071 00073 size_t num_elements() const { return map_.size(); } 00074 00075 size_t num_evictions() const { return num_evictions_; } 00076 size_t num_hits() const { return num_hits_; } 00077 size_t num_misses() const { return num_misses_; } 00078 size_t num_inserts() const { return num_inserts_; } 00079 size_t num_identical_reinserts() const { return num_identical_reinserts_; } 00080 size_t num_deletes() const { return num_deletes_; } 00081 00083 void SanityCheck(); 00084 00087 void Clear(); 00088 00090 void ClearStats(); 00091 00092 static GoogleString FormatName() { return "LRUCache"; } 00093 virtual GoogleString Name() const { return FormatName(); } 00094 virtual bool IsBlocking() const { return true; } 00095 virtual bool IsHealthy() const { return is_healthy_; } 00096 virtual void ShutDown() { set_is_healthy(false); } 00097 00098 void set_is_healthy(bool x) { is_healthy_ = x; } 00099 00100 private: 00101 typedef std::pair<const GoogleString*, SharedString> KeyValuePair; 00102 typedef std::list<KeyValuePair*> EntryList; 00104 typedef EntryList::iterator ListNode; 00105 typedef std::map<GoogleString, ListNode> Map; 00106 inline size_t entry_size(KeyValuePair* kvp) const; 00107 inline ListNode Freshen(KeyValuePair* key_value); 00108 bool EvictIfNecessary(size_t bytes_needed); 00109 00110 size_t max_bytes_in_cache_; 00111 size_t current_bytes_in_cache_; 00112 size_t num_evictions_; 00113 size_t num_hits_; 00114 size_t num_misses_; 00115 size_t num_inserts_; 00116 size_t num_identical_reinserts_; 00117 size_t num_deletes_; 00118 bool is_healthy_; 00119 EntryList lru_ordered_list_; 00120 Map map_; 00121 00122 DISALLOW_COPY_AND_ASSIGN(LRUCache); 00123 }; 00124 00125 } 00126 00127 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_LRU_CACHE_H_