🐛 Fix material cache error

This commit is contained in:
omg-xtao 2022-11-20 13:59:16 +08:00 committed by GitHub
parent e83a9fd8cb
commit d096cd29a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -11,12 +11,11 @@ class GameCache:
async def get_url_list(self, character_name: str): async def get_url_list(self, character_name: str):
qname = f"{self.qname}:{character_name}" qname = f"{self.qname}:{character_name}"
return [str(str_data, encoding="utf-8") for str_data in await self.client.lrange(qname, 0, -1)] return [str(str_data, encoding="utf-8") for str_data in await self.client.lrange(qname, 0, -1)][::-1]
async def set_url_list(self, character_name: str, str_list: List[str]): async def set_url_list(self, character_name: str, str_list: List[str]):
qname = f"{self.qname}:{character_name}" qname = f"{self.qname}:{character_name}"
await self.client.ltrim(qname, 1, 0) await self.client.ltrim(qname, 1, 0)
await self.client.lpush(qname, *str_list) await self.client.lpush(qname, *str_list)
await self.client.expire(qname, self.ttl) await self.client.expire(qname, self.ttl)
count = await self.client.llen(qname) return await self.client.llen(qname)
return count

View File

@ -12,8 +12,11 @@ class GameStrategyService:
self._collections = [839176, 839179, 839181, 1180811] self._collections = [839176, 839179, 839181, 1180811]
else: else:
self._collections = collections self._collections = collections
self._special_posts = {"达达利亚": "21272578"}
async def _get_strategy_from_hyperion(self, collection_id: int, character_name: str) -> int: async def _get_strategy_from_hyperion(self, collection_id: int, character_name: str) -> int:
if character_name in self._special_posts:
return self._special_posts[character_name]
post_id: int = -1 post_id: int = -1
post_full_in_collection = await self._hyperion.get_post_full_in_collection(collection_id) post_full_in_collection = await self._hyperion.get_post_full_in_collection(collection_id)
for post_data in post_full_in_collection["posts"]: for post_data in post_full_in_collection["posts"]:
@ -32,7 +35,7 @@ class GameStrategyService:
async def get_strategy(self, character_name: str) -> str: async def get_strategy(self, character_name: str) -> str:
cache = await self._cache.get_url_list(character_name) cache = await self._cache.get_url_list(character_name)
if len(cache) >= 1: if len(cache) >= 1:
return cache[-1] return cache[0]
for collection_id in self._collections: for collection_id in self._collections:
post_id = await self._get_strategy_from_hyperion(collection_id, character_name) post_id = await self._get_strategy_from_hyperion(collection_id, character_name)
@ -74,18 +77,18 @@ class GameMaterialService:
async def get_material(self, character_name: str) -> str: async def get_material(self, character_name: str) -> str:
cache = await self._cache.get_url_list(character_name) cache = await self._cache.get_url_list(character_name)
if len(cache) >= 1: if len(cache) >= 1:
return cache[-1] image_url_list = cache
for collection_id in self._collections:
post_id = await self._get_material_from_hyperion(collection_id, character_name)
if post_id != -1:
break
else: else:
return "" for collection_id in self._collections:
post_id = await self._get_material_from_hyperion(collection_id, character_name)
if post_id != -1:
break
else:
return ""
artwork_info = await self._hyperion.get_post_info(2, post_id) artwork_info = await self._hyperion.get_post_info(2, post_id)
await self._cache.set_url_list(character_name, artwork_info.image_urls) image_url_list = artwork_info.image_urls
image_url_list = artwork_info.image_urls await self._cache.set_url_list(character_name, image_url_list)
if len(image_url_list) == 0: if len(image_url_list) == 0:
return "" return ""
elif len(image_url_list) == 1: elif len(image_url_list) == 1: