store-gem.js
本地存储localstorage/sessionstorage的封装,cookie完美解决方案
github地址:https://github.com/tccpc/store-gem.js
安装
bower
|
|
npm
|
|
本地存储APIs
|
|
###Constructor
实例化store
set
向缓存中插入一条或多条数据
插入单条myStore.set(key, data,timeouts); //key [必填]必须要为String类型 data [必填] 支持所有类型 timeouts [选填] 支持Number和Date类型 Number类型单位为秒数 默认为 new Date('Fri Dec 31 9999 00:00:00 GMT+0800')
插入多条myStore.set({key: data, key2: data2},timeouts);//过期时间相同
|
|
get
获取key的字符串数据
myStore.get()
无参数为获取全部key/data 返回Object对象myStore.get(key)
返回对应datamyStore.get(key1,key2)
返回Object {key1: data1, key2: data2}
|
|
clear
清空所有key/datamyStore.clear()
|
|
remove
删除key包括key的字符串数据myStore.remove(key)
myStore.remove(key1,key2)
|
|
keys
返回所有key的数组myStore.keys()
|
|
search
搜索所有key中包含string的key/data 返回object对象
搜索方法 myStore.search(string)
|
|
has
判断是否存在返回true/falsemyStore.has(key)
|
|
each
循环遍历,返回false结束遍历
|
|
clearExpires
删除缓存中所有通过WebStorageCache存储的超时值
myStore.clearExpires();
###setTimeout
根据key为已存在的(未超时的)缓存值以当前时间为基准设置新的超时时间。设置成功返回true失败返回false。
myStore.setTimeout(key,timeouts);
|
|
###add
根据key做插入操作,如果key对应的值不存在或者已超时则插入该值,反之什么都不做。插入成功返回true失败返回false。
myStore.add();
###replace
根据key做替换操作,如果key对应的值存在并且未超时则替换该值,反之什么都不做。替换成功返回true失败返回false。
myStore.replace();
链式书写
|
|
兼容
来源:sessionStorage localStorage
特性 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) | iPhone(IOS) | Android | Opera Mobile | Window Phone |
---|---|---|---|---|---|---|---|---|---|
localStorage | 4+ | 3.5+ | 8+ | 10.50+ | 4+ | 3.2+ | 2.1+ | 11+ | 8+ |
sessionStorage | 5+ | 2+ | 8+ | 10.50+ | 4+ | 3.2+ | 2.1+ | 11+ | 8+ |
本地存储大小
JSON.stringify(localStorage).length
当前占用多大容量