(20260109) Almost done with Cosec-TCAOFF sync.
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To define a caching class that uses Redis to asynchronously cache information.
|
||||
To define a caching class that uses Redis to asynchronously cache information. This is truly stateless caching
|
||||
since yu can run your backend script from any number of servers and yet have the same cache data sync'd through
|
||||
Redis.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -43,7 +45,7 @@ from redis.asyncio.sentinel import Sentinel
|
||||
# Other utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.serialization.pickle_serializer import PickleSerializer
|
||||
from utils_v2.cache_v2.base import AsyncCachingBase
|
||||
from utils_v2.cache_v2.async_base import AsyncCachingBase
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import List, Any
|
||||
@@ -80,7 +82,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
connection_string: str | dict = None,
|
||||
connection_params: str | dict = None,
|
||||
serializer = None,
|
||||
ping_counter = 1_000,
|
||||
debug = False,
|
||||
@@ -90,7 +92,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
"""
|
||||
Implements a simple cache in Redis which holds and returns all native datatypes like ints, floats, bools,
|
||||
strings, dicts, lists, sets, and tuples :)
|
||||
:param connection_string: The connection URL or Sentinel JSON for connecting to Redis.
|
||||
:param connection_params: The connection URL or Sentinel JSON for connecting to Redis.
|
||||
:param serializer: The serializer to use.
|
||||
:param ping_counter: The number of requests to Redis after which you want to ping to ensure connection.
|
||||
:param debug: Whether, or not, you want to show debugging messages from the start.
|
||||
@@ -110,30 +112,19 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
self.__requests_since_last_ping = 0
|
||||
|
||||
# Figure out the connection mechanism:
|
||||
if isinstance(connection_string, dict):
|
||||
self.__sentinel_json = connection_string
|
||||
if isinstance(connection_params, dict):
|
||||
self.__sentinel_json = connection_params
|
||||
self.__connection_string = None
|
||||
self.__printer("Received Sentinel JSON.")
|
||||
elif isinstance(connection_string, str):
|
||||
self._printer("Received Sentinel JSON.")
|
||||
elif isinstance(connection_params, str):
|
||||
try:
|
||||
self.__sentinel_json = json.from_string(connection_string)
|
||||
self.__sentinel_json = json.from_string(connection_params)
|
||||
self.__connection_string = None
|
||||
self.__printer("Seems like a Sentinel JSON String.")
|
||||
self._printer("Seems like a Sentinel JSON String.")
|
||||
except:
|
||||
self.__sentinel_json = None
|
||||
self.__connection_string = connection_string
|
||||
self.__printer("Seems like a regular Connection String.")
|
||||
|
||||
# ┳┓ ┓ •
|
||||
# ┃┃┏┓┣┓┓┏┏┓┏┓┓┏┓┏┓
|
||||
# ┻┛┗ ┗┛┗┻┗┫┗┫┗┛┗┗┫
|
||||
# ┛ ┛ ┛
|
||||
|
||||
def enable_debug(self):
|
||||
self.__printer.enable()
|
||||
|
||||
def disable_debug(self):
|
||||
self.__printer.disable()
|
||||
self.__connection_string = connection_params
|
||||
self._printer("Seems like a regular Connection String.")
|
||||
|
||||
# ┏┓ •
|
||||
# ┃ ┏┓┏┓┏┓┏┓┏╋┓┏┓┏┓
|
||||
@@ -174,7 +165,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
else: raise ValueError("Either a Sentinel JSON or a Connection String is needed.")
|
||||
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("CONN. ERR!", exception)
|
||||
return False
|
||||
|
||||
async def disconnect(self) -> bool:
|
||||
@@ -187,7 +178,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
if self.__client is not None:
|
||||
try: await self.__client.close()
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer(exception)
|
||||
return False
|
||||
return True
|
||||
return True
|
||||
@@ -210,7 +201,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
self.__requests_since_last_ping = 0
|
||||
return True
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer(exception)
|
||||
return await self.connect()
|
||||
else:
|
||||
self.__requests_since_last_ping += 1
|
||||
@@ -254,7 +245,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("LIST ERR!", exception)
|
||||
if raise_exception: raise
|
||||
else: return None
|
||||
|
||||
@@ -288,7 +279,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("TTL ERR!", key, exception)
|
||||
if raise_exception: raise
|
||||
else: return None
|
||||
|
||||
@@ -322,7 +313,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("SET ERR!", key, exception)
|
||||
if raise_exception: raise
|
||||
else: return False
|
||||
|
||||
@@ -353,7 +344,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("CACHE MISS!", key, exception)
|
||||
if raise_exception: raise
|
||||
else: return on_fail
|
||||
|
||||
@@ -381,7 +372,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("DEL ERR!", key, exception)
|
||||
if raise_exception: raise
|
||||
else: return False
|
||||
|
||||
@@ -409,7 +400,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
try:
|
||||
|
||||
# check if the key already exists,
|
||||
# Check if the key already exists,
|
||||
# regardless of that, increment the counter:
|
||||
already_existed = await self.__client.exists(key)
|
||||
new_value = await self.__client.incrby(key, value)
|
||||
@@ -422,7 +413,7 @@ class AsyncRedisCache(AsyncCachingBase):
|
||||
|
||||
# If an exception occurs in th eprocess:
|
||||
except Exception as exception:
|
||||
self.__printer(exception)
|
||||
self._printer("COUNT ERR!", key, value, exception)
|
||||
if raise_exception: raise
|
||||
else: return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user