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::AsyncCache Class Reference

#include "async_cache.h"

Inheritance diagram for net_instaweb::AsyncCache:

Public Member Functions

 AsyncCache (CacheInterface *cache, QueuedWorkerPool *pool)
virtual void Get (const GoogleString &key, Callback *callback)
virtual void Put (const GoogleString &key, const SharedString &value)
virtual void Delete (const GoogleString &key)
virtual void MultiGet (MultiGetRequest *request)
virtual GoogleString Name () const
virtual bool IsBlocking () const
virtual void ShutDown ()
void CancelPendingOperations ()
virtual bool IsHealthy () const
int32 outstanding_operations ()
- Public Member Functions inherited from net_instaweb::CacheInterface
void PutSwappingString (const GoogleString &key, GoogleString *value)
virtual CacheInterfaceBackend ()
virtual bool MustEncodeKeyInValueOnPut () const
virtual void PutWithKeyInValue (const GoogleString &key, const SharedString &key_and_value)

Static Public Member Functions

static GoogleString FormatName (StringPiece cache)
- Static Public Member Functions inherited from net_instaweb::CacheInterface
static const char * KeyStateName (KeyState state)

Static Public Attributes

static const int64 kMaxQueueSize = 2000

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

Employs a QueuedWorkerPool to turn a synchronous cache implementation into an asynchronous one. This makes sense to do only if the cache implemention is potentially slow, due to network latency or disk seek time.

This class also serves to serialize access to the passed-in cache, ensuring that it is accessed from only one thread at a time.

Constructor & Destructor Documentation

net_instaweb::AsyncCache::AsyncCache ( CacheInterface cache,
QueuedWorkerPool pool 

Does not takes ownership of the synchronous cache that is passed in. Does not take ownership of the pool, which might be shared with other users.

Note that in the future we may try to add multi-threaded access to the underlying cache (e.g. AprMemCache supports this), so we take the pool as the constructor arg.

Member Function Documentation

void net_instaweb::AsyncCache::CancelPendingOperations ( )

Cancels all pending cache operations. Puts and Deletes are dropped. Gets and MultiGets are retired by calling their callbacks with kNotFound.

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

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.

Implements net_instaweb::CacheInterface.

virtual bool net_instaweb::AsyncCache::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::AsyncCache::IsHealthy ( ) const

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.

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::AsyncCache::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 GoogleString net_instaweb::AsyncCache::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::AsyncCache::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::AsyncCache::ShutDown ( )

Prevent the AsyncCache from issuing any more Gets. Any subsequent Gets will have their callback invoked immediately with kNotFound. Outstanding Gets may be completed depending on timing.

This can be called during the process Shutdown flow to avoid introducing more work asynchronously that will have to be completed prior to Shutdown.

Implements net_instaweb::CacheInterface.

Member Data Documentation

const int64 net_instaweb::AsyncCache::kMaxQueueSize = 2000

The maximum number of operations that can be queued up while a server is slow. When this is reached, old Deletes/Puts get dropped, and old Gets are retired with a kNotFound.

This helps bound the amount of memory consumed by queued operations when the cache gets wedged. Note that when CacheBatcher is layered above AsyncCache, it will queue up its Gets at a level above this one, and ultimately send those using a MultiGet.

TODO(jmarantz): Analyze whether we drop operations under load with a non-wedged cache. If it looks like we are dropping Puts the first time we encounter a page then I think we may need to bump this up.

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