Commit 74b3ce0d authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Use redis cache for paginator count

parent f44c8b49
Pipeline #31560 canceled with stages
in 3 minutes and 40 seconds
from rest_framework.pagination import PageNumberPagination
from metagenedb.common.django_default.paginator import CachedCountPaginator
class DefaultPageNumberPagination(PageNumberPagination):
django_paginator_class = CachedCountPaginator
page_size_query_param = 'page_size'
max_page_size = 5000
import hashlib
import inspect
from django.conf import settings
from django.core.cache import cache
from django.core.cache.backends.base import DEFAULT_TIMEOUT
from django.core.paginator import Paginator
from django.utils.functional import cached_property
from django.utils.inspect import method_has_no_args
CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)
class CachedCountPaginator(Paginator):
@cached_property
def count(self):
"""Return the total number of objects, across all pages."""
# Create has from SQL query for REDIS cache
hash_object = hashlib.md5(str(self.object_list.query).encode('utf-8'))
redis_key = hash_object.hexdigest()
if redis_key in cache:
return cache.get(redis_key)
else:
c = getattr(self.object_list, 'count', None)
if callable(c) and not inspect.isbuiltin(c) and method_has_no_args(c):
count = c()
else:
count = len(self.object_list)
cache.set(redis_key, count, timeout=CACHE_TTL)
return count
......@@ -21,6 +21,8 @@ CACHES = {
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "example"
"KEY_PREFIX": "metagenedb"
}
}
# Time in seconds for
CACHE_TTL = int(os.getenv('CACHE_TTL', 600))
......@@ -21,8 +21,8 @@ kubectl create secret generic backend-cron-credentials -n ${NAMESPACE} --from-li
envsubst < ci/kubernetes/postgresql.yaml | kubectl apply -n ${NAMESPACE} -f -
kubectl -n ${NAMESPACE} wait --for=condition=available --timeout=600s deployment/postgresql
## Redis
# envsubst < ci/kubernetes/redis.yaml | kubectl apply -n ${NAMESPACE} -f -
# kubectl -n ${NAMESPACE} wait --for=condition=available --timeout=600s deployment/redis
envsubst < ci/kubernetes/redis.yaml | kubectl apply -n ${NAMESPACE} -f -
kubectl -n ${NAMESPACE} wait --for=condition=available --timeout=600s deployment/redis
## Backend
envsubst < ci/kubernetes/backend.yaml | kubectl apply -n ${NAMESPACE} -f -
kubectl -n ${NAMESPACE} patch deployment backend -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}"
......
......@@ -45,6 +45,8 @@ spec:
key: secret_key
- name: REDIS_HOST
value: redis
- name: REDIS_PORT
value: '6379'
- name: PORT
value: "8000"
- name: DEBUG
......
......@@ -64,6 +64,8 @@ services:
redis:
image: redis:alpine
container_name: redis
ports:
- "6379:6379"
networks:
- main
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment