Files
JayuCloud/JY-Artist-WeChat/admin/utils/cache.js
T
2026-06-29 23:26:43 +08:00

120 lines
3.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 = 'adminMiniCache:'
/**
* 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: 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: 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: 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 memoryEntry = memoryCache[key]
if (memoryEntry && (!memoryEntry.expireAt || memoryEntry.expireAt > now())) {
return memoryEntry.value
}
delete memoryCache[key]
try {
const storedEntry = wx.getStorageSync(buildKey(key))
if (storedEntry && (!storedEntry.expireAt || storedEntry.expireAt > now())) {
memoryCache[key] = storedEntry
return storedEntry.value
}
wx.removeStorageSync(buildKey(key))
} 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 entry = {
value,
expireAt: ttlMs && ttlMs > 0 ? now() + ttlMs : 0
}
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: 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,
remember
}