Skip to content

Instantly share code, notes, and snippets.

@freelancing-solutions
Created April 8, 2023 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freelancing-solutions/6f338143d465f0c8fb06c29ee663c810 to your computer and use it in GitHub Desktop.
Save freelancing-solutions/6f338143d465f0c8fb06c29ee663c810 to your computer and use it in GitHub Desktop.
Cache Class
class Cache:
"""
A class to handle caching of data, both in-memory and in Redis.
The class provides thread-safe caching and automatic cache eviction when full.
:param max_size: The maximum size of the in-memory cache. The default is set in the config file.
:param expiration_time: The expiration time of each cache entry, in seconds. The default is set in the config
file.
:param use_redis: Indicates whether to use a Redis cache. The default is False.
"""
def __init__(self, cache_name: str = "mem", max_size: int = MEM_CACHE_SIZE, expiration_time: int = EXPIRATION_TIME,
use_redis: bool = False):
"""
Initializes the cache and creates a redis client if use_redis=True.
IF Redis fails the cache will fall back to using in Memory Cache
"""
self.max_size = max_size
self.expiration_time = expiration_time
self._cache_name = cache_name
self._cache = {}
self._lock = threading.Lock()
self._use_redis = use_redis
self._logger = init_logger(camel_to_snake(self.__class__.__name__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment