Page Speed Optimization Libraries  1.2.24.1
net/instaweb/apache/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_APACHE_APR_MEM_CACHE_H_
00020 #define NET_INSTAWEB_APACHE_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   virtual const char* Name() const { return "AprMemCache"; }
00099   virtual bool IsBlocking() const { return true; }
00100 
00103   void RecordError();
00104 
00110   virtual bool IsHealthy() const;
00111 
00113   virtual void ShutDown();
00114 
00115   virtual bool MustEncodeKeyInValueOnPut() const { return true; }
00116   virtual void PutWithKeyInValue(const GoogleString& key,
00117                                  SharedString* key_and_value);
00118 
00121   void set_timeout_us(int timeout_us);
00122 
00123  private:
00124   void DecodeValueMatchingKeyAndCallCallback(
00125       const GoogleString& key, const char* data, size_t data_len,
00126       const char* calling_method, Callback* callback);
00127 
00131   void PutHelper(const GoogleString& key, SharedString* key_and_value);
00132 
00133   StringVector hosts_;
00134   std::vector<int> ports_;
00135   GoogleString server_spec_;
00136   bool valid_server_spec_;
00137   int thread_limit_;
00138   int timeout_us_;
00139   apr_pool_t* pool_;
00140   apr_memcache2_t* memcached_;
00141   std::vector<apr_memcache2_server_t*> servers_;
00142   Hasher* hasher_;
00143   Timer* timer_;
00144   AtomicBool shutdown_;
00145 
00146   Variable* timeouts_;
00147   Variable* last_error_checkpoint_ms_;
00148   Variable* error_burst_size_;
00149 
00150   bool is_machine_local_;
00151   MessageHandler* message_handler_;
00152 
00164   Variable* last_apr_error_;
00165 
00166   DISALLOW_COPY_AND_ASSIGN(AprMemCache);
00167 };
00168 
00169 }  
00170 
00171 #endif  ///< NET_INSTAWEB_APACHE_APR_MEM_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines