Page Speed Optimization Libraries
1.8.31.3
|
00001 /* 00002 * Copyright 2012 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_SYSTEM_PUBLIC_APR_MEM_CACHE_H_ 00020 #define NET_INSTAWEB_SYSTEM_PUBLIC_APR_MEM_CACHE_H_ 00021 00022 #include <cstddef> 00023 #include <vector> 00024 00025 #include "net/instaweb/util/public/atomic_bool.h" 00026 #include "net/instaweb/util/public/basictypes.h" 00027 #include "net/instaweb/util/public/cache_interface.h" 00028 #include "net/instaweb/util/public/string.h" 00029 #include "net/instaweb/util/public/string_util.h" 00030 #include "net/instaweb/util/public/timer.h" 00031 00032 struct apr_memcache2_t; 00033 struct apr_memcache2_server_t; 00034 struct apr_pool_t; 00035 00036 namespace net_instaweb { 00037 00038 class Hasher; 00039 class MessageHandler; 00040 class SharedString; 00041 class Statistics; 00042 class UpDownCounter; 00043 class Variable; 00044 00050 class AprMemCache : public CacheInterface { 00051 public: 00060 static const size_t kValueSizeThreshold = 1 * 1000 * 1000; 00061 00063 static const int64 kHealthCheckpointIntervalMs = 30 * Timer::kSecondMs; 00064 00068 static const int64 kMaxErrorBurst = 4; 00069 00075 AprMemCache(const StringPiece& servers, int thread_limit, Hasher* hasher, 00076 Statistics* statistics, Timer* timer, MessageHandler* handler); 00077 ~AprMemCache(); 00078 00079 static void InitStats(Statistics* statistics); 00080 00081 const GoogleString& server_spec() const { return server_spec_; } 00082 00084 virtual void Get(const GoogleString& key, Callback* callback); 00085 virtual void Put(const GoogleString& key, SharedString* value); 00086 virtual void Delete(const GoogleString& key); 00087 virtual void MultiGet(MultiGetRequest* request); 00088 00091 bool Connect(); 00092 00093 bool valid_server_spec() const { return valid_server_spec_; } 00094 00097 bool GetStatus(GoogleString* status_string); 00098 00099 static GoogleString FormatName() { return "AprMemCache"; } 00100 virtual GoogleString Name() const { return FormatName(); } 00101 00102 virtual bool IsBlocking() const { return true; } 00103 00106 void RecordError(); 00107 00113 virtual bool IsHealthy() const; 00114 00116 virtual void ShutDown(); 00117 00118 virtual bool MustEncodeKeyInValueOnPut() const { return true; } 00119 virtual void PutWithKeyInValue(const GoogleString& key, 00120 SharedString* key_and_value); 00121 00124 void set_timeout_us(int timeout_us); 00125 00126 private: 00127 void DecodeValueMatchingKeyAndCallCallback( 00128 const GoogleString& key, const char* data, size_t data_len, 00129 const char* calling_method, Callback* callback); 00130 00134 void PutHelper(const GoogleString& key, SharedString* key_and_value); 00135 00136 StringVector hosts_; 00137 std::vector<int> ports_; 00138 GoogleString server_spec_; 00139 bool valid_server_spec_; 00140 int thread_limit_; 00141 int timeout_us_; 00142 apr_pool_t* pool_; 00143 apr_memcache2_t* memcached_; 00144 std::vector<apr_memcache2_server_t*> servers_; 00145 Hasher* hasher_; 00146 Timer* timer_; 00147 AtomicBool shutdown_; 00148 00149 Variable* timeouts_; 00150 UpDownCounter* last_error_checkpoint_ms_; 00151 UpDownCounter* error_burst_size_; 00152 00153 bool is_machine_local_; 00154 MessageHandler* message_handler_; 00155 00167 Variable* last_apr_error_; 00168 00169 DISALLOW_COPY_AND_ASSIGN(AprMemCache); 00170 }; 00171 00172 } 00173 00174 #endif ///< NET_INSTAWEB_SYSTEM_PUBLIC_APR_MEM_CACHE_H_