Page Speed Optimization Libraries  1.13.35.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 PAGESPEED_SYSTEM_APR_MEM_CACHE_H_
20 #define PAGESPEED_SYSTEM_APR_MEM_CACHE_H_
21 
22 #include <cstddef>
23 #include <vector>
24 
30 #include "pagespeed/kernel/cache/cache_interface.h"
32 
33 struct apr_memcache2_t;
34 struct apr_memcache2_server_t;
35 struct apr_pool_t;
36 
37 namespace net_instaweb {
38 
39 class Hasher;
40 class MessageHandler;
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 
72  AprMemCache(const ExternalClusterSpec& cluster, int thread_limit,
73  Hasher* hasher, Statistics* statistics, Timer* timer,
74  MessageHandler* handler);
75  ~AprMemCache();
76 
77  static void InitStats(Statistics* statistics);
78 
79  const ExternalClusterSpec& cluster_spec() const { return cluster_spec_; }
80 
82  virtual void Get(const GoogleString& key, Callback* callback);
83  virtual void Put(const GoogleString& key, const SharedString& value);
84  virtual void Delete(const GoogleString& key);
85  virtual void MultiGet(MultiGetRequest* request);
86 
89  bool Connect();
90 
91  bool valid_server_spec() const { return valid_server_spec_; }
92 
95  bool GetStatus(GoogleString* status_string);
96 
97  static GoogleString FormatName() { return "AprMemCache"; }
98  virtual GoogleString Name() const { return FormatName(); }
99 
100  virtual bool IsBlocking() const { return true; }
101 
104  void RecordError();
105 
111  virtual bool IsHealthy() const;
112 
114  virtual void ShutDown();
115 
116  virtual bool MustEncodeKeyInValueOnPut() const { return true; }
117  virtual void PutWithKeyInValue(const GoogleString& key,
118  const SharedString& key_and_value);
119 
122  void set_timeout_us(int timeout_us);
123 
124  private:
125  void DecodeValueMatchingKeyAndCallCallback(
126  const GoogleString& key, const char* data, size_t data_len,
127  const char* calling_method, Callback* callback);
128 
132  void PutHelper(const GoogleString& key, const SharedString& key_and_value);
133 
134  ExternalClusterSpec cluster_spec_;
135  bool valid_server_spec_;
136  int thread_limit_;
137  int timeout_us_;
138  apr_pool_t* pool_;
139  apr_memcache2_t* memcached_;
140  std::vector<apr_memcache2_server_t*> servers_;
141  Hasher* hasher_;
142  Timer* timer_;
143  AtomicBool shutdown_;
144 
145  Variable* timeouts_;
146  UpDownCounter* last_error_checkpoint_ms_;
147  UpDownCounter* error_burst_size_;
148 
149  MessageHandler* message_handler_;
150 
162  Variable* last_apr_error_;
163 
164 
165 };
166 
167 }
168 
169 #endif
Definition: atomic_bool.h:31
Definition: apr_mem_cache.h:50
Abstract interface for a cache.
Definition: cache_interface.h:32
Definition: statistics.h:43
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
virtual bool IsBlocking() const
Definition: apr_mem_cache.h:100
static const int64 kMaxErrorBurst
Definition: apr_mem_cache.h:68
static const int64 kSecondMs
Definition: timer.h:34
virtual void PutWithKeyInValue(const GoogleString &key, const SharedString &key_and_value)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void MultiGet(MultiGetRequest *request)
static const int64 kHealthCheckpointIntervalMs
Amount of time after a burst of errors to retry memcached operations.
Definition: apr_mem_cache.h:63
virtual void Put(const GoogleString &key, const SharedString &value)
Definition: shared_string.h:40
virtual GoogleString Name() const
Definition: apr_mem_cache.h:98
virtual bool MustEncodeKeyInValueOnPut() const
Definition: apr_mem_cache.h:116
static const size_t kValueSizeThreshold
Definition: apr_mem_cache.h:60
bool GetStatus(GoogleString *status_string)
Definition: statistics.h:73
Definition: message_handler.h:39
Definition: external_server_spec.h:44
void set_timeout_us(int timeout_us)
AprMemCache(const ExternalClusterSpec &cluster, int thread_limit, Hasher *hasher, Statistics *statistics, Timer *timer, MessageHandler *handler)
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
virtual void ShutDown()
Close down the connection to the memcached servers.
Definition: hasher.h:30
virtual void Get(const GoogleString &key, Callback *callback)
As mentioned above, Get and MultiGet are blocking in this implementation.
virtual bool IsHealthy() const