Verified Commit 507fa328 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Implement proper cache busting

parent 124aca7b
......@@ -46,9 +46,10 @@ class Noop(Backend):
class Redis(Backend):
def __init__(self, params):
def __init__(self, params, write_only=False):
self.params = params
self._redis = None
self.write_only = write_only
async def redis(self):
if self._redis:
......@@ -59,6 +60,8 @@ class Redis(Backend):
return self._redis
async def get(self, key):
if self.write_only:
raise self.NotFound(key)
r = await self.redis()
try:
v = await r.get(key)
......@@ -89,9 +92,20 @@ class Redis(Backend):
_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
if _DEFAULT:
return _DEFAULT
_DEFAULT = Redis(settings.ASYNC_REDIS_PARAMS)
_DEFAULT = Redis(settings.ASYNC_REDIS_PARAMS, **kwargs)
return _DEFAULT
......@@ -98,7 +98,7 @@ class SearchSingleConsumer(AsyncHttpConsumer):
self.scope["query_string"].decode(), keep_blank_values=True
)
if "nocache" in params:
c = cache.Noop()
c = cache.get_write_only_default()
else:
c = cache.get_default()
try:
......
......@@ -44,7 +44,6 @@ class Activitypub(Source):
]
async def get(self, lookup, session, cache):
try:
actor_data = await cache.get("activitypub:profile:{}".format(lookup))
except cache.NotFound:
......
......@@ -72,7 +72,7 @@ async def test_search_consumer_no_cache(
)
response = await communicator.get_response()
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(
"webfinger", "test@user.domain", get.return_value
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment