关于前端缓存的一个封装思路
2017-02-19 16:11
441 查看
function getCache (key) { var cachedContentStr = window.localStorage.getItem(key); console.log(cachedContentStr); if (cachedContentStr ) { var cachedContent = JSON.parse(cachedContentStr); var cacheEndTime = cachedContent.cacheEndTime || 0; if (cacheEndTime === 0) { return cachedContent.realValue; } else { var currentDate = new Date().getTime(); if (currentDate <= cacheEndTime) { return cachedContent.realValue; } } } return undefined; }; /** * * @key 缓存的key * @param value 对象字面量 * @timeOut 缓存的时间,单位是分钟 */ function setCache(key, value, timeOut) { timeOut = timeOut || 0; value = value || {}; var timeOutms = 0; var cachedContent = {}; if (timeOut > 0) { var currentDate = new Date().getTime(); timeOutms = timeOut * 60 * 1000; var cacheEndTime = currentDate + timeOut; cachedContent = { realValue: value, cacheEndTime: timeOutms }; }else { cachedContent = { realValue: value, cacheEndTime: timeOutms }; } window.localStorage.setItem(key, JSON.stringify(cachedContent)); }; function deleteCache (key) { window.localStorage.removeItem(key); }; /** * 每次登陆的时候清空过期的localStorage内容,预防localStorage变得越来越大 */ function deleteInvlidCache () { };
相关文章推荐
- 关于密码管理,一个很好的思路
- 关于制作一个通用的DOS批处理段注释命令的思路介绍
- 让我顺间思路清晰的一个无聊调试(关于实例、继承)
- 关于HibernateUtil的一个封装
- 一个关于异步下载的封装
- 关于封装--一个有趣的C++例子
- 关于dao层的封装和前端分页的结合(文章有点长,耐心点哦)
- 前端学习笔记一:关于一个新页面的开发
- 一个简单的PHP缓存思路的实现
- 关于ConcurrentQueue--一个队列缓存的设计
- 个人封装的一个获取磁盘详细信息的类(如厂家,2级缓存等)
- 关于一个简单shellcode封装成C语言的形式
- 发一个我自己写的.net中使用的缓存封装类(算是第三版了)
- 关于C# 模仿千千静听 磁铁窗体 的个人的一个思路
- 自己写的一个关于Sqlite3 操作的 的C++封装
- 请各位大虾们帮帮小弟,谢谢!一个关于产品搜索数据库设计思路的问题
- 关于轻量级数据库SQLite一个简单的SQLiteHelper访问数据库封装类
- 在一个浏览器里呈现所有浏览器测试结果的前端测试工具的思路
- 关于304缓存 (转沫鱼的前端世界)
- 关于DAO层一个封装