Cache¶
TenantCache ¶
In-process LRU cache with per-entry TTL for tenant objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum number of tenant entries to hold (LRU eviction when full). Default: 1000. |
1000
|
ttl
|
int
|
Seconds before an entry is considered stale. Default: 60. |
60
|
Example::
cache = TenantCache(max_size=500, ttl=30)
cache.set(tenant)
hit = cache.get(tenant.id) # by ID
hit = cache.get_by_identifier("acme-corp") # by slug
cache.invalidate(tenant.id)
cache.clear()
Source code in src/fastapi_tenancy/cache/tenant_cache.py
get ¶
get(tenant_id: str) -> Tenant | None
Return the cached tenant for tenant_id, or None on miss/expiry.
On a cache hit, the entry is promoted to the MRU position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
Opaque tenant primary key. |
required |
Returns:
| Type | Description |
|---|---|
Tenant | None
|
Cached |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
get_by_identifier ¶
get_by_identifier(identifier: str) -> Tenant | None
Return the cached tenant for identifier, or None on miss/expiry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Human-readable tenant slug. |
required |
Returns:
| Type | Description |
|---|---|
Tenant | None
|
Cached |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
set ¶
set(tenant: Tenant) -> None
Insert or refresh tenant in the cache.
When the cache is at capacity, the LRU entry is evicted before inserting the new entry.
Thread/task safety: all mutations are protected by _get_lock().
Call await cache.aset(tenant) from async contexts to benefit from
the lock; set() is kept synchronous for backwards-compatibility
but should only be called before any concurrent tasks access the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant
|
Tenant
|
The tenant to cache. Both ID and identifier keys are written atomically. |
required |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
aset
async
¶
aset(tenant: Tenant) -> None
Async variant of set() — acquires the mutex before writing.
Use this in async code paths (middleware, resolvers, cache proxies) to guarantee safety under concurrent task scheduling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant
|
Tenant
|
The tenant to cache. |
required |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
invalidate ¶
Remove the cache entry for tenant_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
Opaque tenant primary key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/fastapi_tenancy/cache/tenant_cache.py
invalidate_by_identifier ¶
Remove the cache entry for identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Human-readable tenant slug. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/fastapi_tenancy/cache/tenant_cache.py
clear ¶
Evict all entries and reset both indices.
Returns:
| Type | Description |
|---|---|
int
|
Number of entries evicted. |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
| Python | |
|---|---|
size ¶
stats ¶
Return a snapshot of cache state for monitoring.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary with:
- |
Source code in src/fastapi_tenancy/cache/tenant_cache.py
purge_expired ¶
Eagerly remove all expired entries.
Not required — entries are lazily evicted on access — but useful to call periodically in low-traffic applications to reclaim memory.
Returns:
| Type | Description |
|---|---|
int
|
Number of entries evicted. |