Page Speed Optimization Libraries
1.2.24.1
|
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_CACHE_INTERFACE_H_ 00020 #define NET_INSTAWEB_UTIL_PUBLIC_CACHE_INTERFACE_H_ 00021 00022 #include <vector> 00023 00024 #include "base/logging.h" 00025 #include "net/instaweb/util/public/basictypes.h" 00026 #include "net/instaweb/util/public/shared_string.h" 00027 #include "net/instaweb/util/public/string.h" 00028 00029 namespace net_instaweb { 00030 00032 class CacheInterface { 00033 public: 00034 enum KeyState { 00035 kAvailable, 00036 kNotFound 00037 }; 00038 00039 class Callback { 00040 public: 00041 virtual ~Callback(); 00042 SharedString* value() { return &value_; } 00043 00047 bool DelegatedValidateCandidate(const GoogleString& key, KeyState state) { 00048 return ValidateCandidate(key, state); 00049 } 00050 00051 void DelegatedDone(KeyState state) { 00052 Done(state); 00053 } 00054 00055 protected: 00056 friend class CacheInterface; 00057 00071 virtual bool ValidateCandidate(const GoogleString& key, 00072 KeyState state) { return true; } 00073 00080 virtual void Done(KeyState state) = 0; 00081 00082 private: 00083 SharedString value_; 00084 }; 00085 00088 class SynchronousCallback : public Callback { 00089 public: 00090 SynchronousCallback() { Reset(); } 00091 00092 bool called() const { return called_; } 00093 KeyState state() const { return state_; } 00095 00096 void Reset() { 00097 called_ = false; 00098 state_ = CacheInterface::kNotFound; 00099 SharedString empty; 00100 *value() = empty; 00101 } 00102 00103 virtual void Done(CacheInterface::KeyState state) { 00104 called_ = true; 00105 state_ = state; 00106 } 00107 00108 private: 00109 bool called_; 00110 CacheInterface::KeyState state_; 00111 00112 DISALLOW_COPY_AND_ASSIGN(SynchronousCallback); 00113 }; 00114 00116 struct KeyCallback { 00117 KeyCallback(const GoogleString& k, Callback* c) : key(k), callback(c) {} 00118 GoogleString key; 00119 Callback* callback; 00120 }; 00121 typedef std::vector<KeyCallback> MultiGetRequest; 00122 00123 virtual ~CacheInterface(); 00124 00131 virtual void Get(const GoogleString& key, Callback* callback) = 0; 00132 00140 virtual void MultiGet(MultiGetRequest* request); 00141 00145 virtual void Put(const GoogleString& key, SharedString* value) = 0; 00146 virtual void Delete(const GoogleString& key) = 0; 00147 00151 void PutSwappingString(const GoogleString& key, GoogleString* value) { 00152 SharedString shared_string; 00153 shared_string.SwapWithString(value); 00154 Put(key, &shared_string); 00155 } 00156 00158 virtual const char* Name() const = 0; 00159 00162 virtual bool IsBlocking() const = 0; 00163 00170 virtual bool IsHealthy() const = 0; 00171 00176 virtual void ShutDown() = 0; 00177 00191 virtual bool MustEncodeKeyInValueOnPut() const { return false; } 00192 00196 virtual void PutWithKeyInValue(const GoogleString& key, 00197 SharedString* key_and_value) { 00198 CHECK(false); 00199 } 00200 00201 protected: 00203 void ValidateAndReportResult(const GoogleString& key, KeyState state, 00204 Callback* callback); 00205 00208 void ReportMultiGetNotFound(MultiGetRequest* request); 00209 }; 00210 00211 } 00212 00213 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_CACHE_INTERFACE_H_