Moved create_cache_client() to cache_manager

This commit is contained in:
Profitroll 2025-02-09 23:45:18 +01:00
parent cda570eb37
commit 9e9b6bc7dc
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
3 changed files with 31 additions and 30 deletions

View File

@ -5,7 +5,7 @@ from libbot.pycord.classes import PycordBot
from classes.cache.holo_cache_memcached import HoloCacheMemcached from classes.cache.holo_cache_memcached import HoloCacheMemcached
from classes.cache.holo_cache_redis import HoloCacheRedis from classes.cache.holo_cache_redis import HoloCacheRedis
from modules.cache_utils import create_cache_client from modules.cache_manager import create_cache_client
logger: Logger = logging.getLogger(__name__) logger: Logger = logging.getLogger(__name__)

29
modules/cache_manager.py Normal file
View File

@ -0,0 +1,29 @@
from typing import Dict, Any, Literal
from classes.cache.holo_cache_memcached import HoloCacheMemcached
from classes.cache.holo_cache_redis import HoloCacheRedis
def create_cache_client(
config: Dict[str, Any],
engine: Literal["memcached", "redis"] | None = None,
) -> HoloCacheMemcached | HoloCacheRedis:
if engine not in ["memcached", "redis"] or engine is None:
raise KeyError(
f"Incorrect cache engine provided. Expected 'memcached' or 'redis', got '{engine}'"
)
if "cache" not in config or engine not in config["cache"]:
raise KeyError(
f"Cache configuration is invalid. Please check if all keys are set (engine: '{engine}')"
)
match engine:
case "memcached":
return HoloCacheMemcached.from_config(config["cache"][engine])
case "redis":
return HoloCacheRedis.from_config(config["cache"][engine])
case _:
raise KeyError(
f"Cache implementation for the engine '{engine}' is not present."
)

View File

@ -1,37 +1,9 @@
from copy import deepcopy from copy import deepcopy
from typing import Dict, Any, Literal from typing import Any
from bson import ObjectId from bson import ObjectId
from ujson import dumps, loads from ujson import dumps, loads
from classes.cache.holo_cache_memcached import HoloCacheMemcached
from classes.cache.holo_cache_redis import HoloCacheRedis
def create_cache_client(
config: Dict[str, Any],
engine: Literal["memcached", "redis"] | None = None,
) -> HoloCacheMemcached | HoloCacheRedis:
if engine not in ["memcached", "redis"] or engine is None:
raise KeyError(
f"Incorrect cache engine provided. Expected 'memcached' or 'redis', got '{engine}'"
)
if "cache" not in config or engine not in config["cache"]:
raise KeyError(
f"Cache configuration is invalid. Please check if all keys are set (engine: '{engine}')"
)
match engine:
case "memcached":
return HoloCacheMemcached.from_config(config["cache"][engine])
case "redis":
return HoloCacheRedis.from_config(config["cache"][engine])
case _:
raise KeyError(
f"Cache implementation for the engine '{engine}' is not present."
)
def json_to_string(json_object: Any) -> str: def json_to_string(json_object: Any) -> str:
json_object_copy: Any = deepcopy(json_object) json_object_copy: Any = deepcopy(json_object)