1.0.2 #283 #284

Merged
edraft merged 3 commits from 1.0.2 into master 2023-03-29 23:06:51 +02:00
3 changed files with 41 additions and 2 deletions
Showing only changes of commit 2fecbf2c31 - Show all commits

View File

@ -27,6 +27,7 @@ from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC
from bot_data.service.api_key_repository_service import ApiKeyRepositoryService
from bot_data.service.auth_user_repository_service import AuthUserRepositoryService
from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService
from bot_data.service.cache_service import CacheService
from bot_data.service.client_repository_service import ClientRepositoryService
from bot_data.service.game_server_repository_service import GameServerRepositoryService
from bot_data.service.known_user_repository_service import KnownUserRepositoryService
@ -56,6 +57,8 @@ class DataModule(ModuleABC):
pass
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_singleton(CacheService)
services.add_transient(ApiKeyRepositoryABC, ApiKeyRepositoryService)
services.add_transient(AuthUserRepositoryABC, AuthUserRepositoryService)
services.add_transient(ServerRepositoryABC, ServerRepositoryService)

View File

@ -0,0 +1,16 @@
from cpl_query.extension import List
from bot_data.model.server import Server
class CacheService:
def __init__(self):
self._cached_server = List(Server)
@property
def cached_server(self) -> List[Server]:
return self._cached_server
@cached_server.setter
def cached_server(self, value: List[Server]):
self._cached_server = value

View File

@ -8,12 +8,14 @@ from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.filtered_result import FilteredResult
from bot_data.model.server import Server
from bot_data.service.cache_service import CacheService
class ServerRepositoryService(ServerRepositoryABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC, cache: CacheService):
self._logger = logger
self._context = db_context
self._cache = cache
ServerRepositoryABC.__init__(self)
@ -24,6 +26,7 @@ class ServerRepositoryService(ServerRepositoryABC):
for result in results:
servers.append(Server(result[1], result[2], result[3], id=result[0]))
self._cache.cached_server = List(Server, servers)
return servers
def get_filtered_servers(self, criteria: ServerSelectCriteria) -> FilteredResult:
@ -52,11 +55,23 @@ class ServerRepositoryService(ServerRepositoryABC):
return result
def get_server_by_id(self, server_id: int) -> Server:
cs = self._cache.cached_server.where(lambda x: x.id == server_id).single_or_default()
if cs is not None:
self._logger.error(__name__, f"CACHE")
return cs
self._logger.trace(__name__, f"Send SQL command: {Server.get_select_by_id_string(server_id)}")
result = self._context.select(Server.get_select_by_id_string(server_id))[0]
return Server(result[1], result[2], result[3], id=result[0])
server = Server(result[1], result[2], result[3], id=result[0])
self._cache.cached_server.add(server)
return server
def get_server_by_discord_id(self, discord_id: int) -> Server:
cs = self._cache.cached_server.where(lambda x: x.discord_id == discord_id).single_or_default()
if cs is not None:
self._logger.error(__name__, f"CACHE")
return cs
self._logger.trace(
__name__,
f"Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}",
@ -65,6 +80,11 @@ class ServerRepositoryService(ServerRepositoryABC):
return Server(result[1], result[2], result[3], id=result[0])
def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]:
cs = self._cache.cached_server.where(lambda x: x.discord_id == discord_id).single_or_default()
if cs is not None:
self._logger.error(__name__, f"CACHE")
return cs
self._logger.trace(
__name__,
f"Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}",