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 dataflake.cache.simple and dataflake.cache.timeout documentation.

The Interfaces page contains more information about the cache APIs.

Table Of Contents

Previous topic

Next topic

Related links

This Page


Download as PDF