仿Redis用来作前端浏览器的数据存储结构
用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用来作前端浏览器的数据存储结构的更多相关文章
- window.name应用于浏览器端数据存储
本代码简单地分享利用window.name实现浏览器端数据存储: 1.在同一个页面一个地方设置window.name = "abc",另外一个地方读取window.name,自然能 ...
- 浏览器本地数据存储解决方案以及cookie的坑
本地数据存储解决方案以及cookie的坑 问题: cookie过长导致页面打开失败 背景: 在公司的项目中有一个需求是打开多个工单即在同一个页面中打开了多个tab(iframe),但是需要在刷新时只刷 ...
- Redis之数据存储结构
今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...
- JS中浏览器的数据存储机制
一.JS中的三种数据存储方式 cookie.sessionStorage.localStorage 二.cookie 1.cookie的定义: cookie是存储在浏览器上的一小段数据,用来记录某些当 ...
- JavaScript浏览器本地数据存储
浏览器本地存储主要使用的是sessionStorage和localStorage.两者都支持,sessionStorage保存的是浏览器和服务器的一次对话信息,只在一次回话中有效.当在新标签页或新窗口 ...
- Redis数据存储结构之String
前言: 在Redis使用中,我们最常使用的操作是set key value,或 get key value .这里面包含了redis最基本的数据类型:String,字符串类型是redis中最基本的类型 ...
- Redis(一):基本数据类型与底层存储结构
最近在整理有关redis的相关知识,对于redis的基本数据类型以及其底层的存储结构简要的进行汇总和备注(主要为面试用) Redis对外提供的基本数据类型主要为五类,分别是 STRING:可以存储字符 ...
- springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发
工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...
- redis数据库如何用Django框架缓存数据
---恢复内容开始--- 一.python 使用redis 1.1 安装 pip install redis 测试有一些基本的数据类型 import redis # redis 是一个缓存数据库 # ...
随机推荐
- 使用js实现显示系统当前时间并实现倒计时功能并触发模态框(遮罩)功能
常常在我们的网页中需要倒计时来触发一些函数,例如遮罩等,在本项目中,通过使用jquery,bootstrap,实现了显示系统当前时间,并且实现了倒计时的功能,倒计时实现后将会弹出模态框(遮罩层).模态 ...
- php特殊用法
1.将字符串转换成可执行的php代码(简单的代码)--eval() <?php $str="echo phpinfo();"; echo eval( $str);
- 软件开发学习笔记 <一> UML
UML http://www.uml-diagrams.org http://www.umlchina.com/index.htm 统一建模语言(UML)始于1997年的一个OMG(对象管理组织)标准 ...
- svn 上传出现Cannot accept non-LF lind endings in 'svn:log'
可能是你到cimmit日志的字有svn不认的,重新把长传日志删掉,重新敲一遍就好了
- Python中的file和open简述
help(file) help(open) 老规矩先看一下内置的帮助文档怎么描述file和open,毕竟官方文档是最直接最准确的描述. Help on class file in module __b ...
- win7下装ubuntu14.04双系统
一.给ubuntu准备安装空间 计算机--右键--管理-磁盘管理--选择一个空余空间较多的磁盘--右键--压缩卷--压缩大概60G空间(接下来ubuntu就会装到这60G里面) 二.制作启动u盘 ...
- Android Studio中JNI程序的单步调试和日志打印
近日有个算法(检测碰撞)需要用C++实现,目的是IOS和ANDROID中共享同一段程序. 下面说说android调用这段程序过程中遇到的一些事情.(过程中网上搜索了一些相关文章,大部分说的是eclip ...
- js 用途
嵌入动态文本于HTML页面.[4] 对浏览器事件做出响应.[4] 读写HTML元素.[4] 在数据被提交到服务器之前验证数据.[4] 检测访客的浏览器信息.[4] 控制cookies,包括创 ...
- GPS模块数据放入谷歌地图显示,不准
GPS 串口读出的是 DDMM.MMMM格式 一般上位机是 DD.DDDDDD°或 DD°MM'SS" 格式, 这两种都可以在 GE 里直接输入 举例说明: 3147.8749 (示例,经纬 ...
- python 面向对象(类)
面向对象,即是将具备某种共性的事物抽象成一个类(模板),然后再根据类来创建实例对象进行具体的使用. 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写, ...