Skip to content

Commit

Permalink
scripts: add ttl for AssetData in RequestCache
Browse files Browse the repository at this point in the history
GitHub API is not that stable
  • Loading branch information
Fallen-Breath committed Jan 16, 2025
1 parent 21dfef5 commit d0d026d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions scripts/meta/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ def from_response(cls, data: list, etag: str) -> 'RepositoryResponse':
return cls(etag=etag, data=cls.encode_json(data))


ASSET_DATA_DEFAULT_TTL = 100


class AssetData(Serializable):
meta: MetaInfo
size: int
hash_md5: str
hash_sha256: str

# We don't want to delete the AssetData from RequestCache too fast,
# in case of GitHub API error and return a release list with length 0,
# or asset re-download will happen
# so here's a ttl -- delete the asset data if ttl reaches 0
ttl: int = ASSET_DATA_DEFAULT_TTL


class RequestCache(Serializable):
"""
Expand Down
12 changes: 9 additions & 3 deletions scripts/plugin/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from common import constants, log
from common.report import reporter
from meta.cache import RequestCache, ReleasePageResponse, RepositoryResponse, AssetData
from meta.cache import RequestCache, ReleasePageResponse, RepositoryResponse, AssetData, ASSET_DATA_DEFAULT_TTL
from meta.plugin import MetaInfo
from utils import request_utils, file_utils

Expand Down Expand Up @@ -44,8 +44,14 @@ def dump_for_save(self) -> dict:
if page not in self.__used_release_page:
cache.release_pages.pop(page)
for asset_id in list(cache.asset_data.keys()):
if asset_id not in self.__used_asset_data:
cache.asset_data.pop(asset_id)
asset_data = cache.asset_data[asset_id]
if asset_id in self.__used_asset_data:
asset_data.ttl = ASSET_DATA_DEFAULT_TTL
else:
asset_data.ttl -= 1
if asset_data.ttl <= 0:
cache.asset_data.pop(asset_id)
log.info('Deleted asset data {!r} from cache'.format(asset_data))
return cache.serialize()

async def fetch_release_page(self, page: int, per_page: int) -> ReleasePageResponse:
Expand Down

0 comments on commit d0d026d

Please sign in to comment.