-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
56 lines (54 loc) · 1.47 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @example
* let fetchRes: FetchRes<T> = () => requestArticle(url)
* let cachePrefix = 'last:article/view:'
* let cacheKey = `${cachePrefix}${url}`
* let getCache: GetCache<T> = async () => {
* return st.getStorage(cacheKey)
* }
* let doClean = keys => {} // 同类缓存清理策略
* let setCache: SetCache<T> = async res => {
* return st.setStorage(cacheKey, res, doClean)
* }
* return useCacheFirst<Res>({ getCache, setCache, fetchRes })
*/
export async function useCacheFirst<T = any>({
setCache,
getCache,
fetchRes,
duration,
throttle,
preferCacheOnExpire,
}: {
setCache: SetCache<T>,
getCache: GetCache<T>,
fetchRes: FetchRes<T>,
duration?: number,
throttle?: number,
preferCacheOnExpire?: boolean,
}) {
if (duration == null) { duration = Infinity }
if (throttle == null) { throttle = -1 }
const fetchAndCache: FetchRes<T> = async (silent) => {
const res = await fetchRes(silent)
const cachedAt = Date.now()
setCache({ res, cachedAt }) // 仅触发
return res
}
const cache = await getCache()
if (cache != null) {
const { res, cachedAt } = cache
const cachedFor = Date.now() - cachedAt
const isExpired = cachedFor > duration
const useCache = !isExpired || preferCacheOnExpire
const inThrottle = cachedFor < throttle
const invokeFetch = !inThrottle
if (useCache) {
if (invokeFetch) {
fetchAndCache(true) // 仅触发
}
return res
}
}
return fetchAndCache(false)
}