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.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.encoders import CustomJSONEncoder
|
from services.encoders import CustomJSONEncoder
|
||||||
|
from services.memorycache import authors_cache_region
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
from services.schema import mutation, query
|
from services.schema import mutation, query
|
||||||
from services.logger import root_logger as logger
|
from services.logger import root_logger as logger
|
||||||
|
@ -109,37 +110,45 @@ async def get_author_id(_, _info, user: str):
|
||||||
|
|
||||||
@query.field('load_authors_by')
|
@query.field('load_authors_by')
|
||||||
async def load_authors_by(_, _info, by, limit, offset):
|
async def load_authors_by(_, _info, by, limit, offset):
|
||||||
logger.debug(f'loading authors by {by}')
|
cache_key = f"load_authors_by_{json.dumps(by)}_{limit}_{offset}"
|
||||||
q = select(Author)
|
|
||||||
if by.get('slug'):
|
|
||||||
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
|
|
||||||
elif by.get('name'):
|
|
||||||
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
|
||||||
elif by.get('topic'):
|
|
||||||
q = (
|
|
||||||
q.join(ShoutAuthor)
|
|
||||||
.join(ShoutTopic)
|
|
||||||
.join(Topic)
|
|
||||||
.where(Topic.slug == str(by['topic']))
|
|
||||||
)
|
|
||||||
|
|
||||||
if by.get('last_seen'): # in unix time
|
@authors_cache_region.cache_on_arguments(cache_key)
|
||||||
before = int(time.time()) - by['last_seen']
|
async def _load_authors_by(_, _info, by, limit, offset):
|
||||||
q = q.filter(Author.last_seen > before)
|
logger.debug(f'loading authors by {by}')
|
||||||
elif by.get('created_at'): # in unix time
|
q = select(Author)
|
||||||
before = int(time.time()) - by['created_at']
|
if by.get('slug'):
|
||||||
q = q.filter(Author.created_at > before)
|
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
|
||||||
|
elif by.get('name'):
|
||||||
|
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
||||||
|
elif by.get('topic'):
|
||||||
|
q = (
|
||||||
|
q.join(ShoutAuthor)
|
||||||
|
.join(ShoutTopic)
|
||||||
|
.join(Topic)
|
||||||
|
.where(Topic.slug == str(by['topic']))
|
||||||
|
)
|
||||||
|
|
||||||
order = by.get('order')
|
if by.get('last_seen'): # in unix time
|
||||||
if order in ['likes', 'shouts', 'followers']:
|
before = int(time.time()) - by['last_seen']
|
||||||
q = q.order_by(desc(text(f'{order}_stat')))
|
q = q.filter(Author.last_seen > before)
|
||||||
|
elif by.get('created_at'): # in unix time
|
||||||
|
before = int(time.time()) - by['created_at']
|
||||||
|
q = q.filter(Author.created_at > before)
|
||||||
|
|
||||||
# q = q.distinct()
|
order = by.get('order')
|
||||||
q = q.limit(limit).offset(offset)
|
if order in ['likes', 'shouts', 'followers']:
|
||||||
|
q = q.order_by(desc(text(f'{order}_stat')))
|
||||||
|
|
||||||
|
# q = q.distinct()
|
||||||
|
q = q.limit(limit).offset(offset)
|
||||||
|
|
||||||
|
authors = get_with_stat(q)
|
||||||
|
|
||||||
|
return authors
|
||||||
|
|
||||||
|
return await _load_authors_by()
|
||||||
|
|
||||||
authors = get_with_stat(q)
|
|
||||||
|
|
||||||
return authors
|
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author_follows')
|
@query.field('get_author_follows')
|
||||||
|
|
|
@ -1,26 +1,11 @@
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
from dogpile.cache import make_region
|
from dogpile.cache import make_region
|
||||||
|
|
||||||
# Создание региона кэша с TTL 300 секунд
|
from settings import REDIS_URL
|
||||||
cache_region = make_region().configure('dogpile.cache.memory', expiration_time=300)
|
|
||||||
|
|
||||||
|
# Создание региона кэша с TTL
|
||||||
# Декоратор для кэширования методов
|
authors_cache_region = make_region()
|
||||||
def cache_method(cache_key: str):
|
authors_cache_region.configure(
|
||||||
def decorator(f):
|
'dogpile.cache.redis',
|
||||||
@wraps(f)
|
arguments={'url': f'{REDIS_URL}/1'},
|
||||||
def decorated_function(*args, **kwargs):
|
expiration_time=3600, # Cache expiration time in seconds
|
||||||
# Генерация ключа для кэширования
|
)
|
||||||
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
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user