Page Speed Optimization Libraries
1.2.24.1
|
#include "apr_mem_cache.h"
Public Member Functions | |
AprMemCache (const StringPiece &servers, int thread_limit, Hasher *hasher, Statistics *statistics, Timer *timer, MessageHandler *handler) | |
const GoogleString & | server_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, 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 const char * | Name () const |
The name of this CacheInterface -- used for logging and debugging. | |
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, SharedString *key_and_value) |
void | set_timeout_us (int timeout_us) |
Static Public Member Functions | |
static void | InitStats (Statistics *statistics) |
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 |
Interface to memcached via the apr_memcache2*, as documented in http://apr.apache.org/docs/apr-util/1.4/group___a_p_r___util___m_c.html.
While this class derives from CacheInterface, it is a blocking implementation, suitable for instantiating underneath an AsyncCache.
net_instaweb::AprMemCache::AprMemCache | ( | const StringPiece & | servers, |
int | thread_limit, | ||
Hasher * | hasher, | ||
Statistics * | statistics, | ||
Timer * | timer, | ||
MessageHandler * | handler | ||
) |
servers is a comma-separated list of host[:port] where port defaults to 11211, the memcached default.
thread_limit is used to provide apr_memcache2_server_create with a hard maximum number of client connections to open.
bool net_instaweb::AprMemCache::Connect | ( | ) |
Connects to the server, returning whether the connnection 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 [inline, virtual] |
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 [virtual] |
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 | ) | [virtual] |
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 [inline, virtual] |
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 void net_instaweb::AprMemCache::Put | ( | const GoogleString & | key, |
SharedString * | value | ||
) | [virtual] |
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, |
SharedString * | key_and_value | ||
) | [virtual] |
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.
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.
const int64 net_instaweb::AprMemCache::kMaxErrorBurst = 4 [static] |
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 [static] |
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 0.10.0.0-1699 @1522] AprMemCache::Put error: Internal error on key http://example.com/image.jpg, 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.