~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/cache/lru_cache.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
20
 
#define PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_
21
 
 
22
 
#include <cstddef>
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"
29
 
 
30
 
namespace net_instaweb {
31
 
 
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.
35
 
//
36
 
// The purpose of this implementation is as a default implementation,
37
 
// or an local shadow for memcached.
38
 
//
39
 
// Also of note: the Get interface allows for streaming.  To get into
40
 
// a GoogleString, use a StringWriter.
41
 
//
42
 
// TODO(jmarantz): The Put interface does not currently stream, but this
43
 
// should be added.
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
 
 
51
 
  // Puts an object into the cache, sharing the bytes.
52
 
  //
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);
59
 
 
60
 
  // Total size in bytes of keys and values stored.
61
 
  size_t size_bytes() const { return base_.size_bytes(); }
62
 
 
63
 
  // Maximum capacity.
64
 
  size_t max_bytes_in_cache() const { return base_.max_bytes_in_cache(); }
65
 
 
66
 
  // Number of elements stored
67
 
  size_t num_elements() const { return base_.num_elements(); }
68
 
 
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();
75
 
  }
76
 
  size_t num_deletes() const { return base_.num_deletes(); }
77
 
 
78
 
  // Sanity check the cache data structures.
79
 
  void SanityCheck() { base_.SanityCheck(); }
80
 
 
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(); }
84
 
 
85
 
  // Clear the stats -- note that this will not clear the content.
86
 
  void ClearStats() { base_.ClearStats(); }
87
 
 
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); }
93
 
 
94
 
  void set_is_healthy(bool x) { is_healthy_ = x; }
95
 
 
96
 
 private:
97
 
  struct SharedStringHelper {
98
 
    size_t size(const SharedString& ss) const {
99
 
      return ss.size();
100
 
    }
101
 
    bool Equal(const SharedString& a, const SharedString& b) const {
102
 
      return a.Value() == b.Value();
103
 
    }
104
 
    void EvictNotify(const SharedString& a) {}
105
 
    bool ShouldReplace(const SharedString& old_value,
106
 
                       const SharedString& new_value) const {
107
 
      return true;
108
 
    }
109
 
  };
110
 
  typedef LRUCacheBase<SharedString, SharedStringHelper> Base;
111
 
 
112
 
  Base base_;
113
 
  bool is_healthy_;
114
 
  SharedStringHelper value_helper_;
115
 
 
116
 
  DISALLOW_COPY_AND_ASSIGN(LRUCache);
117
 
};
118
 
 
119
 
}  // namespace net_instaweb
120
 
 
121
 
#endif  // PAGESPEED_KERNEL_CACHE_LRU_CACHE_H_