sift_py.grpc.cache
¶
Utilities for controlling gRPC response caching.
This module provides helper functions and constants for working with the gRPC caching interceptor. Use these utilities to control caching behavior on a per-request basis via metadata.
Example
from sift_py.grpc.cache import with_cache, with_force_refresh, ignore_cache
Enable caching for a request¶
metadata = with_cache() response = stub.GetData(request, metadata=metadata)
Force refresh (bypass cache and store fresh result)¶
metadata = with_force_refresh() response = stub.GetData(request, metadata=metadata)
Ignore cache without clearing¶
metadata = ignore_cache() response = stub.GetData(request, metadata=metadata)
| CLASS | DESCRIPTION |
|---|---|
CacheSettings |
Resolved cache metadata from gRPC request. |
GrpcCache |
Subclass of diskcache.Cache for gRPC response caching. |
| FUNCTION | DESCRIPTION |
|---|---|
with_cache |
Enable caching for a gRPC request. |
with_force_refresh |
Force refresh the cache for a gRPC request. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
METADATA_CACHE_TTL |
|
METADATA_FORCE_REFRESH |
|
METADATA_USE_CACHE |
|
logger |
|
CacheSettings
¶
Bases: NamedTuple
Resolved cache metadata from gRPC request.
| ATTRIBUTE | DESCRIPTION |
|---|---|
custom_ttl |
TYPE:
|
force_refresh |
TYPE:
|
use_cache |
TYPE:
|
GrpcCache
¶
GrpcCache(config: SiftCacheConfig)
Bases: Cache
Subclass of diskcache.Cache for gRPC response caching.
Initialize the cache from configuration.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Cache configuration with ttl, cache_path, size_limit, clear_on_init.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
key_from_proto_message |
|
resolve_cache_metadata |
Extract and resolve cache-related metadata fields. |
set_with_default_ttl |
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
cache_path |
|
default_ttl |
|
size_limit |
|
key_from_proto_message
staticmethod
¶
resolve_cache_metadata
staticmethod
¶
resolve_cache_metadata(
metadata: tuple[tuple[str, str], ...] | None,
) -> CacheSettings
Extract and resolve cache-related metadata fields.
| PARAMETER | DESCRIPTION |
|---|---|
metadata
|
The gRPC request metadata tuple. |
| RETURNS | DESCRIPTION |
|---|---|
CacheSettings
|
CacheMetadata named tuple with resolved cache control fields: |
CacheSettings
|
|
CacheSettings
|
|
CacheSettings
|
|
CacheSettings
|
|
CacheSettings
|
|
CacheSettings
|
|
Example
cache_info = cache.resolve_cache_metadata(metadata) if cache_info.should_read: cached = cache.get(key) if cache_info.should_cache: cache.set_with_default_ttl(key, response, expire=cache_info.custom_ttl)
with_cache
¶
Enable caching for a gRPC request.
| PARAMETER | DESCRIPTION |
|---|---|
ttl
|
Optional custom TTL in seconds. If not provided, uses the default TTL.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[str, str], ...]
|
Metadata tuple to pass to the gRPC stub method. |
Example
metadata = with_cache() response = stub.GetData(request, metadata=metadata)
With custom TTL¶
metadata = with_cache(ttl=7200) # 2 hours response = stub.GetData(request, metadata=metadata)
with_force_refresh
¶
Force refresh the cache for a gRPC request.
Bypasses the cache, fetches fresh data from the server, and stores the result.
| PARAMETER | DESCRIPTION |
|---|---|
ttl
|
Optional custom TTL in seconds. If not provided, uses the default TTL.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[str, str], ...]
|
Metadata tuple to pass to the gRPC stub method. |
Example
metadata = with_force_refresh() response = stub.GetData(request, metadata=metadata)