153 lines
4.8 KiB
JavaScript
153 lines
4.8 KiB
JavaScript
/**
|
|
* Purpose: CACHE_PREFIX supports cache.js by holding or executing the cache prefix page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const CACHE_PREFIX = 'miniCache:'
|
|
/**
|
|
* Purpose: memoryCache supports cache.js by holding or executing the memory cache page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const memoryCache = {}
|
|
/**
|
|
* Purpose: inflightCache supports cache.js by holding or executing the inflight cache page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const inflightCache = {}
|
|
|
|
/**
|
|
* Purpose: now supports cache.js by holding or executing the now page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function now() {
|
|
return Date.now()
|
|
}
|
|
|
|
/**
|
|
* Purpose: buildKey supports cache.js by holding or executing the build key page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function buildKey(key) {
|
|
return `${CACHE_PREFIX}${key}`
|
|
}
|
|
|
|
/**
|
|
* Purpose: clearExpiredEntry supports cache.js by holding or executing the clear expired entry page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function clearExpiredEntry(key, entry) {
|
|
if (!entry) {
|
|
return
|
|
}
|
|
if (entry.expireAt && entry.expireAt <= now()) {
|
|
delete memoryCache[key]
|
|
try {
|
|
wx.removeStorageSync(buildKey(key))
|
|
} catch (error) {}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purpose: getCache supports cache.js by holding or executing the get cache page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function getCache(key, defaultValue) {
|
|
if (!key) {
|
|
return defaultValue
|
|
}
|
|
|
|
const localEntry = memoryCache[key]
|
|
if (localEntry && (!localEntry.expireAt || localEntry.expireAt > now())) {
|
|
return localEntry.value
|
|
}
|
|
clearExpiredEntry(key, localEntry)
|
|
|
|
try {
|
|
const storedEntry = wx.getStorageSync(buildKey(key))
|
|
if (storedEntry && (!storedEntry.expireAt || storedEntry.expireAt > now())) {
|
|
memoryCache[key] = storedEntry
|
|
return storedEntry.value
|
|
}
|
|
clearExpiredEntry(key, storedEntry)
|
|
} catch (error) {}
|
|
|
|
return defaultValue
|
|
}
|
|
|
|
/**
|
|
* Purpose: setCache supports cache.js by holding or executing the set cache page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function setCache(key, value, ttlMs) {
|
|
if (!key) {
|
|
return value
|
|
}
|
|
const expireAt = ttlMs && ttlMs > 0 ? now() + ttlMs : 0
|
|
const entry = {
|
|
value,
|
|
expireAt
|
|
}
|
|
memoryCache[key] = entry
|
|
try {
|
|
wx.setStorageSync(buildKey(key), entry)
|
|
} catch (error) {}
|
|
return value
|
|
}
|
|
|
|
/**
|
|
* Purpose: removeCache supports cache.js by holding or executing the remove cache page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function removeCache(key) {
|
|
if (!key) {
|
|
return
|
|
}
|
|
delete memoryCache[key]
|
|
try {
|
|
wx.removeStorageSync(buildKey(key))
|
|
} catch (error) {}
|
|
}
|
|
|
|
/**
|
|
* Purpose: removeByPrefix supports cache.js by holding or executing the remove by prefix page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function removeByPrefix(prefix) {
|
|
Object.keys(memoryCache).forEach(key => {
|
|
if (key.startsWith(prefix)) {
|
|
delete memoryCache[key]
|
|
try {
|
|
wx.removeStorageSync(buildKey(key))
|
|
} catch (error) {}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Purpose: remember supports cache.js by holding or executing the remember page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function remember(key, ttlMs, loader) {
|
|
const cached = getCache(key, undefined)
|
|
if (cached !== undefined) {
|
|
return Promise.resolve(cached)
|
|
}
|
|
if (inflightCache[key]) {
|
|
return inflightCache[key]
|
|
}
|
|
inflightCache[key] = Promise.resolve()
|
|
.then(() => loader())
|
|
.then(value => setCache(key, value, ttlMs))
|
|
.finally(() => {
|
|
delete inflightCache[key]
|
|
})
|
|
return inflightCache[key]
|
|
}
|
|
|
|
module.exports = {
|
|
getCache,
|
|
setCache,
|
|
removeCache,
|
|
removeByPrefix,
|
|
remember
|
|
}
|