Skip to main content

Cherrys : a Redis backend for CherryPy sessions

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:

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 a few 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)

  • db [0]

  • prefix [“”] (prepended to session id if given; useful when ACLs are enabled)

  • user [None] (for old version of authentication can be set to empty string)

  • password [None]

  • lock_timeout [None] (None, or time in seconds until session lock expires)

  • url [None] (alternative to host/port/ssl/db/user/password combination)

Sentinel-related additional (optional) parameters:

  • is_sentinel [False]

  • sentinel_pass [None]

  • sentinel_service [None]

  • tls_skip_verify [False]

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,
    'tools.sessions.lock_timeout: LOCK_TIME_SECONDS,
}

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.db': REDIS_DB,
    'tools.sessions.prefix': REDIS_PREFIX,
    'tools.sessions.user': REDIS_USER,
    'tools.sessions.password': REDIS_PASS,
    'tools.sessions.lock_timeout: LOCK_TIME_SECONDS,

    'tools.sessions.is_sentinel': True,
    'tools.sessions.sentinel_pass': REDIS_SENTINEL_PASS,
    'tools.sessions.sentinel_service': REDIS_SENTINEL_SERVICENAME,
    'tools.sessions.tls_skip_verify': True,
}

Configuration via redis URL

As a shortcut a URL to the redis server can be provided as well.

import cherrys
config = {
    'tools.sessions.on' : True,
    # next setting removes the need for initializing `cherrypy.lib.sessions.RedisSession' above:
    'tools.sessions.storage_class' : cherrys.RedisSession,
    'tools.sessions.url': 'redis://your-name:your-pwd@redis-server:6379/2'
}

The number at the end of the URL (“2”) denotes the redis database to be used.

Running unittests

Unittests require a running redis-server on localhost:6379 setup without any authentication in place.

Then install pytest into your current virtualenv and start it from your command line:

$ pip install pytest
$ pytest

Metadata

Release files for cherrys 2023.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cherrys 2023.2.0
File Size Uploaded
cherrys-2023.2.0.tar.gz 4.4 kB Details

Release files / cherrys-2023.2.0.tar.gz

Download URL cherrys-2023.2.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7e5787cce55cfc38fbf11d213c4065439133343dd40a34b1820ea37296cdfad7
BLAKE2b-256 checksum
How to use checksums
4c00e640b5347c695a0f29e17d3fdd1feeadd4e29cac14e4bccf3e6faf9b10d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

2023.2.0 This release

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page