标签:
/Users/baidu/Documents/Data/Interview/存储-Nosql-Redis-消息队列/Redis实战 中文
先看第二章 数据类型及操作
Redis 的作者 antirez 曾笑称 Redis 为一个数据结构服务器(data structures server),我认为这 是一个非常准确的表述,
Redis 的所有功能就是将数据以其固有的几种结构来保存,并提供 给用户操作这几种结构的接口。
一些操作:set, get, setnx, setex, setrange
$ redis-cli -h 10.117.146.16 -p 6379 10.117.146.16:6379> set testName shanghai OK 10.117.146.16:6379> get testName "shanghai" 10.117.146.16:6379> set testName zhongguo OK 10.117.146.16:6379> get testName "zhongguo" 10.117.146.16:6379> setnx testName beijing (integer) 0 10.117.146.16:6379> get testName "zhongguo" 10.117.146.16:6379> setex testName 3 shanghai OK 10.117.146.16:6379> get testName "shanghai" 10.117.146.16:6379> get testName (nil) 10.117.146.16:6379> set testName zhongguo OK 10.117.146.16:6379> get testName "zhongguo" 10.117.146.16:6379> setrange testName 5 d (integer) 8 10.117.146.16:6379> get testName "zhongduo"
10.117.146.16:6379> mset k1 v1 k2 v2 OK 10.117.146.16:6379> get k1 "v1" 10.117.146.16:6379> get k2 "v2"
msetnx一个不成功,就所有不成功
10.117.146.16:6379> mset k3 v3 k2 v2 OK 10.117.146.16:6379> msetnx k4 v4 k3 v3 (integer) 0 10.117.146.16:6379> get k3 "v3" 10.117.146.16:6379> get k4 (nil)
getset不存在的,返回nil,但是能够设置成功 10.117.146.16:6379> getset k5 v5 (nil) 10.117.146.16:6379> get k5 "v5"
getrange和mget:
10.117.146.16:6379> get testName "zhongduo" 10.117.146.16:6379> getrange testName 0 2 "zho" 10.117.146.16:6379> getrange testName -3 -1 "duo" 10.117.146.16:6379> getrange testName 6 (error) ERR wrong number of arguments for ‘getrange‘ command 10.117.146.16:6379> getrange testName 6 100 "uo" 10.117.146.16:6379> mget k1 k2 k6 1) "v1" 2) "v2" 3) (nil)
incr命令:
10.117.146.16:6379> set age 20 OK 10.117.146.16:6379> incr age (integer) 21 10.117.146.16:6379> get age "21"
10.117.146.16:6379> get age "21" 10.117.146.16:6379> incrby age 5 (integer) 26 10.117.146.16:6379> get age "26" 10.117.146.16:6379> incrby age2 5 (integer) 5 10.117.146.16:6379> get age2 "5"
10.117.146.16:6379> decr age2 (integer) 4 10.117.146.16:6379> decrby age2 3 (integer) 1 decrby可以用incrby 负数来进行
10.117.146.16:6379> append testName .com (integer) 12 10.117.146.16:6379> get testName "zhongduo.com" 10.117.146.16:6379> strlen testName (integer) 12
hashes类型:
Redishash是一个string类型的field和value的映射表.它的添加、删除操作都是O(1()平均)。
10.117.146.16:6379> hset myhash f1 v1 (integer) 1 10.117.146.16:6379> hexists myhash f1 (integer) 1 10.117.146.16:6379> hexists myhash f2 (integer) 0 10.117.146.16:6379> hset myhash f2 v2 (integer) 1 10.117.146.16:6379> hlen myhash (integer) 2
list类型:
Redis 的 list 类型其实就是一个每个子元素都是 string 类型的双向链表。链表的最大长度是(2 的 32 次方)。
我们可以通过 push,pop 操作从链表的头部或者尾部添加删除元素。这使得 list 既可以用作栈,也可以用作队列。
当我们[lr]pop 一个 list 对象时,如果 list 是空, 或者不存在,会立即返回 nil。但是阻塞版本的 b[lr]pop 可以则可以阻塞,
当然可以加超时时 间,超时后也会返回 nil。
lrem:从 key 对应 list 中删除 count 个和 value 相同的元素。 10.117.146.16:6379> lrange mylist 0 -1 1) "hello" 2) "here" 3) "world" 10.117.146.16:6379> lrem mylist 1 ‘here‘ (integer) 1 10.117.146.16:6379> lrange mylist 0 -1 1) "hello" 2) "world"
标签:
原文地址:http://www.cnblogs.com/charlesblc/p/5961518.html