Caching will now be used from libbot

This commit is contained in:
2025-02-16 17:44:13 +01:00
parent afbb36e182
commit 95aecd3c99
10 changed files with 21 additions and 312 deletions

View File

@@ -1,13 +1,13 @@
import logging
from logging import Logger
from typing import Any, Dict
from typing import Any, Dict, Optional
from bson import ObjectId
from discord import User, Member
from libbot.cache.classes import Cache
from libbot.utils import config_get
from pymongo.results import InsertOneResult
from classes.cache import HoloCache
from errors import UserNotFoundError
from modules.database import col_users
@@ -33,14 +33,14 @@ class HoloUser:
cls,
user: User | Member,
allow_creation: bool = True,
cache: HoloCache | None = None,
cache: Optional[Cache] = None,
) -> "HoloUser":
"""Get an object that has a proper binding between Discord ID and database
### Args:
* `user` (User | Member): Object from which an ID can be extracted
* `allow_creation` (bool, optional): Whether to allow creation of a new user record if none found. Defaults to True.
* `cache` (HoloCache | None, optional): Cache engine to get the cache from
* `cache` (Cache, optional): Cache engine to get the cache from
### Raises:
* `UserNotFoundError`: User with such ID does not seem to exist in database
@@ -72,13 +72,13 @@ class HoloUser:
async def from_id(cls, user_id: int) -> "HoloUser":
raise NotImplementedError()
async def _set(self, key: str, value: Any, cache: HoloCache | None = None) -> None:
async def _set(self, key: str, value: Any, cache: Optional[Cache] = None) -> None:
"""Set attribute data and save it into the database
### Args:
* `key` (str): Attribute to be changed
* `value` (Any): Value to set
* `cache` (HoloCache | None, optional): Cache engine to write the update into
* `cache` (Cache, optional): Cache engine to write the update into
"""
if not hasattr(self, key):
raise AttributeError()
@@ -93,12 +93,12 @@ class HoloUser:
logger.info("Set attribute '%s' of user %s to '%s'", key, self.id, value)
async def _remove(self, key: str, cache: HoloCache | None = None) -> None:
async def _remove(self, key: str, cache: Optional[Cache] = None) -> None:
"""Remove attribute data and save it into the database
### Args:
* `key` (str): Attribute to be removed
* `cache` (HoloCache | None, optional): Cache engine to write the update into
* `cache` (Cache, optional): Cache engine to write the update into
"""
if not hasattr(self, key):
raise AttributeError()
@@ -118,7 +118,7 @@ class HoloUser:
def _get_cache_key(self) -> str:
return f"user_{self.id}"
def _update_cache(self, cache: HoloCache | None = None) -> None:
def _update_cache(self, cache: Optional[Cache] = None) -> None:
if cache is None:
return
@@ -129,7 +129,7 @@ class HoloUser:
else:
self._delete_cache(cache)
def _delete_cache(self, cache: HoloCache | None = None) -> None:
def _delete_cache(self, cache: Optional[Cache] = None) -> None:
if cache is None:
return
@@ -159,26 +159,26 @@ class HoloUser:
}
async def set_custom_channel(
self, channel_id: int, cache: HoloCache | None = None
self, channel_id: int, cache: Optional[Cache] = None
) -> None:
await self._set("custom_channel", channel_id, cache=cache)
async def set_custom_role(
self, role_id: int, cache: HoloCache | None = None
self, role_id: int, cache: Optional[Cache] = None
) -> None:
await self._set("custom_role", role_id, cache=cache)
async def remove_custom_channel(self, cache: HoloCache | None = None) -> None:
async def remove_custom_channel(self, cache: Optional[Cache] = None) -> None:
await self._remove("custom_channel", cache=cache)
async def remove_custom_role(self, cache: HoloCache | None = None) -> None:
async def remove_custom_role(self, cache: Optional[Cache] = None) -> None:
await self._remove("custom_role", cache=cache)
async def purge(self, cache: HoloCache | None = None) -> None:
async def purge(self, cache: Optional[Cache] = None) -> None:
"""Completely remove user data from database. Only removes the user record from users collection.
### Args:
* `cache` (HoloCache | None, optional): Cache engine to write the update into
* `cache` (Cache, optional): Cache engine to write the update into
"""
await col_users.delete_one({"_id": self._id})
self._delete_cache(cache)