Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
apr_mem_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_SYSTEM_PUBLIC_APR_MEM_CACHE_H_
20 #define NET_INSTAWEB_SYSTEM_PUBLIC_APR_MEM_CACHE_H_
21 
22 #include <cstddef>
23 #include <vector>
24 
27 #include "net/instaweb/util/public/cache_interface.h"
31 
32 struct apr_memcache2_t;
33 struct apr_memcache2_server_t;
34 struct apr_pool_t;
35 
36 namespace net_instaweb {
37 
38 class Hasher;
39 class MessageHandler;
40 class SharedString;
41 class Statistics;
42 class UpDownCounter;
43 class Variable;
44 
50 class AprMemCache : public CacheInterface {
51  public:
60  static const size_t kValueSizeThreshold = 1 * 1000 * 1000;
61 
63  static const int64 kHealthCheckpointIntervalMs = 30 * Timer::kSecondMs;
64 
68  static const int64 kMaxErrorBurst = 4;
69 
75  AprMemCache(const StringPiece& servers, int thread_limit, Hasher* hasher,
76  Statistics* statistics, Timer* timer, MessageHandler* handler);
77  ~AprMemCache();
78 
79  static void InitStats(Statistics* statistics);
80 
81  const GoogleString& server_spec() const { return server_spec_; }
82 
84  virtual void Get(const GoogleString& key, Callback* callback);
85  virtual void Put(const GoogleString& key, SharedString* value);
86  virtual void Delete(const GoogleString& key);
87  virtual void MultiGet(MultiGetRequest* request);
88 
91  bool Connect();
92 
93  bool valid_server_spec() const { return valid_server_spec_; }
94 
97  bool GetStatus(GoogleString* status_string);
98 
99  static GoogleString FormatName() { return "AprMemCache"; }
100  virtual GoogleString Name() const { return FormatName(); }
101 
102  virtual bool IsBlocking() const { return true; }
103 
106  void RecordError();
107 
113  virtual bool IsHealthy() const;
114 
116  virtual void ShutDown();
117 
118  virtual bool MustEncodeKeyInValueOnPut() const { return true; }
119  virtual void PutWithKeyInValue(const GoogleString& key,
120  SharedString* key_and_value);
121 
124  void set_timeout_us(int timeout_us);
125 
126  private:
127  void DecodeValueMatchingKeyAndCallCallback(
128  const GoogleString& key, const char* data, size_t data_len,
129  const char* calling_method, Callback* callback);
130 
134  void PutHelper(const GoogleString& key, SharedString* key_and_value);
135 
136  StringVector hosts_;
137  std::vector<int> ports_;
138  GoogleString server_spec_;
139  bool valid_server_spec_;
140  int thread_limit_;
141  int timeout_us_;
142  apr_pool_t* pool_;
143  apr_memcache2_t* memcached_;
144  std::vector<apr_memcache2_server_t*> servers_;
145  Hasher* hasher_;
146  Timer* timer_;
147  AtomicBool shutdown_;
148 
149  Variable* timeouts_;
150  UpDownCounter* last_error_checkpoint_ms_;
151  UpDownCounter* error_burst_size_;
152 
153  bool is_machine_local_;
154  MessageHandler* message_handler_;
155 
167  Variable* last_apr_error_;
168 
169  DISALLOW_COPY_AND_ASSIGN(AprMemCache);
170 };
171 
172 }
173 
174 #endif
Definition: apr_mem_cache.h:50
static const int64 kMaxErrorBurst
Definition: apr_mem_cache.h:68
static const int64 kHealthCheckpointIntervalMs
Amount of time after a burst of errors to retry memcached operations.
Definition: apr_mem_cache.h:63
AprMemCache(const StringPiece &servers, int thread_limit, Hasher *hasher, Statistics *statistics, Timer *timer, MessageHandler *handler)
static const size_t kValueSizeThreshold
Definition: apr_mem_cache.h:60
bool GetStatus(GoogleString *status_string)
void set_timeout_us(int timeout_us)
virtual void ShutDown()
Close down the connection to the memcached servers.
virtual void Get(const GoogleString &key, Callback *callback)
As mentioned above, Get and MultiGet are blocking in this implementation.
virtual bool IsHealthy() const