Skip to content

Commit

Permalink
Create custom CacheManager
Browse files Browse the repository at this point in the history
  • Loading branch information
aviupadhyayula committed Feb 5, 2024
1 parent d7ca8c3 commit f76c2ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
53 changes: 53 additions & 0 deletions backend/clubs/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import io
import logging
import re
from urllib.parse import urlparse

import bleach
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,
Expand Down Expand Up @@ -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()
10 changes: 9 additions & 1 deletion backend/pennclubs/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

0 comments on commit f76c2ef

Please sign in to comment.