watson.cache.storage

class watson.cache.storage.BaseStorage(config=None)[source]

Base class for all cache storage classes.

Cache storage classes are designed to act similar to a dict, however get and set methods can be used when a timeout is required on a set, or when a default value is to be specified on a get.

config

dict – The relevant configuration settings for the storage.

__init__(config=None)[source]
expired(key)[source]

Determine if a key has expired or not.

Parameters:key (string) – The key to find
Returns:True/False depending on expiration
Return type:Boolean
flush()[source]

Clears all items from the cache.

get(key, default=None)[source]

Gets a key from the cache, returns the default if not set.

Parameters:key (string) – The key to be retrieved
Returns:The value stored within the cache

Example:

value = cache['key']
set(key, value, timeout=0)[source]

Sets a key in the cache.

Parameters:
  • key (string) – The key to be used as a reference
  • value (mixed) – The value to store in the key
  • timeout (int) – The amount of time in seconds a key is valid for.

Example:

cache['key'] = 'value'
class watson.cache.storage.File(config=None)[source]

A cache storage mechanism for storing items on the local filesystem.

File cache storage will persist the data to the filesystem in whichever directory has been specified in the configuration options. If no directory is specified then the system temporary folder will be used.

__init__(config=None)[source]

Initializes the cache.

Parameters:config (dict) – The config for the cache

Example:

cache = File({'dir': '/tmp', 'prefix': 'my-cache'})
# all cached items will be saved to /tmp
# and will be prefixed with my-cache
cache['key'] = 'value' # /tmp/my-cache-key contains a serialized 'value'
class watson.cache.storage.Memcached(config=None)[source]

A cache storage mechanism for storing items in memcached.

Memcached cache storage will utilize python3-memcached to maintain the cache across multiple servers. Python3-memcached documentation can be found at http://pypi.python.org/pypi/python3-memcached/

__init__(config=None)[source]

Initializes the cache.

Parameters:config (dict) – The config for the cache

Example:

cache = Memcached({'servers': ['127.0.0.1:11211', '192.168.100.1:11211']})
class watson.cache.storage.Memory[source]

A cache storage mechanism for storing items in memory.

Memory cache storage will maintain the cache while the application is being run. This is usually best used in instances when you don’t want to keep the cached items after the application has finished running.

__init__()[source]
class watson.cache.storage.Redis(config=None)[source]

A cache storage mechanism for storing items in redis.

Redis cache storage will utilize redis to maintain the cache across multiple servers. redis documentation can be found at https://github.com/andymccurdy/redis-py

__init__(config=None)[source]

Initializes the cache.

Parameters:config (dict) – The config for the cache

Example:

cache = Redis