2022-07-26 10:07:31 +00:00
|
|
|
from typing import List
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.base_service import BaseService
|
|
|
|
from core.dependence.redisdb import RedisDB
|
|
|
|
|
2023-05-02 16:16:14 +00:00
|
|
|
__all__ = ["GameCache", "GameCacheForStrategy", "GameCacheForMaterial", "GameCacheForLightCone"]
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
2022-08-31 06:48:15 +00:00
|
|
|
class GameCache:
|
2023-03-14 01:27:22 +00:00
|
|
|
qname: str
|
|
|
|
|
|
|
|
def __init__(self, redis: RedisDB, ttl: int = 3600):
|
2022-07-26 10:07:31 +00:00
|
|
|
self.client = redis.client
|
|
|
|
self.ttl = ttl
|
|
|
|
|
|
|
|
async def get_url_list(self, character_name: str):
|
|
|
|
qname = f"{self.qname}:{character_name}"
|
2022-11-20 05:59:16 +00:00
|
|
|
return [str(str_data, encoding="utf-8") for str_data in await self.client.lrange(qname, 0, -1)][::-1]
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
async def set_url_list(self, character_name: str, str_list: List[str]):
|
|
|
|
qname = f"{self.qname}:{character_name}"
|
|
|
|
await self.client.ltrim(qname, 1, 0)
|
|
|
|
await self.client.lpush(qname, *str_list)
|
|
|
|
await self.client.expire(qname, self.ttl)
|
2022-11-20 05:59:16 +00:00
|
|
|
return await self.client.llen(qname)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
2023-04-26 08:48:05 +00:00
|
|
|
async def get_file(self, character_name: str):
|
|
|
|
qname = f"{self.qname}:{character_name}"
|
|
|
|
return await self.client.get(qname)
|
|
|
|
|
|
|
|
async def set_file(self, character_name: str, file: str):
|
|
|
|
qname = f"{self.qname}:{character_name}"
|
|
|
|
await self.client.set(qname, file)
|
|
|
|
await self.client.expire(qname, self.ttl)
|
2023-05-02 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GameCacheForStrategy(BaseService.Component, GameCache):
|
|
|
|
qname = "game:strategy"
|
|
|
|
|
|
|
|
|
|
|
|
class GameCacheForMaterial(BaseService.Component, GameCache):
|
|
|
|
qname = "game:material"
|
|
|
|
|
|
|
|
|
|
|
|
class GameCacheForLightCone(BaseService.Component, GameCache):
|
|
|
|
qname = "game:lightcone"
|