aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-06-06 21:30:23 -0400
committerBobby <[email protected]>2023-06-06 21:30:23 -0400
commit3b2f2278b14566e06209ffc4117e17221aedfe0a (patch)
treeecfbe3884c49744ce49138b32239a8a8f9c912fc
parent5761ebb8969605aa76b47d2bad887bcb99e16858 (diff)
downloadthatcomputerscientist-3b2f2278b14566e06209ffc4117e17221aedfe0a.tar.xz
thatcomputerscientist-3b2f2278b14566e06209ffc4117e17221aedfe0a.zip
Remove Custom Cache and Let Django Handle Cache
-rw-r--r--middleware/contentCachingMiddleware.py38
-rw-r--r--middleware/translationMiddleware.py22
-rw-r--r--thatcomputerscientist/settings.py1
3 files changed, 2 insertions, 59 deletions
diff --git a/middleware/contentCachingMiddleware.py b/middleware/contentCachingMiddleware.py
deleted file mode 100644
index a5820cd8..00000000
--- a/middleware/contentCachingMiddleware.py
+++ /dev/null
@@ -1,38 +0,0 @@
-from django.http import HttpResponse
-import redis
-
-
-r = redis.Redis(host='localhost', port=6379, db=0)
-
-class ContentCachingMiddleware(object):
- # We will cache all text/html responses for 1 hour
- CACHE_TTL = 60 * 60
-
- def __init__(self, get_response):
- self.get_response = get_response
-
- def __call__(self, request):
- lang_cookie = request.COOKIES.get('lang', '')
- path = request.get_full_path()
-
- # Don't cache POST requests or path contains 'blog-admin'
- if request.method == 'POST' or lang_cookie == 'ja' or 'blog-admin' in path:
- return self.get_response(request)
-
- # Try to get cached response
- cache_key = f'path_cache_{lang_cookie}:{path}'
-
- cached_response = r.get(cache_key)
- if cached_response:
- return HttpResponse(cached_response)
-
- # Get response from view
- response = self.get_response(request)
-
- # Cache response if content-type is text/html
- if response.status_code == 200 and 'text/html' in response.get('Content-Type', ''):
- print(cache_key, 'cached')
- r.set(cache_key, response.content.decode('utf-8'))
- r.expire(cache_key, self.CACHE_TTL)
-
- return response \ No newline at end of file
diff --git a/middleware/translationMiddleware.py b/middleware/translationMiddleware.py
index e7a1c1e2..a67453d0 100644
--- a/middleware/translationMiddleware.py
+++ b/middleware/translationMiddleware.py
@@ -1,10 +1,6 @@
import os
from bs4 import BeautifulSoup
from django.http import HttpResponse
-import redis
-
-
-r = redis.Redis(host='localhost', port=6379, db=0)
from django.conf import settings
from google.cloud import translate_v2 as translate
@@ -18,25 +14,15 @@ class TranslationMiddleware:
self.CACHE_TTL = 60 * 60
def __call__(self, request):
- # translate only if lang cookie is set to ja
response = self.get_response(request)
lang_cookie = request.COOKIES.get('lang', '')
- path = request.get_full_path()
- if lang_cookie != 'ja' or 'blog-admin' in path:
+ if lang_cookie != 'ja':
return response
content_type = response.get('Content-Type', '').lower()
if 'text' not in content_type:
return response
-
- # Try to get cached response
- cache_key = cache_key = f'path_cache_{lang_cookie}:{path}'
-
- cache_response = r.get(cache_key)
- if cache_response:
- response = HttpResponse(cache_response)
- return response
HTML_content =response.content.decode('utf-8')
@@ -58,8 +44,4 @@ class TranslationMiddleware:
target_language=target_language,
)['translatedText']
- r.set(cache_key, translated_content)
- r.expire(cache_key, self.CACHE_TTL)
-
- response.content = translated_content.encode('utf-8')
- return response
+ return HttpResponse(translated_content)
diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py
index 37aacc79..7c24b42f 100644
--- a/thatcomputerscientist/settings.py
+++ b/thatcomputerscientist/settings.py
@@ -88,7 +88,6 @@ MIDDLEWARE = [
'middleware.globalmetamiddleware.GlobalMetaMiddleware',
'middleware.uuidmiddleware.UserUUIDMiddleware',
'middleware.translationMiddleware.TranslationMiddleware',
- 'middleware.contentCachingMiddleware.ContentCachingMiddleware',
]
CONFIGURED_SUBDOMAINS = {