用js写了一个类似redis存储结构的类库,目前只有的存储类型只有hash、set两个,

还没测试过性能,欢迎各位猿友能够帮我指出程序代码的缺陷,

后期有时间会完善其他几个类型的存储结构。

 /**************************************************************************
* 类名: CRedis
* 描述: Redis数据库的JS版本
* 版本: 2.0 * 作者: 老狼
* 创建日期: 2016-07-13
* 更新日期: 2016-07-13
**************************************************************************/
var C_REDIS_TYPE_NONE = 0;
var C_REDIS_TYPE_STRING = 1;
var C_REDIS_TYPE_LIST = 2;
var C_REDIS_TYPE_SET = 3;
var C_REDIS_TYPE_SORTEDSET = 4;
var C_REDIS_TYPE_HASH = 5;
var C_REDIS_TYPE_UNKNOW = 6; var C_RESULT_NOTEXISTS = 0;
var C_RESULT_EXISTS = 1; function CRedis() {
this.redis = {};
this.redis.count = 0;
this.redis.db = [];
}; CRedis.prototype.HashDelete = function (key, hashFields) {
if (this._isNullByParams(key, hashFields) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; var n = this.redis.db[key].length;
if ('[object String]' === Object.prototype.toString.call(hashFields)) {
if (C_RESULT_NOTEXISTS == this.HashExists(key, hashFields))
return 0; delete this.redis.db[key].dt[hashFields];
--this.redis.db[key].length;
} else if ('[object Array]' === Object.prototype.toString.call(hashFields)) {
for (var i = 0; i < hashFields.length; ++i) {
if (this._isNullByParams(hashFields[i]) || C_RESULT_NOTEXISTS == this.HashExists(key, hashFields[i]))
continue; delete this.redis.db[key].dt[hashFields[i]];
--this.redis.db[key].length;
}
} else {
return -1;
} if (0 == this.redis.db[key].length)
--this.redis.count; return n - this.redis.db[key].length;
};
CRedis.prototype.HashExists = function (key, hashField) {
if (this._isNullByParams(key, hashField) || this._isNonStringByParams(key, hashField))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; return (undefined === this.redis.db[key].dt[hashField] || 0 == this.redis.db[key].length) ? C_RESULT_NOTEXISTS : C_RESULT_EXISTS;
};
CRedis.prototype.HashGet = function (key, hashField) {
if (this._isNullByParams(key, hashField) || this._isNonStringByParams(key, hashField))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; return C_RESULT_EXISTS == this.HashExists(key, hashField) ? this.redis.db[key].dt[hashField] : undefined;
};
CRedis.prototype.HashGetAll = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; return this.redis.db[key].dt;
};
CRedis.prototype.HashKeys = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; var a = [];
for (var k in this.redis.db[key].dt)
a.push(k); return a;
};
CRedis.prototype.HashLength = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; return this.redis.db[key].length;
};
CRedis.prototype.HashSet = function (key, hashField, hashValue) {
if (this._isNullByParams(key, hashField, hashValue) || this._isNonStringByParams(key, hashField))
return -1; if (C_RESULT_EXISTS == this.KeyExists(key) && C_REDIS_TYPE_HASH != this.KeyType(key))
return null; var a = {
"type": C_REDIS_TYPE_HASH,
"length": 0,
"dt": []
}; if (C_RESULT_EXISTS == this.KeyExists(key))
a = this.redis.db[key];
else
++this.redis.count; if (C_RESULT_NOTEXISTS == this.KeyExists(key) || C_RESULT_NOTEXISTS == this.HashExists(key, hashField))
++a.length; a.dt[hashField] = hashValue; this.redis.db[key] = a;
return 200;
};
CRedis.prototype.HashValues = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_HASH != this.KeyType(key))
return null; var a = [];
for (var k in this.redis.db[key].dt)
a.push(this.redis.db[key].dt[k]); return a;
};
CRedis.prototype.KeyDelete = function (keys) {
if (this._isNullByParams(keys))
return -1; var n = this.redis.count;
if ('[object String]' === Object.prototype.toString.call(keys)) {
if (C_RESULT_NOTEXISTS == this.KeyExists(keys))
return 0; delete this.redis.db[keys];
--this.redis.count;
} else if ('[object Array]' === Object.prototype.toString.call(keys)) {
for (var i = 0; i < keys.length; ++i) {
if (this._isNullByParams(keys[i]) || C_RESULT_NOTEXISTS == this.KeyExists(keys[i]))
continue; delete this.redis.db[keys[i]];
--this.redis.count;
}
} else {
return -1;
} return n - this.redis.count;
};
CRedis.prototype.KeyExists = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; return (0 == this.redis.count || undefined === this.redis.db[key] || 0 == this.redis.db[key].length) ? C_RESULT_NOTEXISTS : C_RESULT_EXISTS;
};
CRedis.prototype.KeyType = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; return (C_RESULT_NOTEXISTS == this.KeyExists(key) || null == this.redis.db[key].type ||
undefined == this.redis.db[key].type) ? C_REDIS_TYPE_NONE : this.redis.db[key].type;
};
CRedis.prototype.SetAdd = function (key, value) {
if (this._isNullByParams(key, value) || this._isNonStringByParams(key))
return -1; if (C_RESULT_EXISTS == this.KeyExists(key) && C_REDIS_TYPE_SET != this.KeyType(key))
return null; if (C_RESULT_EXISTS == this.SetContains(key, value))
return C_RESULT_EXISTS; var a = {
"type": C_REDIS_TYPE_SET,
"length": 0,
"key": [],
"dt": []
}; if (C_RESULT_EXISTS == this.KeyExists(key))
a = this.redis.db[key];
else
++this.redis.count; a.key[JSON.stringify(value)] = null;
a.dt.push(value);
++a.length; this.redis.db[key] = a;
return 200;
};
CRedis.prototype.SetContains = function (key, value) {
if (this._isNullByParams(key, value) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; return undefined === this.redis.db[key].key[JSON.stringify(value)] ? C_RESULT_NOTEXISTS : C_RESULT_EXISTS;
};
CRedis.prototype.SetLength = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; return this.redis.db[key].length;
};
CRedis.prototype.SetPop = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; var r = this.redis.db[key].dt[this.redis.db[key].length - 1]; delete this.redis.db[key].key[r];
this.redis.db[key].dt = this.redis.db[key].dt.filter(function (item, i) {
return i !== this.redis.db[key].length - 1;
}, this);
--this.redis.db[key].length; if (0 == this.redis.db[key].length)
--this.redis.count; return r;
};
CRedis.prototype.SetRemove = function (key, values) {
if (this._isNullByParams(key, values) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; var n = this.redis.db[key].length;
if ('[object String]' === Object.prototype.toString.call(values) || '[object Object]' === Object.prototype.toString.call(values)) {
if (C_RESULT_NOTEXISTS == this.SetContains(key, values))
return 0; var jsonValue = JSON.stringify(values); this.redis.db[key].dt = this.redis.db[key].dt.filter(function (item) {
return JSON.stringify(item) !== jsonValue;
});
delete this.redis.db[key].key[jsonValue];
--this.redis.db[key].length;
} else if ('[object Array]' === Object.prototype.toString.call(values)) {
for (var i = 0; i < values.length; ++i) {
if (this._isNullByParams(values[i]) || C_RESULT_NOTEXISTS == this.SetContains(key, values[i]))
continue; var jsonValue = JSON.stringify(values[i]); this.redis.db[key].dt = this.redis.db[key].dt.filter(function (item) {
return JSON.stringify(item) !== jsonValue;
});
delete this.redis.db[key].key[jsonValue];
--this.redis.db[key].length;
}
} else {
return -1;
} if (0 == this.redis.db[key].length)
--this.redis.count; return n - this.redis.db[key].length;
};
CRedis.prototype.SetScan = function (key) {
if (this._isNullByParams(key) || this._isNonStringByParams(key))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; return this.redis.db[key].dt;
};
CRedis.prototype.SetGetRange = function (key, beg, end) {
if (this._isNullByParams(key, beg, end) || this._isNonStringByParams(key) || this._isNonNumberByParams(beg, end))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; if (0 > beg || end < beg || end >= this.redis.db[key].length)
return undefined; var a = [];
for (var i = beg; i <= end; ++i) {
if (this.redis.db[key].length <= i)
break; a.push(this.redis.db[key].dt[i]);
} return a;
};
CRedis.prototype.SetGetValue = function (key, index) {
if (this._isNullByParams(key, index) || this._isNonStringByParams(key) || this._isNonNumberByParams(index))
return -1; if (C_RESULT_NOTEXISTS == this.KeyExists(key))
return undefined; if (C_REDIS_TYPE_SET != this.KeyType(key))
return null; if (0 > index || index >= this.redis.db[key].length)
return undefined; return this.redis.db[key].dt[index];
};
CRedis.prototype._isNullByParams = function () {
return Array.prototype.slice.call(arguments).some(function (item) {
return undefined === item || null === item;
});
};
CRedis.prototype._isNonStringByParams = function () {
return Array.prototype.slice.call(arguments).some(function (item) {
return '[object String]' !== Object.prototype.toString.call(item);
});
};
CRedis.prototype._isNonNumberByParams = function () {
return Array.prototype.slice.call(arguments).some(function (item) {
return '[object Number]' !== Object.prototype.toString.call(item);
});
};

仿Redis用来作前端浏览器的数据存储结构的更多相关文章

  1. window.name应用于浏览器端数据存储

    本代码简单地分享利用window.name实现浏览器端数据存储: 1.在同一个页面一个地方设置window.name = "abc",另外一个地方读取window.name,自然能 ...

  2. 浏览器本地数据存储解决方案以及cookie的坑

    本地数据存储解决方案以及cookie的坑 问题: cookie过长导致页面打开失败 背景: 在公司的项目中有一个需求是打开多个工单即在同一个页面中打开了多个tab(iframe),但是需要在刷新时只刷 ...

  3. Redis之数据存储结构

    今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...

  4. JS中浏览器的数据存储机制

    一.JS中的三种数据存储方式 cookie.sessionStorage.localStorage 二.cookie 1.cookie的定义: cookie是存储在浏览器上的一小段数据,用来记录某些当 ...

  5. JavaScript浏览器本地数据存储

    浏览器本地存储主要使用的是sessionStorage和localStorage.两者都支持,sessionStorage保存的是浏览器和服务器的一次对话信息,只在一次回话中有效.当在新标签页或新窗口 ...

  6. Redis数据存储结构之String

    前言: 在Redis使用中,我们最常使用的操作是set key value,或 get key value .这里面包含了redis最基本的数据类型:String,字符串类型是redis中最基本的类型 ...

  7. Redis(一):基本数据类型与底层存储结构

    最近在整理有关redis的相关知识,对于redis的基本数据类型以及其底层的存储结构简要的进行汇总和备注(主要为面试用) Redis对外提供的基本数据类型主要为五类,分别是 STRING:可以存储字符 ...

  8. springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发

    工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...

  9. redis数据库如何用Django框架缓存数据

    ---恢复内容开始--- 一.python 使用redis 1.1 安装 pip install redis 测试有一些基本的数据类型 import redis # redis 是一个缓存数据库 # ...

随机推荐

  1. 读书笔记---《火球:UML大战需求分析》

    书评 作为一本UML和需求分析的入门书来说还算可以,写的比较接地气,如果是做过很多项目的读者,很容易找到共鸣点. 美中不足:部分概念可能有错误,其中对于Component和Artifact的解释明显和 ...

  2. HMAC加密的消息摘要码

    HMAC(Hash Message Authentication Code)哈希消息授权码,它在消息摘要算法(例如MD5,SHA系列算法)的基础上,使用密钥对消息摘要进行加密.它相当于一个马甲,内里可 ...

  3. sharedUserId

    android:sharedUserId sharedUserId的作用是让两个应用程序共享一个user id,我们都知道linux进程给每一个应用程序分配了一个独立的user id,所以如果两个或多 ...

  4. eclipse注释快捷键(含方法注释)

    整段注释: /*public boolean executeUpdate(String sql) { System.out.println(sql); boolean mark=false; try ...

  5. 收集的在线图片压缩(jpg/png)

    http://www.yasuotu.com/ http://www.jpegmini.com/ http://www.tumiaoya.com/ https://tinypng.com/(推荐) h ...

  6. 防DDOS攻击SHELL脚本

    最近一段时间服务器频繁遭到DDOS攻击,目前只能通过封IP来源来暂时解决.IP不源变化多端,光靠手工来添加简直是恶梦,想了个方法,用SHELL来做. 比较简单,但很实用:) 以下内容根据作者原文进行适 ...

  7. usr/include/dispatch - dispatch_source

    博文一部分摘自:Parse分析,以下简称博文1(LeanCloud工程师针对Parse使用GCD的分析) 博文一部分摘自:GCD入门,以下简称博文2 建议先了解一下:BSD基础知识 在Dispatch ...

  8. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  9. C#的winform控件命名规范

    注:这里用红字标记的部分表示有重复出现,括号内为替代表示方案 1.标准控件 序号 控件类型简写 控件类型 1 btn Button 2 chk CheckBox 3 ckl CheckedListBo ...

  10. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...