redis设计关系数据库


第一次用redis 如何设计数据库的笔记,但是后面觉得还是太麻烦了改用了json,感兴趣可以看一看吧。

前言

最近需要一张用户信息表,因为数据量并不大,想先放在内存中,等需求变更了,再移到磁盘上,或者往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演示,不设计类型的特殊方法的话,演示效果是一样的。

图示

索引/查询:

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

2、根据主键查询记录

直接使用string类型建立主键到id的索引,其实id就是主键,但是我们一般不会用id去找记录,更多的使用account账号去找.

key值 : 域名:表名:列键名:列键值

这样我们直接用get 取得accounts的id 后再去hash中查找记录就行了.

3、其他列索引

最后可以根据表的需要建立一些其他索引,

方法同 2 ,使用类型不一定是set 哪个方便用哪个。

例如 我要统计最近登录的10个用户的信息, 那么我直接用list 的 lrange limit 0 10 就能取到.

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<std::string> records_t; //! vector type to save table records entitys.
typedef std::vector<std::string> 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<cpp_redis::reply> 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<std::string> id_vec = {get_id};
m_redis_client.sadd(m_ids, id_vec); // redis hash type to save records key-value.
std::vector<std::pair<std::string, std::string>> 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<cpp_redis::reply> reply = m_redis_client.hgetall(m_table_name + ":" + id);
m_redis_client.sync_commit(); std::vector<cpp_redis::reply> 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;
}

小结

目前给出了redis增查简单设计方法,更新和删除也是通过redis的基本方法对应设计即可,这里不再详述。

此外,可以看出redis的数据库设计还是比较灵活的,如何设计出最适合我们场景需求且高效的正是它难点所在。

如何使用redis设计关系数据库的更多相关文章

  1. Redis设计与实现(一~五整合版)【搬运】

    Redis设计与实现(一~五整合版) by @飘过的小牛 一 前言 项目中用到了redis,但用到的都是最最基本的功能,比如简单的slave机制,数据结构只使用了字符串.但是一直听说redis是一个很 ...

  2. 《Redis设计与实现》读书笔记

    <Redis设计与实现>读书笔记 很喜欢这本书的创作过程,以开源的方式,托管到Git上进行创作: 作者通读了Redis源码,并分享了详细的带注释的源码,让学习Redis的朋友轻松不少: 阅 ...

  3. 重读redis设计与实现

    重读了一遍redis设计与实现,这次收获也不错,把之前还有些疑惑的点:redis跳跃表的原理.redis持久化的方法.redis复制.redis sentinel.redis集群等,都重新熟悉了一遍, ...

  4. 《Redis设计与实现》

    <Redis设计与实现> 基本信息 作者: 黄健宏 丛书名: 数据库技术丛书 出版社:机械工业出版社 ISBN:9787111464747 上架时间:2014-6-3 出版日期:2014 ...

  5. 《Redis设计与实现》阅读笔记(一)--Redis学习

    Redis学习资料与过程记录 在实习中经常会用到很多Redis,对Redis有了一些模糊的了解,总觉得隔靴搔痒的不痛快,所以决定开始深入的了解Redis,也作为我实习期间的目标. 这篇只是为了占个位置 ...

  6. Redis设计与实现读后感

    看了一下时间,现在是2018年8月22日14:28,看完最后一页内容之后,我简短的停留了一下,任思绪翻飞. redis设计与实现大概看了有12天左右,12天前,我的心里很乱,整个人都处于一种焦虑不安的 ...

  7. 180713-Spring之借助Redis设计访问计数器之扩展篇

    之前写了一篇博文,简单的介绍了下如何利用Redis配合Spring搭建一个web的访问计数器,之前的内容比较初级,现在考虑对其进行扩展,新增访问者记录 记录当前站点的总访问人数(根据Ip或则设备号) ...

  8. 180626-Spring之借助Redis设计一个简单访问计数器

    文章链接:https://liuyueyi.github.io/hexblog/2018/06/26/180626-Spring之借助Redis设计一个简单访问计数器/ Spring之借助Redis设 ...

  9. 【Redis】四、Redis设计原理及相关问题

    (六)Redis设计原理及相关问题   通过前面关于Redis五种数据类型.相关高级特性以及一些简单示例的使用,对Redis的使用和主要的用途应该有所掌握,但是还有一些原理性的问题我们在本部分做一个探 ...

随机推荐

  1. JS禁止右键查看源码,禁止复制,复制内容到剪切板

    有时候我们希望自己的网页源码不被查看,这时需要关掉鼠标的右击事件;有时候我们也希望禁止选择页面内容Ctrl+C复制. 1.禁止右键查看源码; <script> //设置右键事件 funct ...

  2. 【CXF】com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Faul

    在客户端生成代码之后测试出现错误: com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Faul ...

  3. kali linux 下搭建git服务器

    参考:http://www.cnblogs.com/dee0912/p/5815267.html https://www.liaoxuefeng.com/wiki/001373951630592960 ...

  4. Spring使用RMI进行远程方法调用

    (1).我新建了三个项目,SpringRmiApi(存放提供者和消费者共有的xx,例如实体类以及服务接口等等).SpringRmiService(服务提供者).SpringRmiProvider(服务 ...

  5. C# ASP.NET MVC 配置允许跨域访问

    在web.config文件中的 system.webServer 节点下 增加如下配置 <httpProtocol> <customHeaders> <add name= ...

  6. Linux 获取设备树源文件(DTS)里描述的资源【转】

    转自:http://www.linuxidc.com/Linux/2013-07/86839.htm 转自:http://blog.sina.com.cn/s/blog_636a55070101mce ...

  7. OLAP和OLTP的区别(基础知识) 【转】

    联机分析处理 (OLAP) 的概念最早是由关系数据库之父E.F.Codd于1993年提出的,他同时提出了关于OLAP的12条准则.OLAP的提出引起了很大的反响,OLAP作为一类产品同联机事务处理 ( ...

  8. linux更换yum源

    由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除redhat原有的yum rpm - ...

  9. 005_awk案例实战

    一.工作经验总结. (1)日志案例: 10.100.194.39 10.100.194.39 1019-03-16T11:01:04+08:00 www.uuwatch.com^^3FF91DE01B ...

  10. 编译时bad substitution的解决办法

    由于使用的使用的编译器不同导致, 需要使用shell为 #!/bin/bash 即可.