dogpiled-cache-authors
This commit is contained in:
parent
625836afee
commit
6064f0326a
|
@ -14,6 +14,7 @@ from services.cache import set_author_cache, update_author_followers_cache
|
|||
from services.auth import login_required
|
||||
from services.db import local_session
|
||||
from services.encoders import CustomJSONEncoder
|
||||
from services.memorycache import authors_cache_region
|
||||
from services.rediscache import redis
|
||||
from services.schema import mutation, query
|
||||
from services.logger import root_logger as logger
|
||||
|
@ -109,6 +110,10 @@ async def get_author_id(_, _info, user: str):
|
|||
|
||||
@query.field('load_authors_by')
|
||||
async def load_authors_by(_, _info, by, limit, offset):
|
||||
cache_key = f"load_authors_by_{json.dumps(by)}_{limit}_{offset}"
|
||||
|
||||
@authors_cache_region.cache_on_arguments(cache_key)
|
||||
async def _load_authors_by(_, _info, by, limit, offset):
|
||||
logger.debug(f'loading authors by {by}')
|
||||
q = select(Author)
|
||||
if by.get('slug'):
|
||||
|
@ -141,6 +146,10 @@ async def load_authors_by(_, _info, by, limit, offset):
|
|||
|
||||
return authors
|
||||
|
||||
return await _load_authors_by()
|
||||
|
||||
|
||||
|
||||
|
||||
@query.field('get_author_follows')
|
||||
async def get_author_follows(_, _info, slug='', user=None, author_id=None):
|
||||
|
|
|
@ -1,26 +1,11 @@
|
|||
from functools import wraps
|
||||
|
||||
from dogpile.cache import make_region
|
||||
|
||||
# Создание региона кэша с TTL 300 секунд
|
||||
cache_region = make_region().configure('dogpile.cache.memory', expiration_time=300)
|
||||
from settings import REDIS_URL
|
||||
|
||||
|
||||
# Декоратор для кэширования методов
|
||||
def cache_method(cache_key: str):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
# Генерация ключа для кэширования
|
||||
key = cache_key.format(*args, **kwargs)
|
||||
# Получение значения из кэша
|
||||
result = cache_region.get(key)
|
||||
if result is None:
|
||||
# Если значение отсутствует в кэше, вызываем функцию и кэшируем результат
|
||||
result = f(*args, **kwargs)
|
||||
cache_region.set(key, result)
|
||||
return result
|
||||
|
||||
return decorated_function
|
||||
|
||||
return decorator
|
||||
# Создание региона кэша с TTL
|
||||
authors_cache_region = make_region()
|
||||
authors_cache_region.configure(
|
||||
'dogpile.cache.redis',
|
||||
arguments={'url': f'{REDIS_URL}/1'},
|
||||
expiration_time=3600, # Cache expiration time in seconds
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user