Skip to content

added:add multiple cache backend support #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions fancy_cache/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.core.cache import cache, DEFAULT_CACHE_ALIAS
from django.core.cache import caches, DEFAULT_CACHE_ALIAS
from django.utils.encoding import iri_to_uri
from django.utils.cache import (
get_cache_key,
Expand Down Expand Up @@ -128,7 +128,7 @@ def process_response(self, request, response):
if self.remember_all_urls:
self.remember_url(request, cache_key, timeout)

cache.set(cache_key, response, timeout)
self.cache.set(cache_key, response, timeout)

if self.post_process_response_always:
response = self.post_process_response_always(
Expand All @@ -140,9 +140,9 @@ def process_response(self, request, response):

def remember_url(self, request, cache_key, timeout):
url = request.get_full_path()
remembered_urls = cache.get(REMEMBERED_URLS_KEY, {})
remembered_urls = self.cache.get(REMEMBERED_URLS_KEY, {})
remembered_urls[url] = cache_key
cache.set(
self.cache.set(
REMEMBERED_URLS_KEY,
remembered_urls,
LONG_TIME
Expand Down Expand Up @@ -171,9 +171,9 @@ def process_request(self, request):
else:
cache_key += '__hits'
cache_key = md5(cache_key)
if cache.get(cache_key) is None:
cache.set(cache_key, 0, LONG_TIME)
cache.incr(cache_key)
if self.cache.get(cache_key) is None:
self.cache.set(cache_key, 0, LONG_TIME)
self.cache.incr(cache_key)
return response

def _process_request(self, request):
Expand Down Expand Up @@ -226,7 +226,7 @@ def _process_request(self, request):
# No cache information available, need to rebuild.
return None

response = cache.get(cache_key, None)
response = self.cache.get(cache_key, None)
if response is None:
request._cache_update_cache = True
# No cache information available, need to rebuild.
Expand Down Expand Up @@ -339,3 +339,4 @@ def __init__(
cache_alias = settings.CACHE_MIDDLEWARE_ALIAS

self.cache_alias = cache_alias
self.cache = caches[self.cache_alias]
4 changes: 4 additions & 0 deletions fancy_tests/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake'
},
'second_backend': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake'
}
}

Expand Down
17 changes: 16 additions & 1 deletion fancy_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from nose.tools import eq_, ok_
from django.test.client import RequestFactory
from django.core.cache import cache
from django.core.cache import cache, caches
from fancy_cache.memory import find_urls

from . import views
Expand Down Expand Up @@ -190,3 +190,18 @@ def test_remember_stats_all_urls_looong_url(self):
ok_(match[0].startswith('/something/really'))
eq_(match[2]['hits'], 1)
eq_(match[2]['misses'], 1)

def test_cache_backends(self):
request = self.factory.get('/anything')

response = views.home7(request)
eq_(response.status_code, 200)
ok_(re.findall('Random:\w+', response.content.decode("utf8")))
random_string_1 = re.findall('Random:(\w+)', response.content.decode("utf8"))[0]

# clear second cache backend
caches['second_backend'].clear()
response = views.home7(request)
eq_(response.status_code, 200)
random_string_2 = re.findall('Random:(\w+)', response.content.decode("utf8"))[0]
ok_(random_string_1 != random_string_2)
4 changes: 4 additions & 0 deletions fancy_tests/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ def home5bis(request):
@cache_page(60, remember_stats_all_urls=True, remember_all_urls=True)
def home6(request):
return _view(request)

@cache_page(60, cache='second_backend')
def home7(request):
return _view(request)