Redis backend for CherryPy sessions
Project description
CherryPy kicks some serious arse in the ‘I am just a HTTP framework’ category! As of version 3.2 it supports 4 types of storage for sessions by default:
Ram
File
Redis is growing in popularity as an alternative to Memcached (and is fully supported on dotCloud!)
Installation
If you are not using pip yet. Install it and while you are at it consider using virtualenv too.
$ pip install cherrys
redis-py and CherryPy are required dependencies hence will be installed if necessary.
We recommend installing hiredis-py (a faster parser) as well.
$ pip install hiredis
Usage (and abusage)
To tell CherryPy which backend to use, we need to specify the storage_type for the sessions tool. You may want to read more about CherryPy configuration.
tools.sessions.storage_type : 'redis'
But CherryPy doesn’t know Redis. Hence the first thing we need to do is add the cherrys RedisSession class to cherrypy.lib.sessions.
import cherrys cherrypy.lib.sessions.RedisSession = cherrys.RedisSession
It is that simple!
Config dictionary
There are 4 optional parameters you can set:
host [127.0.0.1] (when is_sentinel this is host for sentinel service)
port [6379] (when is_sentinel this is port for sentinel service)
ssl [False] (for both sentinel and redis)
tls_skip_verify [False] (for both sentinel and redis)
is_sentinel [False]
sentinel_pass [None]
sentinel_service [None]
db [0]
prefix [‘cherrys_’] (prepended to session if, useful when ACLs are enabled)
user [None] (for old version of authentication can be set to empty string)
password [None]
A full config dictionary to activate Redis backed sessions would look like this.
config = { 'tools.sessions.on' : True, 'tools.sessions.storage_type' : 'redis', 'tools.sessions.host': REDIS_HOST, 'tools.sessions.port': REDIS_PORT, 'tools.sessions.db': REDIS_DB, 'tools.sessions.prefix': REDIS_PREFIX, 'tools.sessions.user': REDIS_USER, 'tools.sessions.password': REDIS_PASS, }
A full config dictionary to activate RedisSentinelSSL backed sessions would look like this.
config = { 'tools.sessions.on' : True, 'tools.sessions.storage_type' : 'redis', 'tools.sessions.host': REDIS_HOST, 'tools.sessions.port': REDIS_PORT, 'tools.sessions.ssl': True, 'tools.sessions.tls_skip_verify': True, 'tools.sessions.is_sentinel': True, 'tools.sessions.sentinel_pass': REDIS_SENTINEL_PASS, 'tools.sessions.sentinel_service': REDIS_SENTINEL_SERVICENAME, 'tools.sessions.db': REDIS_DB, 'tools.sessions.prefix': REDIS_PREFIX, 'tools.sessions.user': REDIS_USER, 'tools.sessions.password': REDIS_PASS, }
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file cherrys-0.5.tar.gz
.
File metadata
- Download URL: cherrys-0.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1921593b619d38ffd86015d4d0efd06c822c3a4bc3c3cb206d9460b96f1c7598 |
|
MD5 | 2d0a160274a5909ac5d4db42d9e54c65 |
|
BLAKE2b-256 | 1313e4ef4a66ad5c3c19bfac416946785e31d13923be588b0cd2516cf86f1dcc |