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
Metadata
Release files for pyramid-kvs2 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyramid_kvs2-2.1.1.tar.gz | 15.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyramid_kvs2-2.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.2 kB
Release files / pyramid_kvs2-2.1.1.tar.gz
| Download URL | pyramid_kvs2-2.1.1.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fdbc9695cdb22b6a13f3a1447f6994411b692569ca1ccc4525dca1d2a0d6aab3
|
|
BLAKE2b-256 checksum How to use checksums |
34d152023e39218146d1d852ca0fbbdd060fd819840224fe474ed52cc3c80afe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.25.9 CPython/3.12.11 Linux/6.11.0-1018-azure
|
Release files / pyramid_kvs2-2.1.1-py3-none-any.whl
| Download URL | pyramid_kvs2-2.1.1-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8bf8e2e44b5b9a52368828027d7b09eee3928d1252e0201d92ba655f9cd43ba6
|
|
BLAKE2b-256 checksum How to use checksums |
617b272f1bceb6e77c65cef32791f93d6c0f45b7736e6906d287c7c3e0605f15
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.25.9 CPython/3.12.11 Linux/6.11.0-1018-azure
|