From 04e15721c99192871420cae19dcb7fb072334601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E7=82=BD=E9=94=AE?= <397201698@qq.com> Date: Tue, 27 Jul 2021 16:53:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E6=89=BE=E6=9C=80=E6=97=A7=E4=B8=80?= =?UTF-8?q?=E6=9D=A1=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/cache/RepositorySearch.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/utils/cache/RepositorySearch.ts b/src/utils/cache/RepositorySearch.ts index fbadd3b..d88b41c 100755 --- a/src/utils/cache/RepositorySearch.ts +++ b/src/utils/cache/RepositorySearch.ts @@ -6,16 +6,31 @@ interface CacheWrapper { interface Caches { total_page:number - list:{[page:number]:T[]} + list:{[page:number]:T[]}, + cache_created_time:Date } const cache:CacheWrapper = {} +/** + * 查找最旧一条缓存 + * @returns + */ +function findOldestCache () { + const cache_sort = Object.entries(cache).sort((a, b) => a[1].cache_created_time.getTime() - b[1].cache_created_time.getTime()) + const oldest = cache_sort.shift()! + return { + page: parseInt(oldest[0], 10), + cache: oldest[1] + } +} + export function RepositorySearchCache (keyword:string, max_cache_count:number) { if (!cache[keyword]) { cache[keyword] = { list: [], - total_page: 0 + total_page: 0, + cache_created_time: new Date() } } @@ -42,7 +57,7 @@ export function RepositorySearchCache (keyword:string, max_cache_count:number) { add (page:number, data:RepositorySearchResultItem[]) { if (!_cache_list[page] && this.getCount() >= max_cache_count) { this.remove( - parseInt(Object.keys(_cache_list).shift()!, 10) + findOldestCache().page ) } _cache_list[page] = data