Using dataflake.cache

dataflake.cache provides several cache implementations with a shared simplified API.

Using a SimpleCache object:

>>> from dataflake.cache.simple import SimpleCache
>>> cache = SimpleCache()
>>> cache.set('key1', 'value1')
>>> cache.get('key1')
'value1'
>>> cache.invalidate('key1')
>>> cache.get('key1', default='not available')
'not available'

To attach a specific lifetime to cached items, a cache implementation with built-in timeout is provided as well:

>>> import time
>>> from dataflake.cache.timeout import TimeoutCache
>>> cache = TimeoutCache()
>>> cache.setTimeout(1)
>>> cache.set('key1', 'value1')
>>> cache.get('key1')
'value1'
>>> time.sleep(1)
>>> cache.get('key1', default='not available')
'not available'

Both the simple and timeout caches are available as thread-safe implementations using locks, see the Modules documentation.

The Interfaces page contains more information about the cache APIs.