Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
net_instaweb::AprMemCache Class Reference

#include "apr_mem_cache.h"

Inheritance diagram for net_instaweb::AprMemCache:

Public Member Functions

 AprMemCache (const ExternalClusterSpec &cluster, int thread_limit, Hasher *hasher, Statistics *statistics, Timer *timer, MessageHandler *handler)
const ExternalClusterSpeccluster_spec () const
virtual void Get (const GoogleString &key, Callback *callback)
 As mentioned above, Get and MultiGet are blocking in this implementation.
virtual void Put (const GoogleString &key, const SharedString &value)
virtual void Delete (const GoogleString &key)
virtual void MultiGet (MultiGetRequest *request)
bool Connect ()
bool valid_server_spec () const
bool GetStatus (GoogleString *status_string)
virtual GoogleString Name () const
virtual bool IsBlocking () const
void RecordError ()
virtual bool IsHealthy () const
virtual void ShutDown ()
 Close down the connection to the memcached servers.
virtual bool MustEncodeKeyInValueOnPut () const
virtual void PutWithKeyInValue (const GoogleString &key, const SharedString &key_and_value)
void set_timeout_us (int timeout_us)
- Public Member Functions inherited from net_instaweb::CacheInterface
void PutSwappingString (const GoogleString &key, GoogleString *value)
virtual CacheInterfaceBackend ()

Static Public Member Functions

static void InitStats (Statistics *statistics)
static GoogleString FormatName ()
- Static Public Member Functions inherited from net_instaweb::CacheInterface
static const char * KeyStateName (KeyState state)

Static Public Attributes

static const size_t kValueSizeThreshold = 1 * 1000 * 1000
static const int64 kHealthCheckpointIntervalMs = 30 * Timer::kSecondMs
 Amount of time after a burst of errors to retry memcached operations.
static const int64 kMaxErrorBurst = 4

Additional Inherited Members

- Public Types inherited from net_instaweb::CacheInterface
enum  KeyState {
  kAvailable = 0, kNotFound = 1, kOverload = 2, kNetworkError = 3,
  kTimeout = 4
typedef std::vector< KeyCallbackMultiGetRequest
- Protected Member Functions inherited from net_instaweb::CacheInterface
void ValidateAndReportResult (const GoogleString &key, KeyState state, Callback *callback)
 Invokes callback->ValidateCandidate() and callback->Done() as appropriate.
void ReportMultiGetNotFound (MultiGetRequest *request)

Detailed Description

Interface to memcached via the apr_memcache2*, as documented in

While this class derives from CacheInterface, it is a blocking implementation, suitable for instantiating underneath an AsyncCache.

Constructor & Destructor Documentation

net_instaweb::AprMemCache::AprMemCache ( const ExternalClusterSpec cluster,
int  thread_limit,
Hasher hasher,
Statistics statistics,
Timer timer,
MessageHandler handler 

thread_limit is used to provide apr_memcache2_server_create with a hard maximum number of client connections to open.

Member Function Documentation

bool net_instaweb::AprMemCache::Connect ( )

Connects to the server, returning whether the connection was successful or not.

bool net_instaweb::AprMemCache::GetStatus ( GoogleString status_string)

Get detailed status in a string, returning false if the server failed to return status.

virtual bool net_instaweb::AprMemCache::IsBlocking ( ) const

Returns true if this cache is guaranteed to call its callbacks before returning from Get and MultiGet.

Implements net_instaweb::CacheInterface.

virtual bool net_instaweb::AprMemCache::IsHealthy ( ) const

Determines whether memcached is healthy enough to attempt another operation. Note that even though there may be multiple shards, some of which are healthy and some not, we don't currently track errors on a per-shard basis, so we effectively declare all the memcached instances unhealthy if any of them are.

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::AprMemCache::MultiGet ( MultiGetRequest *  request)

Gets multiple keys, calling multiple callbacks. Default implementation simply loops over all the keys and calls Get.

MultiGetRequest, declared above, is a vector of structs of keys and callbacks.

Ownership of the request is transferred to this function.

Reimplemented from net_instaweb::CacheInterface.

virtual bool net_instaweb::AprMemCache::MustEncodeKeyInValueOnPut ( ) const

To deal with underlying cache systems (e.g. memcached) that cannot tolerate arbitrary-sized keys, we use a hash of the key and put the key in the value, using the functions in key_value_codec.h.

To do this without pointlessly copying the value bytes, we use SharedString::Append(). However, that's not thread-safe. So when making a cache Asynchronous with AsyncCache, we must do the SharedString::Append call in the thread that initiates the Put, before queuing a threaded Put.

This method indicates whether a cache implementation requires encoding the keys in the value using key_value_codec.

Reimplemented from net_instaweb::CacheInterface.

virtual GoogleString net_instaweb::AprMemCache::Name ( ) const

The name of this CacheInterface – used for logging and debugging.

It is strongly recommended that you provide a static GoogleString FormatName(...) method for use in formatting the Name() return, and in testing, e.g. in third_party/pagespeed/system/

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::AprMemCache::Put ( const GoogleString key,
const SharedString value 

Puts a value into the cache. The value that is passed in is not modified, but the SharedString is passed by non-const pointer because its reference count is bumped.

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::AprMemCache::PutWithKeyInValue ( const GoogleString key,
const SharedString key_and_value 

Performs a cache Put, but assumes the key has already been encoded into the value with key_value_codec. It is only valid to call this when MustEncodeKeyInValueOnPut() returns true.

Reimplemented from net_instaweb::CacheInterface.

void net_instaweb::AprMemCache::RecordError ( )

Records in statistics that a system error occurred, helping it detect when it's unhealthy if they are too frequent.

void net_instaweb::AprMemCache::set_timeout_us ( int  timeout_us)

Sets the I/O timeout in microseconds. This should be called at setup time and not while there are operations in flight.

Member Data Documentation

const int64 net_instaweb::AprMemCache::kMaxErrorBurst = 4

Maximum number of errors tolerated within kHealthCheckpointIntervalMs, after which AprMemCache will declare itself unhealthy for kHealthCheckpointIntervalMs.

const size_t net_instaweb::AprMemCache::kValueSizeThreshold = 1 * 1000 * 1000

Experimentally it seems large values larger than 1M bytes result in a failure, e.g. from load-tests: [Fri Jul 20 10:29:34 2012] [error] [mod_pagespeed @1522] AprMemCache::Put error: Internal error on key, value-size 1393146 External to this class, we use a fallback cache (in Apache a FileCache) to handle too-large requests. This is managed by class FallbackCache in ../util.

The documentation for this class was generated from the following file: