diff options
| author | Bobby <[email protected]> | 2023-06-06 21:04:56 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-06-06 21:04:56 -0400 |
| commit | 039d7b6ac3607dcff22a831ff240417e683c1f22 (patch) | |
| tree | ada5f67a8916c318daa5199e656dec021e2ddb08 /middleware | |
| parent | 5de7630148dd9ef69a66ea269e0b8077628e51ca (diff) | |
| download | thatcomputerscientist-039d7b6ac3607dcff22a831ff240417e683c1f22.tar.xz thatcomputerscientist-039d7b6ac3607dcff22a831ff240417e683c1f22.zip | |
Text Content Caching for 1 hr to reduce load and response times
Diffstat (limited to 'middleware')
| -rw-r--r-- | middleware/contentCachingMiddleware.py | 43 | ||||
| -rw-r--r-- | middleware/translationMiddleware.py | 43 |
2 files changed, 68 insertions, 18 deletions
diff --git a/middleware/contentCachingMiddleware.py b/middleware/contentCachingMiddleware.py new file mode 100644 index 00000000..d5f23f88 --- /dev/null +++ b/middleware/contentCachingMiddleware.py @@ -0,0 +1,43 @@ +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', '') + + # Don't cache POST requests + if request.method == 'POST' or lang_cookie == 'ja': + return self.get_response(request) + + # Try to get cached response + path = request.get_full_path() + cache_key = f'{lang_cookie}:{path}' + + # clear cache if clear_cache is in query params + if request.GET.get('cc', '') == 'true': + r.delete(cache_key) + + cached_response = r.get(cache_key) + if cached_response: + print('cached', cache_key) + 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 ba1eda1f..4687984d 100644 --- a/middleware/translationMiddleware.py +++ b/middleware/translationMiddleware.py @@ -1,8 +1,11 @@ import os +from django.http import HttpResponse +import redis + + +r = redis.Redis(host='localhost', port=6379, db=0) -from bs4 import BeautifulSoup from django.conf import settings -from django.core.cache import cache from google.cloud import translate_v2 as translate cred_path = os.path.join(settings.BASE_DIR, 'credentials-translate.json') @@ -11,36 +14,40 @@ os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cred_path class TranslationMiddleware: def __init__(self, get_response): self.get_response = get_response + self.CACHE_TTL = 60 * 60 def __call__(self, request): - response = self.get_response(request) - # translate only if lang cookie is set to ja - if request.COOKIES.get('lang') != 'ja': + response = self.get_response(request) + lang_cookie = request.COOKIES.get('lang', '') + + if lang_cookie != 'ja': return response content_type = response.get('Content-Type', '').lower() if 'text' not in content_type: return response - HTML_content =response.content.decode('utf-8').strip() + # Try to get cached response + path = request.get_full_path() + cache_key = cache_key = f'{lang_cookie}:{path}' + print(f'Japanese translation for {path} requested') + + cache_response = r.get(cache_key) + if cache_response: + print('cached', cache_key) + # r.delete(cache_key) + response = HttpResponse(cache_response) + return response - # add no translate class to the 'highlight' class - soup = BeautifulSoup(HTML_content, 'html.parser') - for tag in soup.find_all(class_='highlight'): - tag['class'].append('notranslate') - HTML_content = str(soup) + print('Cache not found, translating... for key: ', cache_key) + HTML_content =response.content.decode('utf-8') HTML_content = HTML_content.replace( "That Computer Scientist", "ザットコンピュータサイエンティスト" ) - # check if the content is already in the cache - if cache.get(HTML_content): - response.content = cache.get(HTML_content) - return response - translate_client = translate.Client() target_language = 'ja' translated_content = translate_client.translate( @@ -48,8 +55,8 @@ class TranslationMiddleware: target_language=target_language, )['translatedText'] - # set the content in the cache for 7 days - cache.set(HTML_content, translated_content, 60 * 60 * 24 * 7) + r.set(cache_key, translated_content) + r.expire(cache_key, self.CACHE_TTL) response.content = translated_content.encode('utf-8') return response |
