面localStorage用作数据缓存的简易封装

最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限制,因此封装了下面这个对象。

我的封装非常直观简单,比网上的一些晦涩的代码明显小巧精简实用。目前只自动回收过期或最后一次访问时间到现在的间隔最大的项,以后有时间,再把访问次数纳入到自动回收的算法中。

window.MyCache = window.MyCache || {
size: 18, //默认队列数量最大值
defaultexpire: 1800, //默认有效期,30分钟(单位:秒)
getCache: function () {
var jsonStr = window.localStorage ? window.localStorage.getItem('myCacheQueue') : '';
return jsonStr ? JSON.parse(jsonStr) : [];
},
get: function (key) {
var queue = this.getCache();
for (var i = 0; i < queue.length; i++) {
if (queue[i].key == key && queue[i].lasttime > (new Date()).getTime()) {
var re = queue[i];
queue.splice(i, 1);
re.times += 1; //访问次数加一
re.lasttime = (new Date()).getTime() + re.expire * 1000;//自动延长过期时间
queue.push(re); //确保项的顺序跟最后访问时间一致
window.localStorage.setItem('myCacheQueue', JSON.stringify(queue));
return re.value;
}
}
return null;
},
set: function (key, value, expire) {//expire:单位为秒的整数,为负表示永不过期,为零表示删除
if (!key || !value || !window.localStorage) return false;
var queue = getCache();
if (queue.length > this.size) { // 队列满的情况下,移除上次访问时间最早的项
queue.splice(0, 1);
}
for (var i = 0; i < queue.length; i++) {
if (queue[i].key == key) {//如果已经存在,则删除
queue.splice(i, 1);
}
}
if (expire != 0) {
var time = !expire ? this.defaultexpire: expire < 0 ? 9999999999999 : expire;
queue.push({ "key": key, "value": value, "times": 0, "expire": time, "lasttime": (new Date()).getTime() + time * 1000 });
window.localStorage.setItem('myCacheQueue', JSON.stringify(queue));
}
}
}

面localStorage用作数据缓存的简易封装的更多相关文章

  1. 页面localStorage用作数据缓存的简易封装

    最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限 ...

  2. sectionStorage与localStorage更新缓存,以及更新layui的数据缓存

    var aa = sessionStorage.getItem('datInfo');//获取缓存数据 name = aa.user; var names = '张三'; sessionStorage ...

  3. jQuery数据缓存方案详解:$.data()的使用

    我们经常使用隐藏控件或者是js全局变量来临时存储数据,全局变量容易导致命名污染,隐藏控件导致经常读写dom浪费性能.jQuery提供了自己的数据缓存方案,能够达到和隐藏控件.全局变量相同的效果,但是j ...

  4. jQuery 2.0.3 源码分析 数据缓存

    历史背景: jQuery从1.2.3版本引入数据缓存系统,主要的原因就是早期的事件系统 Dean Edwards 的 ddEvent.js代码 带来的问题: 没有一个系统的缓存机制,它把事件的回调都放 ...

  5. Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析

    mongodb和memcached不是一个范畴内的东西.mongodb是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据.mongodb和memcached不存在谁替换谁的问题. 和 ...

  6. 微信小程序-数据缓存

    每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync).wx.getStorage(wx.getStorageSync).wx.clearSt ...

  7. 【菜鸟学习jquery源码】数据缓存与data()

    前言 最近比较烦,深圳的工作还没着落,论文不想弄,烦.....今天看了下jquery的数据缓存的代码,参考着Aaron的源码分析,自己有点理解了,和大家分享下.以后也打算把自己的jquery的学习心得 ...

  8. jQuery源码解读 - 数据缓存系统:jQuery.data

    jQuery在1.2后引入jQuery.data(数据缓存系统),主要的作用是让一组自定义的数据可以DOM元素相关联——浅显的说:就是让一个对象和一组数据一对一的关联. 一组和Element相关的数据 ...

  9. jQuery1.9.1源码分析--数据缓存Data模块

    jQuery1.9.1源码分析--数据缓存Data模块 阅读目录 jQuery API中Data的基本使用方法介绍 jQuery.acceptData(elem)源码分析 jQuery.data(el ...

随机推荐

  1. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. C#进阶系列——WebApi 接口测试工具:WebApiTestClient

    前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员也不懂C#语法,API里面的接口也不能直接给他们看,没办法,只有整个详细一点的文档呗.由于接口个数有点多,每 ...

  4. 如何用Unity创建一个的简单的HoloLens 3D程序

    注:本文提到的代码示例下载地址>How to create a Hello World 3D holographic app with Unity 之前我们有讲过一次如何在HoloLens中创建 ...

  5. Activity去Title的几种方式

    第一种:直接加一行代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...

  6. C# 动态修改Config

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); confi ...

  7. IO多路复用概念性

    sellect.poll.epoll三者的区别 先来了解一下什么是进程切换 为了控制进程的执行,内核必须有能力挂起正在CPU上运行的进程,并恢复以前挂起的某个进程的执行,这种行为为进程的切换,任务切换 ...

  8. mongodb安装启动遇到的问题

    好不容易下载到了mongodb,配置的时候遇到了不少问题. 下载的是解压包,不是官网的,有一个bin目录,解压到一个自己想要的目录,如d:\mongo,首先把bin复制进来,然后创建data目录,da ...

  9. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  10. Steamroller

    FCC题目:对嵌套的数组进行扁平化处理.你必须考虑到不同层级的嵌套. 示例: steamroller([[["a"]], [["b"]]]) 应该返回 [&qu ...