Files
JayuCloud/JY-Artist-WeChat/utils/cache.js
T

153 lines
4.8 KiB
JavaScript
Raw Normal View History

2026-05-23 18:45:41 +08:00
/**
* 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:'
2026-05-23 18:45:41 +08:00
/**
* 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 = {}
2026-05-23 18:45:41 +08:00
/**
* 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 = {}
2026-05-23 18:45:41 +08:00
/**
* 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()
}
2026-05-23 18:45:41 +08:00
/**
* 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}`
}
2026-05-23 18:45:41 +08:00
/**
* 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) {}
}
}
2026-05-23 18:45:41 +08:00
/**
* 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
}
2026-05-23 18:45:41 +08:00
/**
* 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
}
2026-05-23 18:45:41 +08:00
/**
* 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) {}
}
2026-05-23 18:45:41 +08:00
/**
* 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) {}
}
})
}
2026-05-23 18:45:41 +08:00
/**
* 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
}