2021-03-04 17:47:13 +01:00
|
|
|
# standard imports
|
|
|
|
import hashlib
|
|
|
|
import logging
|
|
|
|
|
2021-02-06 16:13:47 +01:00
|
|
|
# third-party imports
|
|
|
|
from redis import Redis
|
|
|
|
|
2021-03-04 17:47:13 +01:00
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2021-02-06 16:13:47 +01:00
|
|
|
|
|
|
|
class InMemoryStore:
|
|
|
|
cache: Redis = None
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def cache_data(key: str, data: str):
|
|
|
|
"""
|
|
|
|
:param key:
|
|
|
|
:type key:
|
|
|
|
:param data:
|
|
|
|
:type data:
|
|
|
|
:return:
|
|
|
|
:rtype:
|
|
|
|
"""
|
|
|
|
cache = InMemoryStore.cache
|
|
|
|
cache.set(name=key, value=data)
|
|
|
|
cache.persist(name=key)
|
|
|
|
|
|
|
|
|
|
|
|
def get_cached_data(key: str):
|
|
|
|
"""
|
|
|
|
:param key:
|
|
|
|
:type key:
|
|
|
|
:return:
|
|
|
|
:rtype:
|
|
|
|
"""
|
|
|
|
cache = InMemoryStore.cache
|
|
|
|
return cache.get(name=key)
|
|
|
|
|
|
|
|
|
|
|
|
def create_cached_data_key(identifier: bytes, salt: str):
|
|
|
|
"""
|
|
|
|
:param identifier:
|
|
|
|
:type identifier:
|
|
|
|
:param salt:
|
|
|
|
:type salt:
|
|
|
|
:return:
|
|
|
|
:rtype:
|
|
|
|
"""
|
|
|
|
hash_object = hashlib.new("sha256")
|
|
|
|
hash_object.update(identifier)
|
|
|
|
hash_object.update(salt.encode(encoding="utf-8"))
|
|
|
|
return hash_object.digest().hex()
|