API Reference¶
Core¶
psycache.PostgresCache
¶
A Postgres-based cache.
__init__(pool, *, instrumentations=())
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pool
|
CachePool
|
The cache pool to use. |
required |
instrumentations
|
Sequence[CacheInstrumentation]
|
Sequence of instrumentations to use. |
()
|
cleanup_expired()
¶
Delete all expired cache entries.
Return the number of deleted entries.
flush()
¶
Flush all cache entries.
Return the number of flushed entries.
get_raw(key, span_name=None)
¶
Get a raw dict from the cache for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
span_name
|
str | None
|
Name for the span that is passed to instrumentation. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The raw dict for key, or None if the key is not found or expired. |
put_raw(key, value, ttl, span_name=None)
¶
Put value into the cache under key with time-to-live of ttl.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key under which to store the value. |
required |
value
|
dict[str, Any]
|
The value to store in the cache. |
required |
ttl
|
int | timedelta
|
The time-to-live for the cache entry. |
required |
span_name
|
str | None
|
Name for the span that is passed to instrumentation. |
None
|
remove(key)
¶
Remove the cache entry for key.
Trying to remove a non-existent key is a no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to remove. |
required |
start_cleanup_thread(interval)
¶
Start a daemon thread that periodically deletes expired cache entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
timedelta | float
|
Time between cleanup runs. In seconds or as a timedelta. |
required |
Returns:
| Type | Description |
|---|---|
CleanupService
|
A |
psycache.AsyncPostgresCache
¶
An asyncio-based Postgres cache.
cleanup_expired()
async
¶
Same as PostgresCache.cleanup_expired,
but async.
flush()
async
¶
Same as PostgresCache.flush,
but async.
get_raw(key, span_name=None)
async
¶
Same as PostgresCache.get_raw,
but async.
put_raw(key, value, ttl, span_name=None)
async
¶
Same as PostgresCache.put_raw,
but async.
remove(key)
async
¶
Same as PostgresCache.remove,
but async.
start_cleanup_task(interval)
¶
Start a Task that periodically deletes expired
cache entries.
Must be called within a running asyncio event loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
timedelta | float
|
Time between cleanup runs. In seconds or as a timedelta. |
required |
Returns:
| Type | Description |
|---|---|
AsyncCleanupService
|
An |
psycache.CleanupService
¶
Handle for a periodic cache cleanup thread.
Can be used as a context manager or stopped manually via stop().
stop(timeout=5.0)
¶
Stop the cleanup thread and wait for it to finish.
Return whether the thread exited before the timeout elapsed.
psycache.AsyncCleanupService
¶
Handle for a periodic cache cleanup task.
Can be used as an async context manager or stopped manually via stop().
stop(timeout=5.0)
async
¶
Stop the cleanup task and wait for it to finish.
Return whether the task exited before the timeout elapsed.
psycache.init_db(conn)
¶
Create the psycache table if it doesn’t exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
A psycopg connection. |
required |
Pool adapters¶
psycopg_pool¶
psycache.psycopg_pool.PsycopgCachePool
¶
A cache pool based on psycopg_pool.ConnectionPool.
psycache.psycopg_pool.AsyncPsycopgCachePool
¶
A cache pool based on psycopg_pool.AsyncConnectionPool.
SQLAlchemy¶
psycache.sqlalchemy.SQLAlchemyCachePool
¶
A cache pool based on a SQLAlchemy engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Engine
|
The SQLAlchemy engine to use. |
required |
psycache.sqlalchemy.AsyncSQLAlchemyCachePool
¶
A cache pool based on a SQLAlchemy async engine.
Instrumentation¶
psycache.instrumentation.prometheus.PrometheusInstrumentation
¶
Prometheus-backed instrumentation for cache operations.
psycache.instrumentation.sentry.SentryInstrumentation
¶
Sentry-backed instrumentation for cache operations.