查找最旧一条缓存

This commit is contained in:
周炽键 2021-07-27 16:53:45 +08:00
parent 5817d0d9af
commit 04e15721c9
1 changed files with 18 additions and 3 deletions

View File

@ -6,16 +6,31 @@ interface CacheWrapper<T> {
interface Caches<T> { interface Caches<T> {
total_page:number total_page:number
list:{[page:number]:T[]} list:{[page:number]:T[]},
cache_created_time:Date
} }
const cache:CacheWrapper<RepositorySearchResultItem> = {} const cache:CacheWrapper<RepositorySearchResultItem> = {}
/**
*
* @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) { export function RepositorySearchCache (keyword:string, max_cache_count:number) {
if (!cache[keyword]) { if (!cache[keyword]) {
cache[keyword] = { cache[keyword] = {
list: [], 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[]) { add (page:number, data:RepositorySearchResultItem[]) {
if (!_cache_list[page] && this.getCount() >= max_cache_count) { if (!_cache_list[page] && this.getCount() >= max_cache_count) {
this.remove( this.remove(
parseInt(Object.keys(_cache_list).shift()!, 10) findOldestCache().page
) )
} }
_cache_list[page] = data _cache_list[page] = data