Dedis数据结构
阅读时间:全文 261 字,预估用时 2 分钟
创作日期:2017-04-02
上篇文章:《Python基础教程》复读笔记(1)
BEGIN
前言
一般使用hash结构较多,尤其是存储对象,非常使用. 比如有多个对象存入redis中(redis中数据读取速度快常用于做数据中转):
man = {
'smoke': 'No',
'wine': 'year, is very good!'
}
woman = {
'smoke': 'No',
'win': 'No'
}
#省略连接操作
#此时用hset存入序列化后的对象到redis
r.hset('human', '男人', json.dumps(man))
r.hset('human', '女人', json.dumps(woman))
在NodeJS中取出redis中的数据:
//省略连接操作
slient.hget('human', '男人', function(err, reply){
//反序列化并解析成js对象赋值给man
man = JSON.parse(reply);
});
//或者取出整个human
slient.hgetall('human', function(err, reply){...})
redis常用数据结构
-
字符串 string
- set str haha
- get str
- del str
-
哈希对象 hash
- hset dog eating yes runing yes speak none
- hdel dog eating
- hgetall dog
-
链表,栈 list
- lpush students lucy
- lpop students
- lrange students 0 10
- lindex students 0
-
集合 set
- sadd students lucy
- smembers students
- sismember students lucy #检查lucy是否在students里
- srem students lucy #移除元素
-
可排序集合 zset
- zadd students 0 lucy
- zrem students lucy #删除元素
- zrange students 0 10
- zrangebyscore students 0 10 #指定分值范围
- 注:同时显示分值后面加 withscores
FINISH
上篇文章:《Python基础教程》复读笔记(1)