Cache

PostgreSQL

class kinto.core.cache.postgresql.Cache(client, *args, **kwargs)

Cache backend using PostgreSQL.

Enable in configuration:

kinto.cache_backend = kinto.core.cache.postgresql

Database location URI can be customized:

kinto.cache_url = postgres://user:pass@db.server.lan:5432/dbname

Alternatively, username and password could also rely on system user ident or even specified in ~/.pgpass (see PostgreSQL documentation).

Note

Some tables and indices are created when kinto migrate is run. This requires some privileges on the database, or some error will be raised.

Alternatively, the schema can be initialized outside the python application, using the SQL file located in kinto/core/cache/postgresql/schema.sql. This allows to distinguish schema manipulation privileges from schema usage.

A connection pool is enabled by default:

kinto.cache_pool_size = 10
kinto.cache_maxoverflow = 10
kinto.cache_max_backlog = -1
kinto.cache_pool_recycle = -1
kinto.cache_pool_timeout = 30
kinto.cache_poolclass =
    kinto.core.storage.postgresql.pool.QueuePoolWithMaxBacklog

The max_backlog limits the number of threads that can be in the queue waiting for a connection. Once this limit has been reached, any further attempts to acquire a connection will be rejected immediately, instead of locking up all threads by keeping them waiting in the queue.

See dedicated section in SQLAlchemy documentation for default values and behaviour.

Note

Using a dedicated connection pool is still recommended to allow load balancing, replication or limit the number of connections used in a multi-process deployment.

Noindex:

Redis

class kinto_redis.cache.Cache(client, *args, **kwargs)

Cache backend implementation using Redis.

Enable in configuration:

kinto.cache_backend = kinto_redis.cache

(Optional) Instance location URI can be customized:

kinto.cache_url = redis://localhost:6379/1

A threaded connection pool is enabled by default:

kinto.cache_pool_size = 50

If the database is used for multiple Kinto deployement cache, you may want to add a prefix to every key to avoid collision:

kinto.cache_prefix = stack1_
Noindex:

Memory

class kinto.core.cache.memory.Cache(*args, **kwargs)

Cache backend implementation in local thread memory.

Enable in configuration:

kinto.cache_backend = kinto.core.cache.memory
Noindex:

API

Implementing a custom cache backend consists on implementing the following interface:

class kinto.core.cache.CacheBase(*args, **kwargs)
initialize_schema(dry_run=False)

Create every necessary objects (like tables or indices) in the backend.

This is executed when the kinto migrate command is run.

Parameters:dry_run (bool) – simulate instead of executing the operations.
flush()

Delete every values.

ttl(key)

Obtain the expiration value of the specified key.

Parameters:key (str) – key
Returns:number of seconds or negative if no TTL.
Return type:float
expire(key, ttl)

Set the expiration value ttl for the specified key.

Parameters:
  • key (str) – key
  • ttl (float) – number of seconds
set(key, value, ttl=None)

Store a value with the specified key. If ttl is provided, set an expiration value.

Parameters:
  • key (str) – key
  • value (str) – value to store
  • ttl (float) – expire after number of seconds
get(key)

Obtain the value of the specified key.

Parameters:key (str) – key
Returns:the stored value or None if missing.
Return type:str
delete(key)

Delete the value of the specified key.

Parameters:key (str) – key