让redis也能实现数据库才有的増删改、查询过滤功能

haitao 2014-01-08 04:47:52
加精
搜了一下,似乎没有redis的分版,正好方便从sql向redis转换的同行,就发在本版吧

redis性能强悍,多机方便,但是这种key=value机制的系统,似乎无法实现数据库特有的増删改、查询过滤

经过几天学习,似乎找到一个办法。。。。
(虽然有点累赘,但胜于无。也算是redis初学者的抛砖引玉)

例子:表man,字段id、姓名、年龄、城市
记录数据:
1 zhangsan 20 bj
2 lisi 25 sh
3 wangwu 20 sz
4 zhaoliu 25 bj
5 maqi 20 sz

批量insert:
hset man.1 name zhangsan age 20 city bj
hset man.2 name lisi age 25 city sh
hset man.3 name wangwu age 20 city sz
hset man.4 name zhaoliu age 25 city bj
hset man.5 name maqi age 20 city sz
//建立索引
sadd man.age.20 1 3 5
sadd man.age.25 2 4

sadd man.name.zhangsan 1
sadd man.name.lisi 2
sadd man.name.wangwu 3
sadd man.name.zhaoliu 4
sadd man.name.maqi 5

sadd man.city.bj 1 4
sadd man.city.sh 2
sadd man.city.sz 2 5

insert单人:
6 zhouba 22 sz
hset man.6 name zhouba age 22 city sz
sadd man.age.22 6
sadd man.name.zhouba 6
sadd man.city.sz 6

delete:3
t=hget man.3 name
srem man.name.[t] 3
t=hget man.3 age
srem man.age.[t] 3
t=hget man.3 city
srem man.city.[t] 3
del man.3

update:4 city:bj->sh
t=hget man.4 city
srem man.city.[t] 4
sadd man.city.sh 4
hset man.4 city sh

查询:city=sz 的人
smembers man.city.sz

查询:age=20 的人
smembers man.age.20

查询:city=sz and age=20 的人
sinter man.city.sz man.city.20

...全文
25491 45 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
45 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
使用springDataJpa对redis数据库进行范围查询,但是Between, Like , Containing这些关键字都不能用,有什么方法么?
akxj2022 2016-07-11
  • 打赏
  • 举报
回复
学习了~ 好好学习,天天向上
谁是您好 2015-07-30
  • 打赏
  • 举报
回复
小白问个白痴问题,这些代码要在哪儿写???
xinxinlong 2015-04-15
  • 打赏
  • 举报
回复
引用 41 楼 xinxinlong 的回复:
t=hget man.3 name srem man.name.[t] 3 t=hget man.3 age srem man.age.[t] 3 t=hget man.3 city srem man.city.[t] 3 del man.3
这几句中的t应该做何解释呢?
xinxinlong 2015-04-15
  • 打赏
  • 举报
回复
t=hget man.3 name srem man.name.[t] 3 t=hget man.3 age srem man.age.[t] 3 t=hget man.3 city srem man.city.[t] 3 del man.3
xinxinlong 2015-04-15
  • 打赏
  • 举报
回复
引用 32 楼 sirzxj 的回复:
hset man.1 zhangsan age 20 city bj (error) ERR wrong number of arguments for 'hset' command 这个语法对么
应该是hmset man.1 zhangsan age 20 city bj
完美算法 2014-10-26
  • 打赏
  • 举报
回复
这样也可以啊
完美算法 2014-10-26
  • 打赏
  • 举报
回复
这样也可以啊
jsongo 2014-07-09
  • 打赏
  • 举报
回复
Excellent....解决了我一直在找的解决方案。。
sirzxj 2014-04-28
  • 打赏
  • 举报
回复
引用 35 楼 sz_haitao 的回复:
[quote=引用 34 楼 sirzxj 的回复:] [quote=引用 33 楼 sirzxj 的回复:] [quote=引用 30 楼 sz_haitao 的回复:] [quote=引用 29 楼 u011015550 的回复:] 为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
搜一下就知道了 与mssql无关,甚至与sql都无关 纯粹是独立的key=value格式的存储中心,但是访问效率极高[/quote]

hset man.1 zhangsan age 20 city bj
(error) ERR wrong number of arguments for 'hset' command
这个语法对么[/quote] 在线急等[/quote] 嗯,应该是 hmset hmget(key, field1, …,field N):返回名称为key的hash中field i对应的value hmset(key, field1, value1,…,field N, value N):向名称为key的hash中添加元素field i<—>value i [/quote] 恩恩,可以了,感谢~
haitao 2014-04-28
  • 打赏
  • 举报
回复
引用 34 楼 sirzxj 的回复:
[quote=引用 33 楼 sirzxj 的回复:] [quote=引用 30 楼 sz_haitao 的回复:] [quote=引用 29 楼 u011015550 的回复:] 为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
搜一下就知道了 与mssql无关,甚至与sql都无关 纯粹是独立的key=value格式的存储中心,但是访问效率极高[/quote]

hset man.1 zhangsan age 20 city bj
(error) ERR wrong number of arguments for 'hset' command
这个语法对么[/quote] 在线急等[/quote] 嗯,应该是 hmset hmget(key, field1, …,field N):返回名称为key的hash中field i对应的value hmset(key, field1, value1,…,field N, value N):向名称为key的hash中添加元素field i<—>value i
sirzxj 2014-04-28
  • 打赏
  • 举报
回复
引用 33 楼 sirzxj 的回复:
[quote=引用 30 楼 sz_haitao 的回复:] [quote=引用 29 楼 u011015550 的回复:] 为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
搜一下就知道了 与mssql无关,甚至与sql都无关 纯粹是独立的key=value格式的存储中心,但是访问效率极高[/quote]

hset man.1 zhangsan age 20 city bj
(error) ERR wrong number of arguments for 'hset' command
这个语法对么[/quote] 在线急等
sirzxj 2014-04-28
  • 打赏
  • 举报
回复
引用 30 楼 sz_haitao 的回复:
[quote=引用 29 楼 u011015550 的回复:] 为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
搜一下就知道了 与mssql无关,甚至与sql都无关 纯粹是独立的key=value格式的存储中心,但是访问效率极高[/quote]

hset man.1 zhangsan age 20 city bj
(error) ERR wrong number of arguments for 'hset' command
这个语法对么
sirzxj 2014-04-28
  • 打赏
  • 举报
回复
hset man.1 zhangsan age 20 city bj (error) ERR wrong number of arguments for 'hset' command 这个语法对么
楊松坤 2014-02-19
  • 打赏
  • 举报
回复
/( * W * )\ 小白兔 QQ:125004485
Neo_whl 2014-02-13
  • 打赏
  • 举报
回复
为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
haitao 2014-02-13
  • 打赏
  • 举报
回复
引用 29 楼 u011015550 的回复:
为毛redis我都没听说过,难道是05以上版本中的工具,孤陋寡闻啊
搜一下就知道了 与mssql无关,甚至与sql都无关 纯粹是独立的key=value格式的存储中心,但是访问效率极高
t101lian 2014-01-13
  • 打赏
  • 举报
回复
不明觉厉,, 学习。。
LongRui888 2014-01-13
  • 打赏
  • 举报
回复
呵呵,之前用过一下mongdb,这个redis应该也是nosql把
幸运小侯子 2014-01-11
  • 打赏
  • 举报
回复
加载更多回复(17)

6,128

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧