Direct backend class or a decorator usage without calling setup
#106
-
Based on #105 (comment)
# user's file
from cashews.backends.redis import Redis as CashewsRedis
class CustomRedis(CashewsRedis):
...
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @AIGeneratedUsername .
import asyncio
import random
from cashews.backends.redis import Redis
from cashews.decorators.cache.simple import cache
async def func():
backend = Redis("redis://localhost")
await backend.init()
@cache(backend=backend, ttl="10m", prefix="hash")
async def get_token():
...
print(await get_token())
asyncio.run(func())
By the way now user can use his own backend fully integrated in cashews by register_backend function from cashews.backends.redis import Redis
from cashews import cache, register_backend
class MyRedis(Redis):
pass
register_backend("mybackend", MyRedis)
cache.setup("mybackend://", address="redis://localhost")
Regarding enum for backend types: Actially the same solution aiocache have but I don't like it. Most of modern production applications follow 12factors rules and use environment variables to configure it. It became ugly with enums and also If user want to set backend parameters by env variables: from os import env
from cashews import cache
backend = {"mem": backends.MEMORY, "redis": backends.REDIS}[env.get("CACHE_BACKEND", "mem")]
cache.setup(backend, max_connections=int(env.get("REDIS_CACHE_MAX_CONNECTIONS"), client_name=env.get("REDIS_APPLICATION_NAME") or even ugly ... That is why cashews try to solve it for user and force to use uri for configuration: from os import env
from cashews import cache
# CACHE_URI = "redis://redis.host:4888/1?max_connections=100&client_name=application"
cache.setup(env.get("CACHE_URI")) Also there is a problem with enum if I want to set my own backend
|
Beta Was this translation helpful? Give feedback.
Hi @AIGeneratedUsername .
setup
if he want to use backends directlysetup
is a part of the cache wrapper that handle: multibackend, middleware, autoinit, disable/enable functionality.By the way now user can use his own backend fully integrated in cashews by register_backend function