Improved caching and utils structure

This commit is contained in:
2025-04-22 23:22:01 +02:00
parent ec094f9a98
commit 5230ac9ace
15 changed files with 65 additions and 40 deletions

View File

@@ -7,8 +7,8 @@ from libbot.cache.classes import CacheMemcached, CacheRedis
from libbot.cache.manager import create_cache_client
from libbot.pycord.classes import PycordBot as LibPycordBot
from classes import PycordEvent, PycordGuild, PycordUser, PycordEventStage
from modules.logging_utils import get_logger
from classes import PycordEvent, PycordEventStage, PycordGuild, PycordUser
from modules.utils import get_logger
logger: Logger = get_logger(__name__)

View File

@@ -10,7 +10,7 @@ from libbot.cache.classes import Cache
from pymongo.results import InsertOneResult
from modules.database import col_events
from modules.logging_utils import get_logger
from modules.utils import get_logger, restore_from_cache
logger: Logger = get_logger(__name__)
@@ -59,11 +59,10 @@ class PycordEvent:
Raises:
EventNotFoundError: Event was not found
"""
if cache is not None:
cached_entry: Dict[str, Any] | None = cache.get_json(f"{cls.__short_name__}_{event_id}")
cached_entry: Dict[str, Any] | None = restore_from_cache(cls.__short_name__, event_id, cache=cache)
if cached_entry is not None:
return cls(**cached_entry)
if cached_entry is not None:
return cls(**cached_entry)
db_entry = await cls.__collection__.find_one(
{"_id": event_id if isinstance(event_id, ObjectId) else ObjectId(event_id)}
@@ -326,3 +325,11 @@ class PycordEvent:
await self._set(cache, stage_ids=self.stage_ids)
await self._update_event_stage_order(bot, old_stage_ids, cache=cache)
# # TODO Add documentation
# def get_localized_start_date(self, tz: str | timezone | ZoneInfo) -> datetime:
# return self.starts.replace(tzinfo=tz)
#
# # TODO Add documentation
# def get_localized_end_date(self, tz: str | timezone | ZoneInfo) -> datetime:
# return self.ends.replace(tzinfo=tz)

View File

@@ -1,7 +1,7 @@
from dataclasses import dataclass
from datetime import datetime
from logging import Logger
from typing import List, Dict, Any, Optional
from typing import Any, Dict, List, Optional
from zoneinfo import ZoneInfo
from bson import ObjectId
@@ -9,7 +9,7 @@ from libbot.cache.classes import Cache
from pymongo.results import InsertOneResult
from modules.database import col_stages
from modules.logging_utils import get_logger
from modules.utils import get_logger, restore_from_cache
logger: Logger = get_logger(__name__)
@@ -54,11 +54,10 @@ class PycordEventStage:
Raises:
EventStageNotFoundError: Event stage was not found
"""
if cache is not None:
cached_entry: Dict[str, Any] | None = cache.get_json(f"{cls.__short_name__}_{stage_id}")
cached_entry: Dict[str, Any] | None = restore_from_cache(cls.__short_name__, stage_id, cache=cache)
if cached_entry is not None:
return cls(**cached_entry)
if cached_entry is not None:
return cls(**cached_entry)
db_entry = await cls.__collection__.find_one(
{"_id": stage_id if isinstance(stage_id, ObjectId) else ObjectId(stage_id)}

View File

@@ -8,7 +8,7 @@ from pymongo.results import InsertOneResult
from classes.errors import GuildNotFoundError
from modules.database import col_guilds
from modules.logging_utils import get_logger
from modules.utils import get_logger, restore_from_cache
logger: Logger = get_logger(__name__)
@@ -45,11 +45,10 @@ class PycordGuild:
Raises:
GuildNotFoundError: User was not found and creation was not allowed
"""
if cache is not None:
cached_entry: Dict[str, Any] | None = cache.get_json(f"{cls.__short_name__}_{guild_id}")
cached_entry: Dict[str, Any] | None = restore_from_cache(cls.__short_name__, guild_id, cache=cache)
if cached_entry is not None:
return cls(**cached_entry)
if cached_entry is not None:
return cls(**cached_entry)
db_entry = await cls.__collection__.find_one({"id": guild_id})

View File

@@ -8,7 +8,7 @@ from pymongo.results import InsertOneResult
from classes.errors.pycord_user import UserNotFoundError
from modules.database import col_users
from modules.logging_utils import get_logger
from modules.utils import get_logger, restore_from_cache
logger: Logger = get_logger(__name__)
@@ -41,11 +41,10 @@ class PycordUser:
Raises:
UserNotFoundError: User was not found and creation was not allowed
"""
if cache is not None:
cached_entry: Dict[str, Any] | None = cache.get_json(f"{cls.__short_name__}_{user_id}")
cached_entry: Dict[str, Any] | None = restore_from_cache(cls.__short_name__, user_id, cache=cache)
if cached_entry is not None:
return cls(**cached_entry)
if cached_entry is not None:
return cls(**cached_entry)
db_entry = await cls.__collection__.find_one({"id": user_id})