zoukankan      html  css  js  c++  java
  • web-storage-cache 使用JS数据缓存

    https://github.com/WQTeam/web-storage-cache

    使用WebStorageCache,只要在页面上引入下面代码即可。

    <script src="src/web-storage-cache.js"></script>
    <script src="~/scripts/AjaxCache/ajax-cache.js"></script> <script> // create WebStorageCache instance. var wsCache = new WebStorageCache(); // cache 'wqteam' at 'username', expired in 100 seconds wsCache.set('username', 'wqteam', {exp : 100}); </script>

    例子:

    ar wsCache = new WebStorageCache();
    
    // 缓存字符串'wqteam' 到 'username' 中, 超时时间100秒
    wsCache.set('username', 'wqteam', {exp : 100});
    
    // 超时截止日期,可用使用Date类型
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    wsCache.set('username', 'wqteam', {exp : nextYear});
    
    // 获取缓存中 'username' 的值
    wsCache.get('username');
    
    // 缓存简单js对象,默认使用序列化方法为JSON.stringify。可以通过初始化wsCache的时候配置serializer.serialize
    wsCache.set('user', { name: 'Wu', organization: 'wqteam'});
    
    // 读取缓存中的简单js对象 - 默认使用反序列化方法为JSON.parse。可以通过初始化wsCache的时候配置serializer.deserialize
    var user = wsCache.get('user');
    alert(user.name + ' belongs to ' + user.organization);
    
    // 删除缓存中 'username'
    wsCache.delete('username');
    
    // 手工删除所有超时CacheItem,
    wsCache.deleteAllExpires();
    
    // 清除客户端中所有缓存
    wsCache.clear();
    
    // 为已存在的(未超时的)缓存值设置新的超时时间。
    wsCache.touch('username', 1);
    
    // 如果缓存中没有key为username2的缓存,则添加username2。反之什么都不做
    wsCache.add('username2', 'wqteam', {exp : 1});
    
    // 如果缓存中有key为username的缓存,则替换为新值。反之什么都不做
    wsCache.replace('username', 'new wqteam', {exp : 1});
    
    // 检查当前选择作为缓存的storage是否被用户浏览器支持。
    //如果不支持调用WebStorageCache API提供的方法将什么都不做。
    wsCache.isSupported();
    

      

  • 相关阅读:
    5 Python3 函数进阶&迭代器与生成器
    2 python第三章文件操作
    4 python内置函数
    python内置函数 eval()、exec()以及complie()函数
    0 字符与字节的区别
    python enumerate() 函数
    1 python 文件处理
    python 之编写登陆接口
    python 之九九乘法表
    第一模块第二章-数据类型整理
  • 原文地址:https://www.cnblogs.com/vincentvoid/p/6763838.html
Copyright © 2011-2022 走看看