用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. eclipse关闭编译时不必要的校验

  2. BZOJ 1878: [SDOI2009]HH的项链

    1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3548  Solved: 1757[Submit][Statu ...

  3. hadoop在网页客户端的maven配置

    hadoop网页客户端maven配置,只能在tomcat7上运行,tomcat6和tomcat8运行会出错,我用的是tomcat-7.0.67 完整的pom.xml内容为: <!-- 这个配置只 ...

  4. 有关bat的一些代码

    1.if语句    @echo off     if exist E:\aa.dat dir C: >> E:\ff.txt       pause     type E:\ff.txt ...

  5. .NET DateTime 显示格式

    备注     format 参数应包含单个格式说明符 (请参阅 标准日期和时间格式字符串) 或自定义格式模式 (请参阅 Cadenas con formato de fecha y hora pers ...

  6. kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)

    问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的原因是什么? 3.kafka集群consumer和producer状 ...

  7. 轻松理解spring IOC

    spring IOC(Inversion of control)即控制反转 概念:一,spring框架的核心之一 二,控制权由对象本身转向容器:由容器根据配置文件去创建实例并创建各个实例之间的依赖关系 ...

  8. DD_belatedPNG.js解决透明PNG图片背景灰色问题

    <!--[]> <script type="text/javascript" src="http://www.phpddt.com/usr/themes ...

  9. linux下的apache配置文件详解

    .Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...

  10. UML--PowerDesigner使用小结

    以前只是觉得.边看书.边撸代码.效果还不错.现在发现.边看书.边设计类图.效果也不错哈.最好书上有类图.自己刚开始可以依葫芦画瓢. 用到的工具是PowerDesigner... 先新建一个类图.文件& ...