Package wtf :: Package ext :: Module memcache :: Class Memcache
[hide private]
[frames] | no frames]

Class Memcache

source code

object --+
         |
        Memcache

Memcache cluster proxy
Instance Methods [hide private]
 
__init__(self, pools, prepare=None, grace_time=None, retry_time=None, compress_threshold=None, padded=None, split=None, prefix=None, largest_slab=None)
Initialization
source code
bool
delete(self, key, block_time=None, all_pools=False)
Delete a key/value pair from the cache
source code
dict
get(self, *keys)
Get a list of key/value pairs from the cache (if applicable)
source code
bool
set(self, key, value, max_age)
Set a key/value pair unconditionally
source code
bool
add(self, key, value, max_age)
Set a key/value pair if the key does not exist yet
source code
bool
replace(self, key, value, max_age)
Set a key/value pair only if the key does exist already
source code
bool
store(self, method, key, value, max_age, compress=True)
Store the value under the given key expiring now + expiry
source code
str
_error(self, line)
Convert a response line into an error or pass it through
source code
tuple
_encode_value(self, key, value, max_age, compress)
Encode a value for the memcache
source code
any
_decode_value(self, flags, value)
Decode a value depending on its flags
source code
MemcacheConnection
_get_conn(self, key, *keys)
Retrieve memcache connection
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
int DEFAULT_GRACE_TIME = 30
Default grace time in seconds.
int DEFAULT_RETRY_TIME = 60
Default retry time in seconds.
int DEFAULT_COMPRESS_THRESHOLD = 128
Default minimum size for compressing values
bool DEFAULT_PADDED = True
Default padding behavior
bool DEFAULT_SPLIT = True
Default splitting behaviour
int DEFAULT_LARGEST_SLAB = 1048576
Default maximum slab size
dict _TYPEMAP = {0: (None, <function <lambda> at 0x7fafe1a011b8>, <...
typemap
Instance Variables [hide private]
int _compress_threshold
Minimal size for compression
int _grace_time
Grace time for this instance
int _largest_slab
Largest SLAB size
bool _padded
Pad small values (<16 byte)?
tuple _pools
List of available pools
str _prefix
Key prefix to use
int _retry_time
Retry time for this instance
bool _split
Allow large value splitting?
tuple _weighted
Weighted list of available pools
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, pools, prepare=None, grace_time=None, retry_time=None, compress_threshold=None, padded=None, split=None, prefix=None, largest_slab=None)
(Constructor)

source code 

Initialization

grace_time and retry_time describe the behaviour of the dispatcher in case one or more dead pools. The algorithm works as follows:

If a server is detected to be unreachable, it is marked dead. Now the grace counter starts to run. Now if the server stays dead until grace_time is reached requests for the server (read and write) are discarded. This gives short memcache outages (like restarts) a chance to recover without adding load to the other caches of the cluster. However, when the grace time threshold is reached, the server is considered completely dead and the requests are dispatched to the other ones. The now completely-declared-dead server will be retried every retry_time seconds from now on until it's vivified again.

Parameters:
  • pools (iterable) - List of memcache connection pools ([MemcacheConnectionPool, ...])
  • prepare (callable) - Key preparation function
  • grace_time (int) - Grace time in seconds
  • retry_time (int) - Retry time in seconds
  • compress_threshold (int) - Minimum size for compression. If omitted or None, DEFAULT_COMPRESS_THRESHOLD is applied.
  • padded (bool) - Pad small values (< 16 byte)? If omitted or None, DEFAULT_PADDED is applied
  • split (bool) - Split large values? If omitted or None, DEFAULT_SPLIT is applied
  • prefix (str) - Prefix for keys. Empty by default
  • largest_slab (int) - Largest SLAB item size of the server, if omitted or None, DEFAULT_LARGEST_SLAB is applied.
Overrides: object.__init__

delete(self, key, block_time=None, all_pools=False)

source code 
Delete a key/value pair from the cache
Parameters:
  • key (str) - The key to identify the item to delete
  • block_time (int) - Time to block add and replace requests for this key in seconds. If omitted or None, the blocking time is 0.
  • all_pools (bool) - Issue delete to each pool? This may be useful to enforce the deletion on backup pools, too. However, it won't delete the key from currently dead pools. So, it might be not that useful after all, but it's the best we can do from this side of the ocean.
Returns: bool
Whether it was really deleted from the main pool (or the current backup pool) of this key (i.e. whether it existed before)

get(self, *keys)

source code 

Get a list of key/value pairs from the cache (if applicable)

The returned dict contains all pairs it could get. But keys maybe missing or the dict might be completely empty (of course).

Parameters:
  • keys (tuple) - The keys to fetch
Returns: dict
The dict of key/value pairs

set(self, key, value, max_age)

source code 
Set a key/value pair unconditionally
Parameters:
  • key (str) - The key to store under
  • value (any) - The value to store (should be picklable)
  • max_age (int) - Maximum age in seconds
Returns: bool
Stored successfully?

add(self, key, value, max_age)

source code 
Set a key/value pair if the key does not exist yet
Parameters:
  • key (str) - The key to store under
  • value (any) - The value to store (should be picklable)
  • max_age (int) - Maximum age in seconds
Returns: bool
Stored successfully?

replace(self, key, value, max_age)

source code 
Set a key/value pair only if the key does exist already
Parameters:
  • key (str) - The key to store under
  • value (any) - The value to store (should be picklable)
  • max_age (int) - Maximum age in seconds
Returns: bool
Stored successfully?

store(self, method, key, value, max_age, compress=True)

source code 
Store the value under the given key expiring now + expiry
Parameters:
  • method (str) - Actual method to call (set, add or replace)
  • key (str) - The key to store under
  • value (any) - The value to store (should be picklable)
  • max_age (int) - Max age of the entry in seconds
  • compress (bool) - Compress the value?
Returns: bool
Stored successfully?

_error(self, line)

source code 
Convert a response line into an error or pass it through
Parameters:
  • line (str) - The response line to inspect
Returns: str
The stripped response line
Raises:

_encode_value(self, key, value, max_age, compress)

source code 
Encode a value for the memcache
Parameters:
  • key (str) - The store key
  • value (any) - The value to encode
  • max_age (int) - Maxc age of this item
  • compress (bool) - Allow value compression?
Returns: tuple
The flags and the encoded value ((int, str))

_decode_value(self, flags, value)

source code 
Decode a value depending on its flags
Parameters:
  • flags (int) - Flag bit field
  • value (str) - Value to decode
Returns: any
The decoded value
Raises:
  • ValueError - Bad flags or bad value

_get_conn(self, key, *keys)

source code 

Retrieve memcache connection

The actual memcache connection is selected by the key. The algorithm is a simple hashfunc(key) % weighted_selectable_pools

Parameters:
  • key (str) - The key to use for selection
Returns: MemcacheConnection
The connection or None

Class Variable Details [hide private]

DEFAULT_GRACE_TIME

Default grace time in seconds. See __init__ for further details.
Type:
int
Value:
30

DEFAULT_RETRY_TIME

Default retry time in seconds. See __init__ for details.
Type:
int
Value:
60

_TYPEMAP

typemap
Type:
dict
Value:
{0: (None,
     <function <lambda> at 0x7fafe1a011b8>,
     <built-in function loads>),
 1: (<type 'unicode'>,
     <function <lambda> at 0x7fafe1a01230>,
     <function <lambda> at 0x7fafe1a012a8>),
 2: (<type 'str'>, <type 'str'>, <type 'str'>),
 3: (<type 'bool'>, <function <lambda> at 0x7fafe1a01320>, <type 'bool\
...