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

#include "redis_cache.h"

Inheritance diagram for net_instaweb::RedisCache:

Public Member Functions

 RedisCache (StringPiece host, int port, ThreadSystem *thread_system, MessageHandler *message_handler, Timer *timer, int64 reconnection_delay_ms, int64 timeout_us, Statistics *stats, int database_index)
GoogleString ServerDescription () const
void StartUp (bool connect_now=true)
GoogleString Name () const override
 CacheInterface implementations.
bool IsBlocking () const override
bool IsHealthy () const override
void ShutDown () override
void Get (const GoogleString &key, Callback *callback) override
 CacheInterface implementations.
void Put (const GoogleString &key, const SharedString &value) override
void Delete (const GoogleString &key) override
void GetStatus (GoogleString *status_string)
int64 Redirections ()
int64 ClusterSlotsFetches ()
- 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 void InitStats (Statistics *stats)
static GoogleString FormatName ()
static int HashSlot (StringPiece key)
 Redis spec defined hasher for keys. Static, since it's a pure function.
- Static Public Member Functions inherited from net_instaweb::CacheInterface
static const char * KeyStateName (KeyState state)


class RedisCacheTest

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 Redis using hiredis library. This implementation is blocking and thread-safe. One should call StartUp() before making any requests and ShutDown() after finishing work. Connecting will be initiated on StartUp() call. If it fails there or is dropped after any request, the following reconnection strategy is used:

  1. If an operation fails because of communication or protocol error, try reconnecting on the next Get/Put/Delete (without delay).
  2. If (re-)connection attempt is unsuccessfull, try again on next Get/Put/Delete operation, but not until at least reconnection_delay_ms_ have passed from the previous attempt. That ensures that we do not try to connect to unreachable server a lot, but still allows us to reconnect quickly in case of network glitches.

See for details on different locks that the class has.

When using clustering, to be efficient we need to know what redis cluster machine to talk to for a given key. There are two steps: 1) hash the key in a redis-specific way determine the hash slot. 2) consult our cached copy of the slot-to-server mapping to see which server to send to. If we get a redirection after following that approach, our cached mapping is invalid and should be refreshed. explains this all.


TODO(yeputons): consider extracting a common interface with AprMemCache.

TODO(yeputons): consider making Redis-reported errors treated as failures.

TODO(yeputons): add redis AUTH command support.

Constructor & Destructor Documentation

net_instaweb::RedisCache::RedisCache ( StringPiece  host,
int  port,
ThreadSystem thread_system,
MessageHandler message_handler,
Timer timer,
int64  reconnection_delay_ms,
int64  timeout_us,
Statistics stats,
int  database_index 

Uses ThreadSystem to generate several mutexes in constructor only. Does not take ownership of MessageHandler, Timer, and assumes that these pointers are valid throughout full lifetime of RedisCache.

Member Function Documentation

int64 net_instaweb::RedisCache::ClusterSlotsFetches ( )

Total number of times we looked up the cluster slots mappings to avoid redirections.

void net_instaweb::RedisCache::GetStatus ( GoogleString status_string)

Appends detailed status for each server to a string. If a server fails to report a status, then for that server we append an error message instead.

bool net_instaweb::RedisCache::IsBlocking ( ) const

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

Implements net_instaweb::CacheInterface.

bool net_instaweb::RedisCache::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.

void net_instaweb::RedisCache::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.

int64 net_instaweb::RedisCache::Redirections ( )

Total number of times we hit one server and were redirected to a different one.

void net_instaweb::RedisCache::ShutDown ( )

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: