import config from '@/config/index.js'; const __NAME__ = config.name || 'MA'; /** * 缓存数据优化 * var cache = require('utils/cache.js'); * import cache from '../cache' * 使用方法 【 * 一、设置缓存 * string cache.put('k', 'string你好啊'); * json cache.put('k', { "b": "3" }, 2); * array cache.put('k', [1, 2, 3]); * boolean cache.put('k', true); * 二、读取缓存 * 默认值 cache.get('k') * string cache.get('k', '你好') * json cache.get('k', { "a": "1" }) * 三、移除/清理 * 移除: cache.remove('k'); * 清理:cache.clear(); * 】 * @type {String} */ const prefix = config.env === 'release' ? `${__NAME__.toUpperCase()}_` : `${__NAME__.toUpperCase()}_${`${config.env}`.toUpperCase()}_`; const postfix = '_SEED'; /** * @description 设置缓存 * @param {String} k [键名] * @param {Any} v [键值] * @param {Number} t [时间、单位秒] */ function put(k, v, t) { uni.setStorageSync(prefix + k, v) var seconds = parseInt(t); if (seconds > 0) { var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000 + seconds; uni.setStorageSync(prefix + k + postfix, timestamp + "") } else { uni.removeStorageSync(prefix + k + postfix) } } /** * 获取缓存 * @param {String} k [键名] * @param {Any} def [获取为空时默认] */ function get(k, def) { var deadtime = parseInt(uni.getStorageSync(prefix + k + postfix)) if (deadtime) { if (parseInt(deadtime) < Date.parse(new Date()) / 1000) { if (def) { return def; } else { return false; } } } var res = uni.getStorageSync(prefix + k); if (res) { return res; } else { if (def == undefined || def == "") { def = false; } return def; } } /** * @description 删除缓存 * @param {String} k 键值 */ function remove(k) { uni.removeStorageSync(prefix + k); uni.removeStorageSync(prefix + k + postfix); } /** * @description 清理所有缓存 */ function clear() { uni.clearStorageSync(); } module.exports = { put: put, get: get, remove: remove, clear: clear, }