Skip to content
Snippets Groups Projects
Verified Commit 507fa328 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Implement proper cache busting

parent 124aca7b
No related branches found
No related tags found
No related merge requests found
...@@ -46,9 +46,10 @@ class Noop(Backend): ...@@ -46,9 +46,10 @@ class Noop(Backend):
class Redis(Backend): class Redis(Backend):
def __init__(self, params): def __init__(self, params, write_only=False):
self.params = params self.params = params
self._redis = None self._redis = None
self.write_only = write_only
async def redis(self): async def redis(self):
if self._redis: if self._redis:
...@@ -59,6 +60,8 @@ class Redis(Backend): ...@@ -59,6 +60,8 @@ class Redis(Backend):
return self._redis return self._redis
async def get(self, key): async def get(self, key):
if self.write_only:
raise self.NotFound(key)
r = await self.redis() r = await self.redis()
try: try:
v = await r.get(key) v = await r.get(key)
...@@ -89,9 +92,20 @@ class Redis(Backend): ...@@ -89,9 +92,20 @@ class Redis(Backend):
_DEFAULT = None _DEFAULT = None
def get_default(): _WRITE_ONLY_DEFAULT = None
def get_write_only_default():
global _WRITE_ONLY_DEFAULT
if _WRITE_ONLY_DEFAULT:
return _WRITE_ONLY_DEFAULT
_WRITE_ONLY_DEFAULT = Redis(settings.ASYNC_REDIS_PARAMS, write_only=True)
return _WRITE_ONLY_DEFAULT
def get_default(**kwargs):
global _DEFAULT global _DEFAULT
if _DEFAULT: if _DEFAULT:
return _DEFAULT return _DEFAULT
_DEFAULT = Redis(settings.ASYNC_REDIS_PARAMS) _DEFAULT = Redis(settings.ASYNC_REDIS_PARAMS, **kwargs)
return _DEFAULT return _DEFAULT
...@@ -98,7 +98,7 @@ class SearchSingleConsumer(AsyncHttpConsumer): ...@@ -98,7 +98,7 @@ class SearchSingleConsumer(AsyncHttpConsumer):
self.scope["query_string"].decode(), keep_blank_values=True self.scope["query_string"].decode(), keep_blank_values=True
) )
if "nocache" in params: if "nocache" in params:
c = cache.Noop() c = cache.get_write_only_default()
else: else:
c = cache.get_default() c = cache.get_default()
try: try:
......
...@@ -44,7 +44,6 @@ class Activitypub(Source): ...@@ -44,7 +44,6 @@ class Activitypub(Source):
] ]
async def get(self, lookup, session, cache): async def get(self, lookup, session, cache):
try: try:
actor_data = await cache.get("activitypub:profile:{}".format(lookup)) actor_data = await cache.get("activitypub:profile:{}".format(lookup))
except cache.NotFound: except cache.NotFound:
......
...@@ -72,7 +72,7 @@ async def test_search_consumer_no_cache( ...@@ -72,7 +72,7 @@ async def test_search_consumer_no_cache(
) )
response = await communicator.get_response() response = await communicator.get_response()
assert get.call_args[0][0] == "test@user.domain" assert get.call_args[0][0] == "test@user.domain"
assert isinstance(get.call_args[1]["cache"], cache.Noop) assert get.call_args[1]["cache"].write_only is True
get_profile.assert_called_once_with( get_profile.assert_called_once_with(
"webfinger", "test@user.domain", get.return_value "webfinger", "test@user.domain", get.return_value
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment