Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_interface.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_BASE_CACHE_INTERFACE_H_
20 #define PAGESPEED_KERNEL_BASE_CACHE_INTERFACE_H_
21 
22 #include <vector>
23 
24 #include "base/logging.h"
28 
29 namespace net_instaweb {
30 
33  public:
34  enum KeyState {
35  kAvailable = 0,
36  kNotFound = 1,
37  kOverload = 2,
39  kTimeout = 4,
40  };
41 
42  class Callback {
43  public:
44  virtual ~Callback();
45  void set_value(const SharedString& value) { value_ = value; }
46  const SharedString& value() const { return value_; }
47 
52  return ValidateCandidate(key, state);
53  }
54 
55  void DelegatedDone(KeyState state) {
56  Done(state);
57  }
58 
59  protected:
60  friend class CacheInterface;
61 
75  virtual bool ValidateCandidate(const GoogleString& key,
76  KeyState state) { return true; }
77 
84  virtual void Done(KeyState state) = 0;
85 
86  private:
87  SharedString value_;
88  };
89 
92  class SynchronousCallback : public Callback {
93  public:
94  SynchronousCallback() { Reset(); }
95 
96  bool called() const { return called_; }
97  KeyState state() const { return state_; }
99 
100  void Reset() {
101  called_ = false;
102  state_ = CacheInterface::kNotFound;
103  SharedString empty;
104  set_value(empty);
105  }
106 
107  virtual void Done(CacheInterface::KeyState state) {
108  called_ = true;
109  state_ = state;
110  }
111 
112  private:
113  bool called_;
115 
116 
117  };
118 
120  struct KeyCallback {
121  KeyCallback(const GoogleString& k, Callback* c) : key(k), callback(c) {}
122  GoogleString key;
123  Callback* callback;
124  };
125  typedef std::vector<KeyCallback> MultiGetRequest;
126 
127  static const char* KeyStateName(KeyState state);
128 
129  CacheInterface();
130  virtual ~CacheInterface();
131 
138  virtual void Get(const GoogleString& key, Callback* callback) = 0;
139 
147  virtual void MultiGet(MultiGetRequest* request);
148 
152  virtual void Put(const GoogleString& key, const SharedString& value) = 0;
153  virtual void Delete(const GoogleString& key) = 0;
154 
158  void PutSwappingString(const GoogleString& key, GoogleString* value) {
159  SharedString shared_string;
160  shared_string.SwapWithString(value);
161  Put(key, shared_string);
162  }
163 
169  virtual GoogleString Name() const = 0;
170 
174  virtual CacheInterface* Backend();
175 
178  virtual bool IsBlocking() const = 0;
179 
190  virtual bool IsHealthy() const = 0;
191 
196  virtual void ShutDown() = 0;
197 
211  virtual bool MustEncodeKeyInValueOnPut() const { return false; }
212 
216  virtual void PutWithKeyInValue(const GoogleString& key,
217  const SharedString& key_and_value) {
218  CHECK(false);
219  }
220 
221  protected:
223  void ValidateAndReportResult(const GoogleString& key, KeyState state,
224  Callback* callback);
225 
228  void ReportMultiGetNotFound(MultiGetRequest* request);
229 };
230 
231 }
232 
233 #endif
virtual void Put(const GoogleString &key, const SharedString &value)=0
Abstract interface for a cache.
Definition: cache_interface.h:32
virtual bool ValidateCandidate(const GoogleString &key, KeyState state)
Definition: cache_interface.h:75
Requested key needs to be written.
Definition: cache_interface.h:36
Cache lookup ended up in network error.
Definition: cache_interface.h:38
Lookup is discarded due to cache server is overloaded.
Definition: cache_interface.h:37
void SwapWithString(GoogleString *str)
bool DelegatedValidateCandidate(const GoogleString &key, KeyState state)
Definition: cache_interface.h:51
Request timeout.
Definition: cache_interface.h:39
Requested key is available for serving.
Definition: cache_interface.h:35
void PutSwappingString(const GoogleString &key, GoogleString *value)
Definition: cache_interface.h:158
virtual void Done(CacheInterface::KeyState state)
Definition: cache_interface.h:107
virtual CacheInterface * Backend()
KeyState
Definition: cache_interface.h:34
virtual void MultiGet(MultiGetRequest *request)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void Reset()
super.value(), super.set_value() are used to get/set the value.
Definition: cache_interface.h:100
virtual bool IsHealthy() const =0
virtual bool IsBlocking() const =0
virtual void Get(const GoogleString &key, Callback *callback)=0
Vector of structures used to initiate a MultiGet.
Definition: cache_interface.h:120
Definition: shared_string.h:40
Definition: cache_interface.h:42
void ReportMultiGetNotFound(MultiGetRequest *request)
virtual void PutWithKeyInValue(const GoogleString &key, const SharedString &key_and_value)
Definition: cache_interface.h:216
virtual bool MustEncodeKeyInValueOnPut() const
Definition: cache_interface.h:211
virtual GoogleString Name() const =0
void ValidateAndReportResult(const GoogleString &key, KeyState state, Callback *callback)
Invokes callback->ValidateCandidate() and callback->Done() as appropriate.
virtual void Done(KeyState state)=0