"""Boilerplate redis cache adapter. Store sales, transaction fields, and initial statement_detail_id. """ from redis import Redis # from accounting import config class CacheAdapter(): """CacheAdapter class.""" def __init__(self): """Create the adapter.""" self._connection = self._get_connection() def _get_connection(self): """Mockable getter.""" return Redis(unix_socket_path='/tmp/redis.sock') def __del__(self): """Teardown the connection.""" if self._connection: del self._connection def cache_item(self, key, value): """Cache a value under key. Args: key (str|number): Cache key value (str|number): Value to cache """ return self._connection.set(key, value) def get_item(self, key): """Return the value under key if it exists. Args: key (str|number): Cache key Returns: str|None: If found in cache, a string representation of the value else None. """ return self._connection.get(key) def ping(self): """Perform a simple ping, ensuring the connection is active.""" return self._connection.ping() def flushall(self): """Reset the cache completely.""" return self._connection.flushall()