bindbox-mini/utils/cache.js

174 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 缓存管理工具
*/
const REWARD_CACHE_KEY = 'reward_cache_v2' // v2: 修复概率精度问题
const MATCHING_GAME_CACHE_KEY = 'matching_game_cache_v1'
/**
* 判断缓存是否新鲜
* @param {number} timestamp - 缓存时间戳
* @param {number} ttl - 有效期毫秒默认24小时
* @returns {boolean}
*/
export function isFresh(timestamp, ttl = 24 * 60 * 60 * 1000) {
const now = Date.now()
const v = Number(timestamp || 0)
return now - v < ttl
}
/**
* 获取奖励缓存
* @returns {Object}
*/
export function getRewardCache() {
const obj = uni.getStorageSync(REWARD_CACHE_KEY) || {}
return typeof obj === 'object' && obj ? obj : {}
}
/**
* 设置奖励缓存
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
* @param {any} value - 缓存值
*/
export function setRewardCache(activityId, issueId, value) {
const cache = getRewardCache()
const act = cache[activityId] || {}
act[issueId] = { value, ts: Date.now() }
cache[activityId] = act
uni.setStorageSync(REWARD_CACHE_KEY, cache)
}
/**
* 获取奖励缓存项
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
* @returns {any|null}
*/
export function getRewardCacheItem(activityId, issueId) {
const cache = getRewardCache()
const act = cache[activityId] || {}
const c = act[issueId]
if (c && isFresh(c.ts) && Array.isArray(c.value)) {
return c.value
}
return null
}
/**
* 获取对对碰游戏缓存
* @returns {Object}
*/
export function getMatchingGameCache() {
const obj = uni.getStorageSync(MATCHING_GAME_CACHE_KEY) || {}
return typeof obj === 'object' && obj ? obj : {}
}
/**
* 读取对对碰游戏缓存项
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
* @returns {Object|null}
*/
export function readMatchingGameCacheEntry(activityId, issueId) {
const activityKey = String(activityId || '')
const issueKey = String(issueId || '')
if (!activityKey || !issueKey) return null
const cache = getMatchingGameCache()
const act = cache[activityKey] || {}
const entry = act && act[issueKey]
const ok = entry && typeof entry === 'object' && entry.game_id
return ok ? entry : null
}
/**
* 写入对对碰游戏缓存项
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
* @param {Object} entry - 缓存数据
*/
export function writeMatchingGameCacheEntry(activityId, issueId, entry) {
const activityKey = String(activityId || '')
const issueKey = String(issueId || '')
if (!activityKey || !issueKey) return
const cache = getMatchingGameCache()
// 清理超过170秒的缓存记录
const now = Date.now()
const TTL = 170 * 1000 // 170秒
// 遍历所有活动的所有期,删除过期的缓存
for (const actKey in cache) {
const act = cache[actKey]
if (!act || typeof act !== 'object') continue
for (const issKey in act) {
const entry = act[issKey]
if (!entry || typeof entry !== 'object') continue
const ts = Number(entry.ts || 0)
if (now - ts >= TTL) {
// 超过170秒删除此缓存记录
delete act[issKey]
}
}
// 如果该活动下没有任何期了,删除活动
if (Object.keys(act).length === 0) {
delete cache[actKey]
}
}
// 写入新的缓存
const act = (cache[activityKey] && typeof cache[activityKey] === 'object') ? cache[activityKey] : {}
act[issueKey] = entry
cache[activityKey] = act
uni.setStorageSync(MATCHING_GAME_CACHE_KEY, cache)
}
/**
* 清除对对碰游戏缓存项
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
*/
export function clearMatchingGameCacheEntry(activityId, issueId) {
const activityKey = String(activityId || '')
const issueKey = String(issueId || '')
const cache = getMatchingGameCache()
const act = cache[activityKey]
if (!act || typeof act !== 'object') return
if (act[issueKey] !== undefined) delete act[issueKey]
if (Object.keys(act).length === 0) delete cache[activityKey]
else cache[activityKey] = act
uni.setStorageSync(MATCHING_GAME_CACHE_KEY, cache)
}
/**
* 查找最新的对对碰游戏缓存
* @param {string} activityId - 活动ID
* @returns {Object|null}
*/
export function findLatestMatchingGameCacheEntry(activityId) {
const activityKey = String(activityId || '')
if (!activityKey) return null
const cache = getMatchingGameCache()
const act = cache[activityKey]
if (!act || typeof act !== 'object') return null
let bestIssueId = ''
let bestEntry = null
let bestTs = -Infinity
Object.keys(act).forEach(issueId => {
const entry = act[issueId]
if (!entry || typeof entry !== 'object' || !entry.game_id) return
const ts = Number(entry.ts || 0)
if (!bestEntry || ts > bestTs) {
bestTs = ts
bestIssueId = issueId
bestEntry = entry
}
})
if (!bestEntry) return null
return { issue_id: bestIssueId, entry: bestEntry }
}