Skip to main content

Session and cache for Pyramid

Project description

https://github.com/mardiros/pyramid-kvs2/actions/workflows/tests.yml/badge.svg

Some Key Value Store basics for pyramid:

Two KVS are implemented:
  • memcache

  • redis

Here are the provides features:

  • An application cache, shared by every request.

  • A session manager

  • A rate limit per session holder

Every of this components are optional, they exists if they are set in the configuration like below. Component settings are written in json.

Cache

The application cache is a new attribute of the session. request.cache if the settings kvs.cache exists. Here are an example of configuration

kvs.cache = {"kvs": "redis",
             "codec": "pickle",
             "kvs_kwargs": {},
             "ttl": 300,
             "key_prefix": "cache::"}

Every kvs, except the type are optional. The example contains every key with their default values for a redis instance. The kvs_kwargs key is passed to the driver to build the client connection.

Session

The session is accessible via “request.session”, it’s in every pyramid application. This is just an implementation for Key Value Store users.

kvs.session = {"kvs": "redis",
               "key_name": "session_id",
               "session_type": "cookie",
                "ttl": 300,
                "key_prefix": "session::"}

Every kvs, except the type are optional. The example contains every key with their default values for a redis instance.

You can also create a session for an http header like authentication token, it’s help to create a cache per user in an API. API don’t use cookies.

kvs.session = {"kvs": "redis",
               "key_name": "X-Auth-Token",
               "session_type": "header",
                "ttl": 300,
                "key_prefix": "session::"}

Ratelimit

The ratelimit works only if the kvs.session is used! Ratelimit is per session hold and limit number of http queries in a defined period.

kvs.ratelimit = {"window": 1, "limit": 15}

All keys are optional. The example contains every key with their default values for a redis instance.

If the ratelimit is enabled, every response will be decorated with the following http headers:

  • X-RateLimit-Limit: max queries in the period.

  • X-RateLimit-Remaining: current remaining queries in that period.

Usage

Declare the addons in the pyramid.includes in your config, then tweak the settings like above.

# development.ini
[app:main]
pyramid.includes =
    pyramid_kvs2

# declare the application cache
# except type, every keys are optional
# kvs_kwargs for redis is the parameters of the redis.Redis class
# see: http://redis-py.readthedocs.org/en/latest/
# for memcache, parameters of the memcache.Client class
# https://github.com/linsomniac/python-memcached/blob/master/memcache.py#L160
kvs.cache = {"type": "redis"}

# declare the session
kvs.session = {"type": "redis"}

# Authorize a session holder to do 20 http queries max in 2 seconds.
kvs.ratelimit = {"window": 2, "limit": 20}

tests

pyramid_kvs2 have also a ‘mock’ implementation of a kvs used for testing purpose, register it using the pyramid plugins in your tests::

::
pyramid.includes =

pyramid_kvs2.testing

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

pyramid_kvs2-2.0.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyramid_kvs2-2.0.2-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file pyramid_kvs2-2.0.2.tar.gz.

File metadata

  • Download URL: pyramid_kvs2-2.0.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.1 CPython/3.12.10 Linux/6.11.0-1013-azure

File hashes

Hashes for pyramid_kvs2-2.0.2.tar.gz
Algorithm Hash digest
SHA256 0ee3a7f3a6a5f239c36a7065f58394fabf1d25e108d0ab22fc5c2a9d7f943c8d
MD5 e5f0d654b3de06d8e5cc65fb140d4b13
BLAKE2b-256 accdfe75403f48466e30d2e4ded25dc360f62d3cb1e1932cac28db245fa82957

See more details on using hashes here.

File details

Details for the file pyramid_kvs2-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyramid_kvs2-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.1 CPython/3.12.10 Linux/6.11.0-1013-azure

File hashes

Hashes for pyramid_kvs2-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dcb05b103323297deddf4338a8d1eb8bf24fdee0590f2266a9ea4f53db9b213b
MD5 59a43869f992817b689fdca43d0ea77b
BLAKE2b-256 c093fa867b21d08354f6fbeb86107c27f6e454a265723843314b454c64e4002d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page