Page Speed Optimization Libraries
1.5.27.2
|
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 CacheInterface(); 00127 virtual ~CacheInterface(); 00128 00135 virtual void Get(const GoogleString& key, Callback* callback) = 0; 00136 00144 virtual void MultiGet(MultiGetRequest* request); 00145 00149 virtual void Put(const GoogleString& key, SharedString* value) = 0; 00150 virtual void Delete(const GoogleString& key) = 0; 00151 00155 void PutSwappingString(const GoogleString& key, GoogleString* value) { 00156 SharedString shared_string; 00157 shared_string.SwapWithString(value); 00158 Put(key, &shared_string); 00159 } 00160 00166 virtual GoogleString Name() const = 0; 00167 00171 virtual CacheInterface* Backend(); 00172 00175 virtual bool IsBlocking() const = 0; 00176 00183 virtual bool IsHealthy() const = 0; 00184 00189 virtual void ShutDown() = 0; 00190 00204 virtual bool MustEncodeKeyInValueOnPut() const { return false; } 00205 00209 virtual void PutWithKeyInValue(const GoogleString& key, 00210 SharedString* key_and_value) { 00211 CHECK(false); 00212 } 00213 00214 protected: 00216 void ValidateAndReportResult(const GoogleString& key, KeyState state, 00217 Callback* callback); 00218 00221 void ReportMultiGetNotFound(MultiGetRequest* request); 00222 }; 00223 00224 } 00225 00226 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_CACHE_INTERFACE_H_