Coder Social home page Coder Social logo

store's Introduction

store

localstorage本地缓存方案

解决数据过期问题

本地实际应用建议绑定当前的用户,解决不能用户看到不同的缓存,可以做下适当的包装,比如:

setCache:function(key,value,exp){
    //过期时间默认1天
    exp=exp||86400;
    store.set(key+'_'+curUserID, value,exp);
    
},

getCache:function(key){
    return store.get(key+'_'+curUserID);
}

todo:

另外还又可以尝试结合一些简单的加解密技术,来保证客户端缓存数据的相对安全,但势必会造成性能上的损失。

数据加密已经完成,具体见:store_code.js,对数据的存储和获取分别做了加解密。

简单API

设置缓存

//简单的缓存
store.set('a',{a:1,b:2})

//增加过期时间:单位是秒
store.set('a',{a:1,b:2},3600)

//清除指定缓存
store.set('a',null)
//或者
sotre.set('a');//为了代码的可读性,不建议这样使用

获取缓存的数据

store.get('a')
//log:{a:1,b:2}

删除指定缓存

store.del('a');

删除所有过期的缓存

store.clearExp();

清除所有缓存

store.clearAll()

添加一个缓存

//和set的区别是,这个会检测是否已经存在同名的缓存,如果存在,就不做任何处理
// 单set是会覆盖当前值的
store.add('b',{c:1,d:1},3600)

设置缓存的过期时间

//过期时间由原来的1小时,变成了2小时,并且是从创建时间计算的
store.setExp('a',7200)

读取缓存的过期时间

store.getExp('a')

store's People

Contributors

mishe avatar

Watchers

xue xiaogang avatar

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.