1 Star 1 Fork 0

Zn/web-storage-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cache.js 860 Bytes
一键复制 编辑 原始数据 按行查看 历史
function cache(option) {
option = option || {}
this.data = {}
this.size = 0
this.maxSize = option.size || 100
this.defaultTime = option.time || 0
this.defaultTime *= 1000
}
cache.prototype.set = function (key, val, ttl) {
if (this.size >= this.maxSize) {
return false
}
this.data[key] = this._wrap(val, ttl)
this.size++
return true
}
cache.prototype.get = function (key) {
if (!this.data[key]) {
return false
}
const val = this._unwrap(this.data[key])
if (val === false) {
delete this.data[key]
this.size--
}
return val
}
cache.prototype._wrap = function (val, ttl) {
return {
data: val,
expires: ttl ? Date.now() + ttl * 1000 : this.defaultTime,
}
}
cache.prototype._unwrap = function (data) {
return data.expires === 0 || data.expires >= Date.now() ? data.data : false
}
module.exports = cache
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zorn/web-storage-utils.git
git@gitee.com:zorn/web-storage-utils.git
zorn
web-storage-utils
web-storage-utils
development

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385