Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 2m9s
Run linting on the backend code / Build (pull_request) Successful in 34s
Run testing on the backend code / Build (pull_request) Failing after 1m56s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 17s
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Module used for handling cache"""
|
|
from pymemcache import serde
|
|
from pymemcache.client.base import Client
|
|
|
|
from .constants import MEMCACHED_HOST_PATH
|
|
|
|
|
|
class DummyClient:
|
|
"""
|
|
A dummy in-memory client that mimics the behavior of a memcached client.
|
|
|
|
This class is designed to simulate the behavior of the `pymemcache.Client`
|
|
for testing or development purposes. It stores data in a Python dictionary
|
|
and provides methods to set, get, and update key-value pairs.
|
|
|
|
Attributes:
|
|
_data (dict): A dictionary that holds the key-value pairs.
|
|
|
|
Methods:
|
|
set(key, value, **kwargs):
|
|
Stores the given key-value pair in the internal dictionary.
|
|
|
|
set_many(data, **kwargs):
|
|
Updates the internal dictionary with multiple key-value pairs.
|
|
|
|
get(key, **kwargs):
|
|
Retrieves the value associated with the given key from the internal
|
|
dictionary.
|
|
"""
|
|
_data = {}
|
|
def set(self, key, value, **kwargs): # pylint: disable=unused-argument
|
|
"""
|
|
Store a key-value pair in the internal dictionary.
|
|
|
|
Args:
|
|
key: The key for the item to be stored.
|
|
value: The value to be stored under the given key.
|
|
**kwargs: Additional keyword arguments (unused).
|
|
"""
|
|
self._data[key] = value
|
|
|
|
def set_many(self, data, **kwargs): # pylint: disable=unused-argument
|
|
"""
|
|
Update the internal dictionary with multiple key-value pairs.
|
|
|
|
Args:
|
|
data: A dictionary containing key-value pairs to be added.
|
|
**kwargs: Additional keyword arguments (unused).
|
|
"""
|
|
self._data.update(data)
|
|
|
|
def get(self, key, **kwargs): # pylint: disable=unused-argument
|
|
"""
|
|
Retrieve the value associated with the given key.
|
|
|
|
Args:
|
|
key: The key for the item to be retrieved.
|
|
**kwargs: Additional keyword arguments (unused).
|
|
|
|
Returns:
|
|
The value associated with the given key if it exists.
|
|
"""
|
|
return self._data[key]
|
|
|
|
|
|
if MEMCACHED_HOST_PATH is None:
|
|
client = DummyClient()
|
|
else:
|
|
client = Client(
|
|
MEMCACHED_HOST_PATH,
|
|
timeout=1,
|
|
allow_unicode_keys=True,
|
|
encoding='utf-8',
|
|
serde=serde.pickle_serde
|
|
)
|