Page Speed Optimization Libraries  1.8.31.2
net/instaweb/system/public/apr_mem_cache.h
Go to the documentation of this file.
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 Variable;
00043 
00049 class AprMemCache : public CacheInterface {
00050  public:
00059   static const size_t kValueSizeThreshold = 1 * 1000 * 1000;
00060 
00062   static const int64 kHealthCheckpointIntervalMs = 30 * Timer::kSecondMs;
00063 
00067   static const int64 kMaxErrorBurst = 4;
00068 
00074   AprMemCache(const StringPiece& servers, int thread_limit, Hasher* hasher,
00075               Statistics* statistics, Timer* timer, MessageHandler* handler);
00076   ~AprMemCache();
00077 
00078   static void InitStats(Statistics* statistics);
00079 
00080   const GoogleString& server_spec() const { return server_spec_; }
00081 
00083   virtual void Get(const GoogleString& key, Callback* callback);
00084   virtual void Put(const GoogleString& key, SharedString* value);
00085   virtual void Delete(const GoogleString& key);
00086   virtual void MultiGet(MultiGetRequest* request);
00087 
00090   bool Connect();
00091 
00092   bool valid_server_spec() const { return valid_server_spec_; }
00093 
00096   bool GetStatus(GoogleString* status_string);
00097 
00098   static GoogleString FormatName() { return "AprMemCache"; }
00099   virtual GoogleString Name() const { return FormatName(); }
00100 
00101   virtual bool IsBlocking() const { return true; }
00102 
00105   void RecordError();
00106 
00112   virtual bool IsHealthy() const;
00113 
00115   virtual void ShutDown();
00116 
00117   virtual bool MustEncodeKeyInValueOnPut() const { return true; }
00118   virtual void PutWithKeyInValue(const GoogleString& key,
00119                                  SharedString* key_and_value);
00120 
00123   void set_timeout_us(int timeout_us);
00124 
00125  private:
00126   void DecodeValueMatchingKeyAndCallCallback(
00127       const GoogleString& key, const char* data, size_t data_len,
00128       const char* calling_method, Callback* callback);
00129 
00133   void PutHelper(const GoogleString& key, SharedString* key_and_value);
00134 
00135   StringVector hosts_;
00136   std::vector<int> ports_;
00137   GoogleString server_spec_;
00138   bool valid_server_spec_;
00139   int thread_limit_;
00140   int timeout_us_;
00141   apr_pool_t* pool_;
00142   apr_memcache2_t* memcached_;
00143   std::vector<apr_memcache2_server_t*> servers_;
00144   Hasher* hasher_;
00145   Timer* timer_;
00146   AtomicBool shutdown_;
00147 
00148   Variable* timeouts_;
00149   Variable* last_error_checkpoint_ms_;
00150   Variable* error_burst_size_;
00151 
00152   bool is_machine_local_;
00153   MessageHandler* message_handler_;
00154 
00166   Variable* last_apr_error_;
00167 
00168   DISALLOW_COPY_AND_ASSIGN(AprMemCache);
00169 };
00170 
00171 }  
00172 
00173 #endif  ///< NET_INSTAWEB_SYSTEM_PUBLIC_APR_MEM_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines