|
|
|
@ -6,16 +6,31 @@ interface CacheWrapper<T> {
|
|
|
|
|
|
|
|
|
|
interface Caches<T> {
|
|
|
|
|
total_page:number
|
|
|
|
|
list:{[page:number]:T[]}
|
|
|
|
|
list:{[page:number]:T[]},
|
|
|
|
|
cache_created_time:Date
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|