Cache

PostgreSQL

class cliquet.cache.postgresql.PostgreSQL(**kwargs)

Cache backend using PostgreSQL.

Enable in configuration:

cliquet.cache_backend = cliquet.cache.postgresql

Database location URI can be customized:

cliquet.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 cliquet 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 cliquet/cache/postgresql/schema.sql. This allows to distinguish schema manipulation privileges from schema usage.

A threaded connection pool is enabled by default:

cliquet.cache_pool_size = 10

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 cliquet.cache.redis.Redis(*args, **kwargs)

Cache backend implementation using Redis.

Enable in configuration:

cliquet.cache_backend = cliquet.cache.redis

(Optional) Instance location URI can be customized:

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

A threaded connection pool is enabled by default:

cliquet.cache_pool_size = 50
Noindex:

Memory

class cliquet.cache.memory.Memory(*args, **kwargs)

Cache backend implementation in local thread memory.

Enable in configuration:

cliquet.cache_backend = cliquet.cache.memory
Noindex:

API

Implementing a custom cache backend consists in implementating the following interface:

class cliquet.cache.CacheBase(*args, **kwargs)
initialize_schema()

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

This is excuted when the cliquet migrate command is ran.

flush()

Delete every values.

ping(request)

Test that cache backend is operationnal.

Parameters:request (Request) – current request object
Returns:True is everything is ok, False otherwise.
Return type:bool
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