API reference

Cache Function

cache_alchemy.cache(limit: Optional[int], expire: Optional[int], is_method: bool, strict: bool, backend: str, dependency: List[cache_alchemy.dependency.CacheDependency], cache_key_prefix: str = '', **kwargs) → Callable[function, Callable[..., ReturnType]][source]

The base function to creat a cache object like this:

@cache(
    limit=1000,
    expire=60,
    is_method=False,
    strict=True,
    backend="cache_alchemy.backends.memory.MemoryCache",
    dependency=[],
)
def f(x, y):
    pass

# To clear cache
f.cache_clear()
Parameters:
  • expire (int) – expire time with an integer value used as seconds.
  • is_method (bool) – If True, the first argument will be ignored in generate cache key.
  • strict (bool) – If False, make a cache key in a way that is flat as possible rather than as a nested and strict structure that would support partially cache clear. it means that f(x=1, y=2) will now be treated as a distinct call from f(y=2, x=1) which will be cached separately.

DefaultConfig Object

class cache_alchemy.config.DefaultConfig[source]

Bases: configalchemy.configalchemy.BaseConfig

CACHE_ALCHEMY_CACHE_KEY_PREFIX = ''

cache key prefix to avoid key conflict

CACHE_ALCHEMY_DEFAULT_EXPIRE = 86400

default cache expire time (seconds) - setting to 0 means uncached

CACHE_ALCHEMY_DEFAULT_LIMIT = 1000

default cache limit per function - setting to -1 means unlimited - setting to 0 means uncached

CACHE_ALCHEMY_JSON_BACKEND = 'cache_alchemy.backends.json.DistributedJsonCache'

distributed json cache backend - default: distributed cache which need assign client to config

CACHE_ALCHEMY_MEMORY_BACKEND = 'cache_alchemy.backends.memory.DistributedMemoryCache'

memory cache backend - default: distributed cache which need assign client to config

CACHE_ALCHEMY_PICKLE_BACKEND = 'cache_alchemy.backends.pickle.DistributedPickleCache'

memory cache backend - default: distributed cache which need assign client to config

CACHE_ALCHEMY_REDIS_URL = 'redis://127.0.0.1:6379/0'

default redis url

cache_redis_client = None

Need to be assigned after init, if use distributed cache

FunctionCacheDependency Object

Examples:

@json_cache()
def add(a, b):
    return a + b

dependency = FunctionCacheDependency(add)

@json_cache(dependency=[dependency])
def add_and_double(a, b):
    return add(a, b) * 2