如何使用redis设计关系数据库
如何使用redis设计关系数据库
目录
redis设计关系数据库
前言
设计用户信息表结构
hash存储记录
set存储id
图示
索引/查询:
1、select
查询所有记录 : 类似sql的select from table_name
2、根据主键查询记录
3、其他列索引
c++ 实现
小结
redis设计关系数据库
前言
最近需要一张用户信息表,因为数据量并不大,想先放在内存中,等需求变更了,再移到磁盘上,或者往mysql塞,那么问题来了,怎么用redis的数据类型设计一个关系数据库呢。
redis只有key-value这种存储结构,如果想利用它做成想其他数据库一样具备 增删改查等功能只能再设计了,这里分享一下我的设计方法,比较简单,我不知道算不算好,只是网上找了很久没找到一种通用的方法,如果有什么好的方法,还想请教一下各位,十分感谢。
设计用户信息表结构
hash存储记录
key值 : 域名:表名:主键
value值 :直接使用redis的Hash类型
如:
test:accounts_info:0 id 0 accounts ailumiyana_0 password 123456 nick_name sola_0
test:accounts_info:1 id 1 accounts ailumiyana_1 password 123456 nick_name sola_1
test:accounts_info:2 id 2 accounts ailumiyana_2 password 123456 nick_name sola_2
test:accounts_info:3 id 3 accounts ailumiyana_3 password 123456 nick_name sola_3
在这里插入图片描述
set存储id
另添加一个set集存放表主键, 也即id.
key值 : ids:域名:表名
value值: id
将已生成的用户id同时添加进set集合中.
我这里用了list演示,不设计类型的特殊方法的话,演示效果是一样的。
Alt text
图示
Alt text
索引/查询:
1、select
查询所有记录 : 类似sql的select from table_name
有了set表后我们就可以使用redis中sort的get方法,获取所有记录.
sort ids:test:accounts_info get test:accounts_info:->accounts get test:accounts_info:->nick_name
Alt text
2、根据主键查询记录
直接使用string类型建立主键到id的索引,其实id就是主键,但是我们一般不会用id去找记录,更多的使用account账号去找.
key值 : 域名:表名:列键名:列键值
这样我们直接用get 取得accounts的id 后再去hash中查找记录就行了.
在这里插入图片描述
3、其他列索引
最后可以根据表的需要建立一些其他索引,
方法同 2 ,使用类型不一定是set 哪个方便用哪个。
例如 我要统计最近登录的10个用户的信息, 那么我直接用list 的 lrange limit 0 10 就能取到.
Alt text
c++ 实现
以上设计的c++实现,其中的redis的客户端使用了cpp_redis库。
例程中 :
1、我创建了一张 account_info的表 默认使用accounts 作为主键
2、插入4个用户信息.
3、查询用户ailu_1的记录值.
class table// : public redis::er_table
{
public:
//! ctor
table(void);
//! dtor
~table(void) = default;
//! copy ctor
table(const table&) = delete;
//! assignment operator
table& operator=(const table&) = delete;
public:
//! vector type to save table records.
typedef std::vector records_t;
//! vector type to save table records entitys.
typedef std::vector entitys_t;
public:
//! create table,
//! default primary key is the records_t vec[0], if primary_key is empty!
void create(const std::string& table_name, const records_t& vec, const std::string& primary_key = "");
public:
//! incr primary key id.
std::string incr_id();
//! insert new entity to table, pelease orderly insert refer to records vector !
//! return false while entity exits.
bool insert(const entitys_t& vec);
//! get entitys by primary key value.
entitys_t get_entitys_by_primary_key_value(const std::string& primary_key_value);
private:
//! get id by primary key value
//! retrun "" while primary key inexitences.
std::string get_id_by_primary_key_value(const std::string& primary_key_value);
private:
//! redis client
cpp_redis::client m_redis_client;
//!
records_t m_records;
//! records count.
std::size_t m_records_count;
//! ids set key
std::string m_ids;
//! table name
std::string m_table_name;
//! incr key
uint64_t m_incr_key;
//! primary key
std::string m_primary_key;
std::size_t m_primary_key_index;
};
table::table()
:m_records_count(0),
m_incr_key(0)
{
m_redis_client.connect();
m_redis_client.select(3);
m_redis_client.sync_commit();
}
void table::create(const std::string& table_name, const records_t& vec, const std::string& primary_key){
assert(m_records_count == 0);
m_ids = "ids:" + table_name;
m_table_name = table_name;
m_records = vec;
m_records_count = vec.size();
if(primary_key.empty()){
m_primary_key = vec[0]; m_primary_key_index = 0;
} else {
m_primary_key = primary_key; auto iter = std::find(vec.begin(), vec.end(), primary_key); if(iter == vec.end()){ LOG_FATAL << "no such key."; } m_primary_key_index = iter - vec.begin();
}
}
std::string table::incr_id(){
return std::move(std::to_string(m_incr_key++));
}
std::string table::get_id_by_primary_key_value(const std::string& primary_key_value){
std::future fu = m_redis_client.get(primary_key_value);
m_redis_client.sync_commit();
cpp_redis::reply reply = fu.get();
if(!reply.is_null()){
return std::move(reply.as_string());
}
LOG_DEBUG << "primary_key " << primary_key_value << " inexitences. return "".";
return "";
}
bool table::insert(const entitys_t& vec){
assert(m_records_count != 0);
assert(m_records_count == vec.size());
std::string get_id = incr_id();
// check whether the primary key already exists.
std::string check_id = get_id_by_primary_key_value(vec[m_primary_key_index]);
if(!check_id.empty()){
return false;
}
// redis string type primary key to id index.
//LOG_DEBUG << m_table_name + ":" + m_records[m_primary_key_index] + ":" + vec[m_primary_key_index];
m_redis_client.set(m_table_name + ":" + m_records[m_primary_key_index] + ":" + vec[m_primary_key_index], get_id);
// redis set type to save id.
std::vector id_vec = {get_id};
m_redis_client.sadd(m_ids, id_vec);
// redis hash type to save records key-value.
std::vector> entitys_pair_vec;
for(std::size_t i = 0; i < m_records_count; i++){
entitys_pair_vec.emplace_back(make_pair(m_records[i], vec[i]));
}
m_redis_client.hmset(m_table_name + ":" + get_id, entitys_pair_vec);
m_redis_client.sync_commit();
return true;
}
table::entitys_t table::get_entitys_by_primary_key_value(const std::string& primary_key_value){
std::string id = get_id_by_primary_key_value(m_table_name + ":" + m_records[m_primary_key_index] + ":" + primary_key_value);
if(id == ""){
static entitys_t static_empty_entitys_vec; return static_empty_entitys_vec; LOG_ERROR << "no this entitys";
}
entitys_t vec;
std::future reply = m_redis_client.hgetall(m_table_name + ":" + id);
m_redis_client.sync_commit();
std::vector v = reply.get().as_array();
auto iter = v.begin();
for(iter++; iter < v.end(); iter += 2){
//LOG_DEBUG << (*iter).as_string(); vec.emplace_back((*iter).as_string());
}
return std::move(vec);
}
int main()
{
table accounts_info;
table::records_t records_vec = {"id", "accounts", "password", "nick_name"};
accounts_info.create("sip:accounts_info", records_vec, "accounts");
table::entitys_t entitys_vec0 = {"0", "ailu_0", "123", "sola_0"};
accounts_info.insert(entitys_vec0);
table::entitys_t entitys_vec1 = {"1", "ailu_1", "123", "sola_1"};
accounts_info.insert(entitys_vec1);
table::entitys_t entitys_vec2 = {"2", "ailu_2", "123", "sola_2"};
accounts_info.insert(entitys_vec2);
table::entitys_t entitys_vec3 = {"3", "ailu_3", "123", "sola_3"};
accounts_info.insert(entitys_vec3);
table::entitys_t ailu_1_accounts = accounts_info.get_entitys_by_primary_key_value("ailu_1");
auto it = ailu_1_accounts.begin();
while(it != ailu_1_accounts.end()){
std::cout << *it << std::endl; it++;
}
getchar();
return 0;
}
Alt text
Alt text
小结
目前给出了redis增查简单设计方法,更新和删除也是通过redis的基本方法对应设计即可,这里不再详述。
此外,可以看出redis的数据库设计还是比较灵活的,如何设计出最适合我们场景需求且高效的正是它难点所在。
作者 —— 艾露米婭娜
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
总结JAVA----IO流中的字节流
对于IO流中字节流的总结 字节流的概念 由于应用程序,经常需要和文件打交道,所以Inputstream专门提供了读写文件的子类:FileInputStream和FileOutputStream类,如果程序对于文件的操作较为简单,就比如说只是顺序的读写文件,那么就可以使用字节流进行操作.并且我们都知道,所有文件的最底层数据类型都是字节,所以说,字节流的应用范围是十分广的,当然,它也有相应的缺点,只能处理简单的数据文本,一旦数据多了,其处理速度慢的弊端也就显现了出来. 子父类关系 通过超级父类InputStream延伸出的很多派系,并且一些派系完美的解决了,字节流速度慢的问题(字节缓冲流) 编码方式 查阅了挺久资料的,发现在编码方式这块,由于我还没有接触计算机原理这门课,所以对于这块的概念十分的模糊,从昨天到今天也查阅了许多资料,但还是无法很系统的整理出一些真正有用的东西,所以对于编码方式这里,我会在以后认真的总结一番。 其与字符流的不同 1. 字节流传输的最基本单位是字节 字符流传输的最基本单位是字符 2. 编码方式不同 3. 字符流在传输方面上,由于计算机的传输本质都是字节,而一个字符...
- 下一篇
好程序员分享JavaScript事件委托代理和函数封装详解
好程序员分享JavaScript事件委托代理和函数封装详解,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件。下面我们一起看看事件代理相关介绍。 当用事件委托的时候,根本就不需要去遍历元素的子节点,只需要给父级元素添加事件就好了,其他的都是在js里面的执行,这样可以大大的减少dom操作,这才是事件委托的精髓所在。 基本概念 事件委托,通俗地来讲,就是把一个元素响应事件(click、keydown......)的函数委托到另一个元素; 一般来讲,会把一个或者一组元素的事件委托到它的父层或者更外层元素上,真正绑定事件的是外层元素,当事件响应到需要绑定的元素上时,会通过事件冒泡机制从而触发它的外层元素的绑定事件上,然后在外层元素上去执行函数。 举个例子,比如一个宿舍的同学同时快递到了,一种方法就是他们都傻傻地一个个去领取,还有一种方法就是把这件事情委托给宿舍长,让一个人出去拿好所有快递,然后再根据收件人一一分发给每个宿舍同学; 在这里,取快递就是一个事件,每个同学指的是需要响应事件的 DOM 元素,而出去统一领取快递的宿舍长...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7安装Docker,走上虚拟化容器引擎之路