Add methods to delete the restricted_data_cache

This commit is contained in:
Oskar Hahn 2018-02-24 17:54:59 +01:00
parent c47d054c67
commit 5aa8f6193e
3 changed files with 29 additions and 2 deletions

View File

@ -1,5 +1,4 @@
import smtplib import smtplib
from random import choice from random import choice
from django.contrib.auth.hashers import make_password from django.contrib.auth.hashers import make_password

View File

@ -3,7 +3,7 @@ import threading
import time import time
import warnings import warnings
from collections import OrderedDict, defaultdict from collections import OrderedDict, defaultdict
from typing import Any, Dict, Generator, Iterable, List, Tuple, Union, Optional from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple, Union
from channels import Channel, Group from channels import Channel, Group
from channels.asgi import get_channel_layer from channels.asgi import get_channel_layer

View File

@ -396,6 +396,34 @@ class RestrictedDataCache:
self.get_cache_key(user_id), self.get_cache_key(user_id),
"{}/{}".format(collection_string, id)) "{}/{}".format(collection_string, id))
def del_user(self, user_id: int) -> None:
"""
Removes all elements for one user from the cache.
"""
redis = get_redis_connection()
redis.delete(self.get_cache_key(user_id))
def del_all(self) -> None:
"""
Deletes all elements from the cache.
This method uses the redis command SCAN. See
https://redis.io/commands/scan#scan-guarantees for its limitations. If
an element is added to the cache while del_all() is in process, it is
possible, that it is not deleted.
"""
redis = get_redis_connection()
# Get all keys that start with self.base_cache_key and delete them
match = cache.make_key('{}:*'.format(self.base_cache_key))
cursor = 0
while True:
cursor, keys = redis.scan(cursor, match)
for key in keys:
redis.delete(key)
if cursor == 0:
return
def exists_for_user(self, user_id: int) -> bool: def exists_for_user(self, user_id: int) -> bool:
""" """
Returns True if the cache for the user exists, else False. Returns True if the cache for the user exists, else False.