From f76c2efa09a5bf117d1437da89bf7bc36030de30 Mon Sep 17 00:00:00 2001 From: aviupadhyayula Date: Mon, 5 Feb 2024 00:54:38 -0500 Subject: [PATCH] Create custom CacheManager --- backend/clubs/utils.py | 53 ++++++++++++++++++++++++ backend/pennclubs/settings/production.py | 10 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/backend/clubs/utils.py b/backend/clubs/utils.py index 5ccfc2f0f..fa6012c42 100644 --- a/backend/clubs/utils.py +++ b/backend/clubs/utils.py @@ -1,4 +1,5 @@ import io +import logging import re from urllib.parse import urlparse @@ -6,12 +7,18 @@ import requests from bs4 import BeautifulSoup, Comment, NavigableString from django.conf import settings +from django.core.cache import caches +from django.core.cache.backends.base import BaseCache from django.core.files.images import ImageFile from django.db.models import CharField, F, Q, Value from django.template.defaultfilters import slugify from PIL import Image +logging.basicConfig() +logger = logging.getLogger(__name__) + + def get_domain(request): """ Return the current domain that the request is coming from, @@ -355,3 +362,49 @@ def get_django_minified_image(url, **kwargs): resp = requests.get(url) new_image = resize_image(resp.content, **kwargs) return ImageFile(io.BytesIO(new_image), name="image.png") + + +class CacheManager: + _cache = None + _cache_fallback = None + + def __init__(self, *args, **kwargs): + BaseCache.__init__(self, *args, **kwargs) + self._cache = caches["main"] + self._cache_fallback = caches["fallback"] + + def set(self, key, value, timeout=None): + if timeout is None: + timeout = self.default_timeout + + try: + return self._cache.set(key, value, timeout) + except Exception as e: + logger.warning("Switching to fallback cache") + logger.exception(e) + return self._cache_fallback.set(key, value, timeout) + + def get(self, key, default=None): + try: + return self._cache.get(key, default) + except Exception as e: + logger.warning("Switching to fallback cache") + logger.exception(e) + return self._cache_fallback.get(key, default) + + def delete(self, key): + try: + return self._cache.delete(key) + except Exception as e: + logger.warning("Switching to fallback cache") + logger.exception(e) + return self._cache_fallback.delete(key) + + def clear(self): + try: + return self._cache.clear() + except Exception as e: + logger.warning("Switching to fallback cache") + logger.exception(e) + finally: + self._cache_fallback.clear() diff --git a/backend/pennclubs/settings/production.py b/backend/pennclubs/settings/production.py index 6ae09c1c0..858dfe5cd 100644 --- a/backend/pennclubs/settings/production.py +++ b/backend/pennclubs/settings/production.py @@ -59,9 +59,17 @@ # Caching settings CACHES = { "default": { + "BACKEND": "penn-clubs.backend.clubs.utils.CacheManager", + "TIMEOUT": 60 * 60, + }, + "main": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": f"redis://{REDIS_HOST}:6379/1", "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}, "KEY_PREFIX": "django", - } + }, + "fallback": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "unique", + }, }