Page Speed Optimization Libraries
1.3.25.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 = 0, 00036 kNotFound = 1, 00037 kOverload = 2, 00038 kNetworkError = 3, 00039 kTimeout = 4, 00040 }; 00041 00042 class Callback { 00043 public: 00044 virtual ~Callback(); 00045 SharedString* value() { return &value_; } 00046 00050 bool DelegatedValidateCandidate(const GoogleString& key, KeyState state) { 00051 return ValidateCandidate(key, state); 00052 } 00053 00054 void DelegatedDone(KeyState state) { 00055 Done(state); 00056 } 00057 00058 protected: 00059 friend class CacheInterface; 00060 00074 virtual bool ValidateCandidate(const GoogleString& key, 00075 KeyState state) { return true; } 00076 00083 virtual void Done(KeyState state) = 0; 00084 00085 private: 00086 SharedString value_; 00087 }; 00088 00091 class SynchronousCallback : public Callback { 00092 public: 00093 SynchronousCallback() { Reset(); } 00094 00095 bool called() const { return called_; } 00096 KeyState state() const { return state_; } 00098 00099 void Reset() { 00100 called_ = false; 00101 state_ = CacheInterface::kNotFound; 00102 SharedString empty; 00103 *value() = empty; 00104 } 00105 00106 virtual void Done(CacheInterface::KeyState state) { 00107 called_ = true; 00108 state_ = state; 00109 } 00110 00111 private: 00112 bool called_; 00113 CacheInterface::KeyState state_; 00114 00115 DISALLOW_COPY_AND_ASSIGN(SynchronousCallback); 00116 }; 00117 00119 struct KeyCallback { 00120 KeyCallback(const GoogleString& k, Callback* c) : key(k), callback(c) {} 00121 GoogleString key; 00122 Callback* callback; 00123 }; 00124 typedef std::vector<KeyCallback> MultiGetRequest; 00125 00126 virtual ~CacheInterface(); 00127 00134 virtual void Get(const GoogleString& key, Callback* callback) = 0; 00135 00143 virtual void MultiGet(MultiGetRequest* request); 00144 00148 virtual void Put(const GoogleString& key, SharedString* value) = 0; 00149 virtual void Delete(const GoogleString& key) = 0; 00150 00154 void PutSwappingString(const GoogleString& key, GoogleString* value) { 00155 SharedString shared_string; 00156 shared_string.SwapWithString(value); 00157 Put(key, &shared_string); 00158 } 00159 00161 virtual const char* Name() const = 0; 00162 00165 virtual bool IsBlocking() const = 0; 00166 00173 virtual bool IsHealthy() const = 0; 00174 00179 virtual void ShutDown() = 0; 00180 00194 virtual bool MustEncodeKeyInValueOnPut() const { return false; } 00195 00199 virtual void PutWithKeyInValue(const GoogleString& key, 00200 SharedString* key_and_value) { 00201 CHECK(false); 00202 } 00203 00204 protected: 00206 void ValidateAndReportResult(const GoogleString& key, KeyState state, 00207 Callback* callback); 00208 00211 void ReportMultiGetNotFound(MultiGetRequest* request); 00212 }; 00213 00214 } 00215 00216 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_CACHE_INTERFACE_H_