Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
net_instaweb::CacheInterface Class Referenceabstract

Abstract interface for a cache. More...

#include "cache_interface.h"

Inheritance diagram for net_instaweb::CacheInterface:
net_instaweb::AprMemCache net_instaweb::AsyncCache net_instaweb::CacheBatcher net_instaweb::CacheKeyPrepender net_instaweb::CacheStats net_instaweb::CompressedCache net_instaweb::DelayCache net_instaweb::FallbackCache net_instaweb::FileCache net_instaweb::InMemoryCache net_instaweb::LRUCache net_instaweb::MockTimeCache net_instaweb::RedisCache net_instaweb::SharedMemCache< kBlockSize > net_instaweb::ThreadsafeCache net_instaweb::WriteThroughCache


class  Callback
struct  KeyCallback
 Vector of structures used to initiate a MultiGet. More...
class  SynchronousCallback

Public Types

enum  KeyState {
  kAvailable = 0, kNotFound = 1, kOverload = 2, kNetworkError = 3,
  kTimeout = 4
typedef std::vector< KeyCallbackMultiGetRequest

Public Member Functions

virtual void Get (const GoogleString &key, Callback *callback)=0
virtual void MultiGet (MultiGetRequest *request)
virtual void Put (const GoogleString &key, const SharedString &value)=0
virtual void Delete (const GoogleString &key)=0
void PutSwappingString (const GoogleString &key, GoogleString *value)
virtual GoogleString Name () const =0
virtual CacheInterfaceBackend ()
virtual bool IsBlocking () const =0
virtual bool IsHealthy () const =0
virtual void ShutDown ()=0
virtual bool MustEncodeKeyInValueOnPut () const
virtual void PutWithKeyInValue (const GoogleString &key, const SharedString &key_and_value)

Static Public Member Functions

static const char * KeyStateName (KeyState state)

Protected Member Functions

void ValidateAndReportResult (const GoogleString &key, KeyState state, Callback *callback)
 Invokes callback->ValidateCandidate() and callback->Done() as appropriate.
void ReportMultiGetNotFound (MultiGetRequest *request)

Detailed Description

Abstract interface for a cache.

Member Enumeration Documentation


Requested key is available for serving.


Requested key needs to be written.


Lookup is discarded due to cache server is overloaded.


Cache lookup ended up in network error.


Request timeout.

Member Function Documentation

virtual CacheInterface* net_instaweb::CacheInterface::Backend ( )

If this cache is merely a wrapper around a backend that actually does all the work, returns that backend cache object. Otherwise just returns 'this'. Used for testing.

Reimplemented in net_instaweb::CacheStats, net_instaweb::ThreadsafeCache, net_instaweb::CompressedCache, and net_instaweb::CacheKeyPrepender.

virtual void net_instaweb::CacheInterface::Get ( const GoogleString key,
Callback callback 
pure virtual

Initiates a cache fetch, calling callback->ValidateCandidate() and then callback->Done(state) when done.

Note: implementations should normally invoke the callback via ValidateAndReportResult, which will combine ValidateCandidate() and Done() together properly.

Implemented in net_instaweb::SharedMemCache< kBlockSize >, net_instaweb::RedisCache, net_instaweb::CacheBatcher, net_instaweb::AprMemCache, net_instaweb::FileCache, net_instaweb::AsyncCache, net_instaweb::DelayCache, net_instaweb::CacheStats, net_instaweb::FallbackCache, net_instaweb::ThreadsafeCache, net_instaweb::LRUCache, net_instaweb::MockTimeCache, net_instaweb::InMemoryCache, net_instaweb::WriteThroughCache, net_instaweb::CompressedCache, and net_instaweb::CacheKeyPrepender.

virtual bool net_instaweb::CacheInterface::IsBlocking ( ) const
pure virtual
virtual bool net_instaweb::CacheInterface::IsHealthy ( ) const
pure virtual

IsHealthy() is a rough estimation of whether cache is available for any operations. If it's false, caller may reasonably expect that making calls right now is useless as they will fail or have high latency. If it's true, operations should succeed, but some still may fail occasionally. The primary goal is to avoid sending commands to 'unhealthy' caches, e.g. if cache is under heavy load, we do not want to send even more requests.

Memory and file-based caches can simply return 'true'. It should be safe to call this frequently – the implementation shouldn't do much more that check a bool flag under mutex.

Implemented in net_instaweb::SharedMemCache< kBlockSize >, net_instaweb::AprMemCache, net_instaweb::CacheBatcher, net_instaweb::LRUCache, net_instaweb::RedisCache, net_instaweb::AsyncCache, net_instaweb::DelayCache, net_instaweb::FileCache, net_instaweb::WriteThroughCache, net_instaweb::MockTimeCache, net_instaweb::FallbackCache, net_instaweb::CacheStats, net_instaweb::ThreadsafeCache, net_instaweb::InMemoryCache, net_instaweb::CompressedCache, and net_instaweb::CacheKeyPrepender.

virtual void net_instaweb::CacheInterface::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 in net_instaweb::AprMemCache, net_instaweb::AsyncCache, net_instaweb::DelayCache, net_instaweb::FallbackCache, net_instaweb::CacheStats, and net_instaweb::CacheKeyPrepender.

virtual bool net_instaweb::CacheInterface::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 in net_instaweb::AprMemCache.

virtual GoogleString net_instaweb::CacheInterface::Name ( ) const
pure virtual

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/

Implemented in net_instaweb::SharedMemCache< kBlockSize >, net_instaweb::CacheBatcher, net_instaweb::AprMemCache, net_instaweb::LRUCache, net_instaweb::RedisCache, net_instaweb::DelayCache, net_instaweb::FileCache, net_instaweb::WriteThroughCache, net_instaweb::FallbackCache, net_instaweb::AsyncCache, net_instaweb::CacheStats, net_instaweb::MockTimeCache, net_instaweb::ThreadsafeCache, net_instaweb::CacheKeyPrepender, net_instaweb::InMemoryCache, and net_instaweb::CompressedCache.

virtual void net_instaweb::CacheInterface::Put ( const GoogleString key,
const SharedString value 
pure virtual
void net_instaweb::CacheInterface::PutSwappingString ( const GoogleString key,
GoogleString value 

Convenience method to do a Put from a GoogleString* value. The bytes will be swapped out of the value and into a temp SharedString.

virtual void net_instaweb::CacheInterface::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 in net_instaweb::AprMemCache.

void net_instaweb::CacheInterface::ReportMultiGetNotFound ( MultiGetRequest *  request)

Helper method to report a NotFound on each MultiGet key. Deletes the request.

virtual void net_instaweb::CacheInterface::ShutDown ( )
pure virtual

Stops all cache activity. Further Put/Delete calls will be dropped, and MultiGet/Get will call the callback with kNotFound immediately. Note there is no Enable(); once the cache is stopped it is stopped forever. This function is intended for use during process shutdown.

Implemented in net_instaweb::SharedMemCache< kBlockSize >, net_instaweb::AprMemCache, net_instaweb::CacheBatcher, net_instaweb::LRUCache, net_instaweb::RedisCache, net_instaweb::AsyncCache, net_instaweb::DelayCache, net_instaweb::FileCache, net_instaweb::WriteThroughCache, net_instaweb::FallbackCache, net_instaweb::MockTimeCache, net_instaweb::CacheStats, net_instaweb::ThreadsafeCache, net_instaweb::InMemoryCache, net_instaweb::CompressedCache, and net_instaweb::CacheKeyPrepender.

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