咱们将基于localStorage原始api进行扩展,让其支持失效时间,操做完成后的回调。在文章的最后,我将给出库的完成代码,接下来咱们就一步步实现吧。前端
const BaseStorage = function(preId, timeSign){
// 初始化一些操做
}
BaseStorage.prototype = {
storage: localStorage || window.localStorage,
set: function(key, value, cb, time){
},
get: function(key, cb){
},
// 删除storage,若是删除成功,返回删除的内容
remove: function(key, cb){
}
}
复制代码
如上能够发现,咱们的storage会有三个核心api,分别为set,get,remove,咱们使用localStorage做为基础库支持,固然你也能够将上面的库换成sessionStorage或者其余。vue
status: {
SUCCESS: 0, // 成功
FAILURE: 1, // 失败
OVERFLOW: 2, // 数据溢出
TIMEOUT: 3 // 超时
},
复制代码
为了实现过时时间,咱们有两种思路,第一种是先将一个过时时间存到storage中,每次操做都检查一遍是否过时,可是这种方案意味着对不一样的键就要设置不一样的过时时间的storage与之对应,这样会占用额外的库内存,维护起来也不方便。另外一种方法就是将过时时间存放到键值中,将时间和值经过标识符分隔,每次取的时候从值中截取过时时间,再将真实的值取出来返回,这种方案不会添加额外的键值对存储,维护起来也相对简单,因此咱们采用这种方案。 为了区分不一样的库对象,咱们还能够添加键前缀,以下:算法
const BaseLocalStorage = function(preId, timeSign){
this.preId = preId; // 键前缀
this.timeSign = timeSign || '|-|'; // 过时时间和值的分隔符
}
复制代码
基于这个思想,咱们就能够接下来的实现了。vuex
getKey: function(key){
return this.preId + key
},
复制代码
set: function(key, value, cb, time){
var status = this.status.SUCCESS,
key = this.getKey(key);
// 设置失效时间,未设置时间默认为一个月
try{
time = new Date(time).getTime() || time.getTime();
}catch(e){
time = new Date().getTime() + 1000*60*60*24*31
}
try{
this.storage.setItem(key, time + this.timeSign + value);
}catch(e){
status = this.status.OVERFLOW;
}
// 操做完成后的回调
cb && cb.call(this, status, key, value)
}
复制代码
get: function(key, cb){
var status = this.status.SUCCESS,
key = this.getKey(key),
value = null,
timeSignLen = this.timeSign.length,
that = this,
index,
time,
result;
try{
value = that.storage.getItem(key);
}catch(e){
result = {
status: that.status.FAILURE,
value: null
}
cb && cb.call(this, result.status, result.value);
return result
}
if(value) {
index = value.indexOf(that.timeSign);
time = +value.slice(0, index);
// 判断是否过时,过时则清除
if(time > new Date().getTime() || time == 0){
value = value.slice(index+timeSignLen);
}else{
value = null,
status = that.status.TIMEOUT;
that.remove(key);
}
}else{
status = that.status.FAILURE;
}
result = {
status: status,
value: value
};
cb && cb.call(this, result.status, result.value);
return result
}
复制代码
// 删除storage,若是删除成功,返回删除的内容
remove: function(key, cb){
var status = this.status.FAILURE,
key = this.getKey(key),
value = null;
try{
value = this.storage.getItem(key);
}catch(e){
// dosomething
}
if(value){
try{
this.storage.removeItem(key);
status = this.status.SUCCESS;
}catch(e){
// dosomething
}
}
cb && cb.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length))
}
复制代码
在api的实现过程当中,因为某种误操做极可能致使storage报错,因此建议最好用trycatch包裹,这样能够避免影响后面的逻辑。vue-cli
接下来咱们能够这么使用:typescript
let a = new BaseStorage('_', '@');
a.set('name', '123')
a.get('name') // {status: 0, value: "123"}
// 设置失效时间
a.set('name', '123', null, new Date().getTime() + 1000*60*60*24*31)
// 移除
a.remove('name')
复制代码
/** * 数据管理器 */
(function(win){
const BaseStorage = function(preId, timeSign){
this.preId = preId;
this.timeSign = timeSign || '|-|';
}
BaseStorage.prototype = {
status: {
SUCCESS: 0,
FAILURE: 1,
OVERFLOW: 2,
TIMEOUT: 3
},
storage: localStorage || window.localStorage,
getKey: function(key){
return this.preId + key
},
set: function(key, value, cb, time){
var status = this.status.SUCCESS,
key = this.getKey(key);
// 设置失效时间,未设置时间默认为一个月
try{
time = new Date(time).getTime() || time.getTime();
}catch(e){
time = new Date().getTime() + 1000*60*60*24*31
}
try{
this.storage.setItem(key, time + this.timeSign + value);
}catch(e){
status = this.status.OVERFLOW;
}
cb && cb.call(this, status, key, value)
},
get: function(key, cb){
var status = this.status.SUCCESS,
key = this.getKey(key),
value = null,
timeSignLen = this.timeSign.length,
that = this,
index,
time,
result;
try{
value = that.storage.getItem(key);
}catch(e){
result = {
status: that.status.FAILURE,
value: null
}
cb && cb.call(this, result.status, result.value);
return result
}
if(value) {
index = value.indexOf(that.timeSign);
time = +value.slice(0, index);
if(time > new Date().getTime() || time == 0){
value = value.slice(index+timeSignLen);
}else{
value = null,
status = that.status.TIMEOUT;
that.remove(key);
}
}else{
status = that.status.FAILURE;
}
result = {
status: status,
value: value
};
cb && cb.call(this, result.status, result.value);
return result
},
// 删除storage,若是删除成功,返回删除的内容
remove: function(key, cb){
var status = this.status.FAILURE,
key = this.getKey(key),
value = null;
try{
value = this.storage.getItem(key);
}catch(e){
// dosomething
}
if(value){
try{
this.storage.removeItem(key);
status = this.status.SUCCESS;
}catch(e){
// dosomething
}
}
cb && cb.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length))
}
}
win.BS = BaseStorage;
})(window)
复制代码
你们也能够基于此扩展更强大的功能,若是有更好的想法,欢迎交流,探讨。gulp
欢迎关注下方公众号,获取更多前端知识精粹和学习社群: api
回复学习路径,将获取笔者多年从业经验的前端学习路径的思惟导图;数组
交流微信群:微信