Files

35 lines
844 B
TypeScript
Raw Permalink Normal View History

2026-04-30 00:13:37 +08:00
import { sys } from "cc";
export default class Utils {
/**
*
* @param {key} key
* @param {} value
* @param {} expire
*/
static setCache(key: string, value: any, expire: number = 0): void {
let obj = {
data: value, //存储的数据
time: Date.now() / 1000, //记录存储的时间戳
expire: expire, //记录过期时间,单位秒
};
sys.localStorage.setItem(key, JSON.stringify(obj));
}
/**
*
* @param {key} key
*/
static getCache(key: string): any {
let val: any = sys.localStorage.getItem(key);
if (!val) {
return null;
}
val = JSON.parse(val);
if (val.expire && Date.now() / 1000 - val.time > val.expire) {
sys.localStorage.removeItem(key);
return null;
}
return val.data;
}
}