diff --git a/src/constant.ts b/src/constant.ts index abce746..56d766d 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -5,3 +5,5 @@ export const LANGUAGE_COLOR: {[key: string]: string} = { "Java": "#3478CD", "Default": "#333333" } + +export const HALO_RENDER_CACHE_VERSION = "v1.1.0-1" diff --git a/src/utils/git-processor/git-processor.ts b/src/utils/git-processor/git-processor.ts index 2e12cd1..e9d4033 100644 --- a/src/utils/git-processor/git-processor.ts +++ b/src/utils/git-processor/git-processor.ts @@ -1,5 +1,5 @@ import {getGitHubInfo} from "./github-utils"; -import {LANGUAGE_COLOR, RENDER_CLASS} from "../../constant"; +import {HALO_RENDER_CACHE_VERSION, LANGUAGE_COLOR, RENDER_CLASS} from "../../constant"; export const cache: {[key: string]: RepoInfo|-1} = {} @@ -28,11 +28,21 @@ export function fillContent(id: string, // 检测本地缓存 const storageCache = localStorage.getItem(`git_cache:${id}`) if (storageCache) { - return fill(id, (JSON.parse(storageCache))) + const cacheJson: RepoInfo = JSON.parse(storageCache) + // 检查版本与过期时间 + if (cacheJson.version === HALO_RENDER_CACHE_VERSION && + new Date() < new Date(cacheJson.expireTime||0)) + return fill(id, cacheJson) + localStorage.removeItem(`git_cache:${id}`) } cache[id] = -1 utils[func](args).then(r => { cache[id] = r + // 加入缓存版本 + r.version = HALO_RENDER_CACHE_VERSION + // 加入过期时间 + r.expireTime = new Date().setTime(new Date().getTime() + 30*60*1000) + // 存储缓存 localStorage.setItem(`git_cache:${id}`, JSON.stringify(r)) fill(id, r) document.querySelectorAll("." + id).forEach(el => { @@ -68,6 +78,7 @@ export declare interface IGitArgs { } export declare interface RepoInfo { + version?: string platform?: string name?: string owner?: string @@ -80,4 +91,6 @@ export declare interface RepoInfo { description?: string language?: string lastPush?: string + topic?: Array + expireTime?: number }