Code Monkey home page Code Monkey logo

web-storage-cache's Introduction

WebStorageCache

Build Status npm Gitter Chat

Language

see English Document

WebStorageCache 对HTML5 localStorage 和sessionStorage 进行了扩展,添加了超时时间,序列化方法。可以直接存储json对象,同时可以非常简单的进行超时时间的设置。
优化WebStorageCache自动清除访问的过期数据,避免了过期数据的累积。另外也提供了清除全部过期数据的方法:wsCache.deleteAllExpires();

用法

下载 最新 WebStorageCache。

npm下载

npm install web-storage-cache --save-dev

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

<script src="src/web-storage-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>

也可以在RequireJS使用WebStorageCache:

define(['web-storage-cache'], function(WebStorageCache) {
    // 初始化 WebStorageCache 实例.
    var wsCache = new WebStorageCache();
    // 缓存字符串'wqteam' 到 'username' 中, 超时时间100秒.
    wsCache.set('username', 'wqteam', {exp : 100});
});

例子

var 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();

API

Constructor

var wsCache = new WebStorageCache({
    // [可选] 'localStorage', 'sessionStorage', window.localStorage, window.sessionStorage
    //        或者其他实现了 [Storage API] 的storage实例.
    //        默认 'localStorage'.
    storage: 'localStorage',
    // [可选]  类型Number,公共超时事件设置。默认无限大
    exp: Infinity
});

isSupported

检查当前选择作为缓存的storage是否被用户浏览器支持。 如果不支持调用WebStorageCache API提供的方法将什么都不做。

wsCache.isSupported(); // 返回值Boolean。

set

往缓存中插入数据。

// key [必填] 必须要为String类型。
// value [必填] 支持所以可以JSON.parse 的类型。注:当为undefined的时候会执行 delete(key)操作。
// options [选填] js对象,包含两个属性 exp 和 force。
// {
//     // 类型Number。超时时间,秒。默认无限大。
//     exp: 100,
//     // 类型Boolean。为true时:当超过最大容量导致无法继续插入数据操作时,先清空缓存中已超时的
//     // 内容后再尝试插入数据操作。默认为true。
//     force: true
// }
wsCache.set(key, value, options);

get

根据key获取缓存中未超时数据。返回相应类型String、Boolean、PlainObject、Array的值。

// key [必填] String类型。如果发现该key对应的值已过期,会进行delete(key)操作,返回null。
wsCache.get(key);

delete

根据key删除缓存中的值。

wsCache.delete(key);

deleteAllExpires

删除缓存中所有通过WebStorageCache存储的超时值。

wsCache.deleteAllExpires();

clear

清空缓存中全部的值。注意:这个方法会清除不是使用WebStorageCache插入的值。推荐使用:deleteAllExpires

wsCache.clear();

touch

根据key为已存在的(未超时的)缓存值以当前时间为基准设置新的超时时间。

// key [必填] String类型
// exp [必填] number 单位:秒 js对象包含exp属性(以当前时间为起点的新的超时时间)
wsCache.touch(key, exp: 1);

add

根据key做插入操作,如果key对应的值不存在或者已超时则插入该值,反之什么都不做。 注:不是通过WebStorageCache插入的值也会当作失效的值,依然执行add操作

wsCache.add(key, value, options);

replace

根据key做插入操作,如果key对应的值存在并且未超时则插入该值,反之什么都不做
注:超时时间以当前时间为基准重新设置。

wsCache.replace(key, value, options);

web-storage-cache's People

Contributors

mutoe avatar ronggang avatar wuchangming avatar wupinlang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

web-storage-cache's Issues

超时截止日期的api貌似写错了?

api介绍是这样的:

// 超时截止日期 2015 5 18
cache.set('username', 'xx', {exp : new Date('2015 5 `18')});

但出现报错信息:
web-storage-cache.js:205 Uncaught Error:expiresparameter cannot be converted to a valid Date instance

查看源码没有参数为Date类型的处理语句,应该是直接传字符串就行了
cache.set('username', 'xx', {exp : '2015 518'});`

使用add函数遇到的问题

我的需求是为了不影响用户体验,不管数据是否过期先用存储的数据,如果过期了重新获取重新处理一遍,在处理的函数里面使用add函数时发现虽然判断语句内在storage的key 存在时不会set,但是我在add后发现通过get 返回值为true,导致我后面判断是否过期的条件语句内始终不会执行,而且过期时间也在更新,表明数据更新了。我把那个if的条件改成this.storage.getItem(key) 后也就是直接通过localstorage或者sessionstorage取值判断就正常了。

Constructor的问题

楼主问一下,
var defaults = { storage: 'localStorage', exp: Infinity //An expiration time, in seconds. default never . };
**这里的构造函数中的exp在后面代码中并没有使用过啊,在set方法中如果不设exp的话用得就是_maxExpireDate的时间,我用new WebStorageCache({exp:100}).set("x","y");用开发者工具查看的数据中
{"c":1476957713537,"e":7955078400000,"v":""hello""}过期时间e依旧是_maxExpireDate设置的时间的毫秒数

Packaging issues with 1.0.1 via NPM

I've been having problems with require not finding web-storage-cache 1.0.1 and I think I've pinned it down to a change made in this commit:

88c4bd6

Package.json was changed to this:

  "main": "dist/web-storage-cache.js",

...and I think it should actually be this (as it was in 1.0.0):

  "main": "src/web-storage-cache.js",

The path referencing dist doesn't exist as the minified version is in the dist folder.

Also, should the browser property be this?

"browser": "dist/web-storage-cache.min.js",

建议: 添加标准时间传入而非本地时间

正常像用户登录记录token,有效时间,并不会用通过客户端创建的时间为准, 因为客户端的时间不可靠, 一般服务器都会有返回事件,建议使用服务器时间进行有效期判断而非本地时间.我个是使用服务器时间进行校验的

中文介绍看这里

http://www.cnblogs.com/wuchangming/p/4897703.html

发现localstorage只是存储简单的string键值对。但实际使用中经常要配合JSON.parse 和 JSON.stringify, 在很多场景中又需要添加超时时间。看了一些开源的项目也对localstorage进行了封装,但都没有完全复合工作中的业务场景的。所以自己写了一个。

不知道国内有没有类似github这种活跃的社区,感觉国内的开源氛围都不强。

使用:

var wsCache = new WebStorageCache();

// 缓存字符串'wqteam' 到 'username' 中, 超时时间100秒
wsCache.set('username', 'wqteam', {exp : 100});

// 超时截止日期 2015 5 18
wsCache.set('username', 'wqteam', {exp : new Date('2015 5 18')});

// 获取缓存中 '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();

  
API
Constructor

var wsCache = new WebStorageCache({
// [可选] 'localStorage', 'sessionStorage', window.localStorage, window.sessionStorage
// 或者其他实现了 [Storage API] 的storage实例.
// 默认 'localStorage'.
storage: 'localStorage',
// [可选] 类型Number,公共超时事件设置。默认无限大
exp: Infinity
});

  
isSupported

检查当前选择作为缓存的storage是否被用户浏览器支持。 如果不支持调用WebStorageCache API提供的方法将什么都不做。

wsCache.isSupported(); // 返回值Boolean。

  
set

往缓存中插入数据。

// key [必填] 必须要为String类型。
// value [必填] 支持所以可以JSON.parse 的类型。注:当为undefined的时候会执行 delete(key)操作。
// options [选填] js对象,包含两个属性 exp 和 force。
// {
// // 类型Number。超时时间,秒。默认无限大。
// exp: 100,
// // 类型Boolean。为true时:当超过最大容量导致无法继续插入数据操作时,先清空缓存中已超时的
// // 内容后再尝试插入数据操作。默认为true。
// force: true
// }
wsCache.set(key, value, options);

  
get

根据key获取缓存中未超时数据。返回相应类型String、Boolean、PlainObject、Array的值。

// key [必填] String类型。如果发现该key对应的值已过期,会进行delete(key)操作,返回null。
wsCache.get(key);

  
delete

根据key删除缓存中的值。

wsCache.delete(key);

  
deleteAllExpires

删除缓存中所有通过WebStorageCache存储的超时值。

wsCache.deleteAllExpires();

  
clear

清空缓存中全部的值。注意:这个方法会清除不是使用WebStorageCache插入的值。推荐使用:deleteAllExpires。
1

wsCache.clear();

  
touch

根据key为已存在的(未超时的)缓存值以当前时间为基准设置新的超时时间。

// key [必填] String类型
// exp [必填] number 单位:秒 js对象包含exp属性(以当前时间为起点的新的超时时间)
wsCache.touch(key, exp: 1);

  
add

根据key做插入操作,如果key对应的值不存在或者已超时则插入该值,反之什么都不做。 注:不是通过WebStorageCache插入的值也会当作失效的值,依然执行add操作

wsCache.add(key, value, options);

  
replace

根据key做插入操作,如果key对应的值存在并且未超时则插入该值,反之什么都不做
注:超时时间以当前时间为基准重新设置。

wsCache.replace(key, value, options);

是否有可以追加缓存内容的方法

例如在使用add方法之后,发现会把最新的内容加入进去,之前同一个key缓存的内容被替换了。那么是否可以把add的方法变成追加缓存数据内容,也就是说同一个key中可以把多个内容插入进来?

关于存储类型的建议

希望可以把存储类型 storage参数的设定放到set函数里面,因为一个页面上基本上都会有localstorage和sessionstorage的数据存储需求,相比初始化两个wsStorage对象,在保存时设定storage参数更灵活。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.