平台介绍 >> js/jsfun/fnCache.js Cache缓存
js/jsfun/fnCache.js Cache缓存
//应用级全部缓存,可以存储任何数据,但不建议存储大量数据(会占用大量的内存),比Redis快(无需请求)
let key = "cache_key"
//删除缓存
fnCache_remove(key)
//读取缓存
let value = fnCache_get(key)
console.log("缓存不存在:", value)
//设置缓存
//字符串
fnCache_set(key, fnTime_now("Ymd"))
console.log("字符串:", fnCache_get(key), typeof fnCache_get(key))
//数字
fnCache_set(key, fnTime_timestamp())
console.log("数字:", fnCache_get(key), typeof fnCache_get(key))
//bool
fnCache_set(key, true)
console.log("bool:", fnCache_get(key), typeof fnCache_get(key))
//数组
fnCache_set(key, ["1", "2", "3"])
console.log("数组字符串", fnCache_get(key), typeof fnCache_get(key))
//数组
fnCache_set(key, [1, 2, 3])
console.log("数组数字:", fnCache_get(key), typeof fnCache_get(key))
//对象,设置超时10秒
let obj = {"time": fnTime_timestamp(), "title": "张三"}
fnCache_set(key, obj, 10)
console.log("对象:", fnCache_get(key), typeof fnCache_get(key))
console.log("缓存信息:", fnCache_getInfo(key))
//fnTime_sleep(1100)
console.log("对象:", fnCache_get(key), typeof fnCache_get(key))
//设置超时时间戳
let timeout = fnTime_timestamp() + 10
fnCache_set(key, obj, timeout)
console.log("缓存信息:", fnCache_getInfo(key))
//fnTime_sleep(1100)
console.log("对象2:", fnCache_get(key), "timeout:", timeout, "now:", fnTime_timestamp())
//缓存列表
fnCache_listRemove(key)
console.log("列表", fnCache_listGet(key))
//在列表最后一个元素,新增一个元素
fnCache_listPush(key, 1)
console.log("列表", fnCache_listGet(key), typeof fnCache_listGet(key))
fnCache_listPush(key, 3)
fnCache_listPush(key, 5)
console.log("列表", fnCache_listGet(key))
//插入元素
fnCache_listInsert(key, 4, 1)
console.log("列表插入:", fnCache_listGet(key))
//取得列表内容
let content = fnCache_listGet(key)
let listinfo = fnCache_listGetInfo(key)
console.log("列表", content, listinfo)
//弹出最后一个元素
console.log("列表 pop:", fnCache_listPop(key),"info:",fnCache_listGetInfo(key))
//删除一个元素
console.log("列表 delete:", fnCache_listDelete(key,2),"info:",fnCache_listGetInfo(key))
//弹出第一个元素
console.log("列表 shift:", fnCache_listShift(key))
console.log("列表", fnCache_listGet(key))