The Redis Extension provides application caching and key/value store support via Redis.
Documentation References:
​Caching​
API References:
​Cement Redis Extension​
​Redis Library​
Redis (pip install redis
)
This extension honors the following config settings under a [cache.redis]
section in any configuration file:
Setting | Description |
expire_time | The default time in seconds to expire items in the cache. Default: |
host | Redis server ip address or hostname |
port | Redis server port |
db | Redis server database id/namespace |
myapp.pyfrom cement import App, init_defaults​CONFIG = init_defaults('myapp', 'cache.redis')CONFIG['cache.redis']['expire_time'] = 300 # secondsCONFIG['cache.redis']['host'] = '127.0.0.1'CONFIG['cache.redis']['port'] = 6379CONFIG['cache.redis']['db'] = 0​class MyApp(App):class Meta:label = 'myapp'config_defaults = CONFIGextensions = ['redis']cache_handler = 'redis'​with MyApp() as app:# Run the appapp.run()​# Set a cached valueapp.cache.set('my_key', 'my value')​# Get a cached valueapp.cache.get('my_key')​# Delete a cached valueapp.cache.delete('my_key')​# Delete the entire cacheapp.cache.purge()
~/.myapp.conf[myapp]​# set the cache handler to usecache_handler = redis​​[cache.redis]​# time in seconds that an item in the cache will expireexpire_time = 300​# Redis serverhost = 127.0.0.1​# Redis portport = 6379​# Redis database numberdb = 0