2
* Copyright 2010 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
20
#define PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
23
#include "pagespeed/kernel/base/basictypes.h"
24
#include "pagespeed/kernel/base/cache_interface.h"
25
#include "pagespeed/kernel/base/shared_string.h"
26
#include "pagespeed/kernel/base/string.h"
27
#include "pagespeed/kernel/base/string_util.h"
28
#include "pagespeed/kernel/cache/lru_cache_base.h"
30
namespace net_instaweb {
32
// Simple C++ implementation of an in-memory least-recently used (LRU)
33
// cache. This implementation is not thread-safe, and must be
34
// combined with a mutex to make it so.
36
// The purpose of this implementation is as a default implementation,
37
// or an local shadow for memcached.
39
// Also of note: the Get interface allows for streaming. To get into
40
// a GoogleString, use a StringWriter.
42
// TODO(jmarantz): The Put interface does not currently stream, but this
44
class LRUCache : public CacheInterface {
46
explicit LRUCache(size_t max_size);
49
virtual void Get(const GoogleString& key, Callback* callback);
51
// Puts an object into the cache, sharing the bytes.
53
// TODO(jmarantz): currently if the caller mutates the
54
// SharedString after having called Put, it will actually
55
// modify the value in the cache. We should change
56
// SharedString to Copy-On-Write semantics.
57
virtual void Put(const GoogleString& key, SharedString* new_value);
58
virtual void Delete(const GoogleString& key);
60
// Total size in bytes of keys and values stored.
61
size_t size_bytes() const { return base_.size_bytes(); }
64
size_t max_bytes_in_cache() const { return base_.max_bytes_in_cache(); }
66
// Number of elements stored
67
size_t num_elements() const { return base_.num_elements(); }
69
size_t num_evictions() const { return base_.num_evictions(); }
70
size_t num_hits() const { return base_.num_hits(); }
71
size_t num_misses() const { return base_.num_misses(); }
72
size_t num_inserts() const { return base_.num_inserts(); }
73
size_t num_identical_reinserts() const {
74
return base_.num_identical_reinserts();
76
size_t num_deletes() const { return base_.num_deletes(); }
78
// Sanity check the cache data structures.
79
void SanityCheck() { base_.SanityCheck(); }
81
// Clear the entire cache. Used primarily for testing. Note that this
82
// will not clear the stats, however it will update current_bytes_in_cache_.
83
void Clear() { base_.Clear(); }
85
// Clear the stats -- note that this will not clear the content.
86
void ClearStats() { base_.ClearStats(); }
88
static GoogleString FormatName() { return "LRUCache"; }
89
virtual GoogleString Name() const { return FormatName(); }
90
virtual bool IsBlocking() const { return true; }
91
virtual bool IsHealthy() const { return is_healthy_; }
92
virtual void ShutDown() { set_is_healthy(false); }
94
void set_is_healthy(bool x) { is_healthy_ = x; }
97
struct SharedStringHelper {
98
size_t size(const SharedString& ss) const {
101
bool Equal(const SharedString& a, const SharedString& b) const {
102
return a.Value() == b.Value();
104
void EvictNotify(const SharedString& a) {}
105
bool ShouldReplace(const SharedString& old_value,
106
const SharedString& new_value) const {
110
typedef LRUCacheBase<SharedString, SharedStringHelper> Base;
114
SharedStringHelper value_helper_;
116
DISALLOW_COPY_AND_ASSIGN(LRUCache);
119
} // namespace net_instaweb
121
#endif // PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_