Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lru_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
20 #define PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
21 
22 #include <cstddef>
29 
30 namespace net_instaweb {
31 
44 class LRUCache : public CacheInterface {
45  public:
46  explicit LRUCache(size_t max_size);
47  virtual ~LRUCache();
48 
49  virtual void Get(const GoogleString& key, Callback* callback);
50 
57  virtual void Put(const GoogleString& key, const SharedString& new_value);
58  virtual void Delete(const GoogleString& key);
59 
62  void DeleteWithPrefixForTesting(StringPiece prefix);
63 
65  size_t size_bytes() const { return base_.size_bytes(); }
66 
68  size_t max_bytes_in_cache() const { return base_.max_bytes_in_cache(); }
69 
71  size_t num_elements() const { return base_.num_elements(); }
72 
73  size_t num_evictions() const { return base_.num_evictions(); }
74  size_t num_hits() const { return base_.num_hits(); }
75  size_t num_misses() const { return base_.num_misses(); }
76  size_t num_inserts() const { return base_.num_inserts(); }
77  size_t num_identical_reinserts() const {
78  return base_.num_identical_reinserts();
79  }
80  size_t num_deletes() const { return base_.num_deletes(); }
81 
83  void SanityCheck() { base_.SanityCheck(); }
84 
87  void Clear() { base_.Clear(); }
88 
90  void ClearStats() { base_.ClearStats(); }
91 
92  static GoogleString FormatName() { return "LRUCache"; }
93  virtual GoogleString Name() const { return FormatName(); }
94  virtual bool IsBlocking() const { return true; }
95  virtual bool IsHealthy() const { return is_healthy_; }
96  virtual void ShutDown() { set_is_healthy(false); }
97 
98  void set_is_healthy(bool x) { is_healthy_ = x; }
99 
100  private:
101  struct SharedStringHelper {
102  size_t size(const SharedString& ss) const {
103  return ss.size();
104  }
105  bool Equal(const SharedString& a, const SharedString& b) const {
106  return a.Value() == b.Value();
107  }
108  void EvictNotify(const SharedString& a) {}
109  bool ShouldReplace(const SharedString& old_value,
110  const SharedString& new_value) const {
111  return true;
112  }
113  };
114  typedef LRUCacheBase<SharedString, SharedStringHelper> Base;
115 
116  Base base_;
117  bool is_healthy_;
118  SharedStringHelper value_helper_;
119 
120 
121 };
122 
123 }
124 
125 #endif
size_t size_bytes() const
Total size in bytes of keys and values stored.
Definition: lru_cache_base.h:246
virtual void ShutDown()
Definition: lru_cache.h:96
Abstract interface for a cache.
Definition: cache_interface.h:32
size_t max_bytes_in_cache() const
Maximum capacity.
Definition: lru_cache.h:68
virtual bool IsHealthy() const
Definition: lru_cache.h:95
void Clear()
Definition: lru_cache_base.h:292
void DeleteWithPrefixForTesting(StringPiece prefix)
void SanityCheck()
Sanity check the cache data structures.
Definition: lru_cache_base.h:262
void ClearStats()
Clear the stats – note that this will not clear the content.
Definition: lru_cache.h:90
size_t num_elements() const
Number of elements stored.
Definition: lru_cache.h:71
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void Put(const GoogleString &key, const SharedString &new_value)
size_t num_elements() const
Number of elements stored.
Definition: lru_cache_base.h:252
Definition: lru_cache.h:44
Definition: shared_string.h:40
virtual bool IsBlocking() const
Definition: lru_cache.h:94
Definition: cache_interface.h:42
virtual GoogleString Name() const
Definition: lru_cache.h:93
size_t max_bytes_in_cache() const
Maximum capacity.
Definition: lru_cache_base.h:249
void Clear()
Definition: lru_cache.h:87
size_t size_bytes() const
Total size in bytes of keys and values stored.
Definition: lru_cache.h:65
virtual void Get(const GoogleString &key, Callback *callback)
void ClearStats()
Clear the stats – note that this will not clear the content.
Definition: lru_cache_base.h:305
void SanityCheck()
Sanity check the cache data structures.
Definition: lru_cache.h:83