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

#include "lru_cache.h"

Inheritance diagram for net_instaweb::LRUCache:
net_instaweb::CacheInterface

Public Member Functions

 LRUCache (size_t max_size)
 
virtual void Get (const GoogleString &key, Callback *callback)
 
virtual void Put (const GoogleString &key, const SharedString &new_value)
 
virtual void Delete (const GoogleString &key)
 
void DeleteWithPrefixForTesting (StringPiece prefix)
 
size_t size_bytes () const
 Total size in bytes of keys and values stored.
 
size_t max_bytes_in_cache () const
 Maximum capacity.
 
size_t num_elements () const
 Number of elements stored.
 
size_t num_evictions () const
 
size_t num_hits () const
 
size_t num_misses () const
 
size_t num_inserts () const
 
size_t num_identical_reinserts () const
 
size_t num_deletes () const
 
void SanityCheck ()
 Sanity check the cache data structures.
 
void Clear ()
 
void ClearStats ()
 Clear the stats – note that this will not clear the content.
 
virtual GoogleString Name () const
 
virtual bool IsBlocking () const
 
virtual bool IsHealthy () const
 
virtual void ShutDown ()
 
void set_is_healthy (bool x)
 
- Public Member Functions inherited from net_instaweb::CacheInterface
virtual void MultiGet (MultiGetRequest *request)
 
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 ()
 
- Static Public Member Functions inherited from net_instaweb::CacheInterface
static const char * KeyStateName (KeyState state)
 

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

Simple C++ implementation of an in-memory least-recently used (LRU) cache. This implementation is not thread-safe, and must be combined with a mutex to make it so.

The purpose of this implementation is as a default implementation, or an local shadow for memcached.

Also of note: the Get interface allows for streaming. To get into a GoogleString, use a StringWriter.

Todo:
TODO(jmarantz): The Put interface does not currently stream, but this should be added.

Member Function Documentation

void net_instaweb::LRUCache::Clear ( )
inline

Clear the entire cache. Used primarily for testing. Note that this will not clear the stats, however it will update current_bytes_in_cache_.

void net_instaweb::LRUCache::DeleteWithPrefixForTesting ( StringPiece  prefix)

Deletes all objects whose key starts with prefix. Not part of cache interface. Exported for testing only.

virtual void net_instaweb::LRUCache::Get ( const GoogleString key,
Callback callback 
)
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.

Implements net_instaweb::CacheInterface.

virtual bool net_instaweb::LRUCache::IsBlocking ( ) const
inlinevirtual

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::LRUCache::IsHealthy ( ) const
inlinevirtual

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 GoogleString net_instaweb::LRUCache::Name ( ) const
inlinevirtual

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/system_caches_test.cc.

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::LRUCache::Put ( const GoogleString key,
const SharedString new_value 
)
virtual

Puts an object into the cache, sharing the bytes.

Todo:
TODO(jmarantz): currently if the caller mutates the SharedString after having called Put, it will actually modify the value in the cache. We should change SharedString to Copy-On-Write semantics.

Implements net_instaweb::CacheInterface.

virtual void net_instaweb::LRUCache::ShutDown ( )
inlinevirtual

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.

Implements net_instaweb::CacheInterface.


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