Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map),列表(list),集合(sets)和 有序集合(sorted sets)等类型。
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make
将daemonize on
修改为
daemonize yes
src/redis-server redis.conf
执行安装脚本,一直默认就可以
./utils/install_server.sh
mv /etc/init.d/redis_6379 /etc/init.d/redis
vim /etc/redis/6379.conf
requirepass redispass
service redis restart
再次连接发现需要输入密码
keys pattern
keys还支持通配符
127.0.0.1:6379> set we "hello"
OK
127.0.0.1:6379> keys *
1) "we"
127.0.0.1:6379> set name wanger
OK
127.0.0.1:6379> keys name
1) "name"
127.0.0.1:6379> keys *
1) "name"
2) "we"
keys 命令遍历了Redis中所有的键,当键的数量过多时会影响Redis性能
del key1 key2 ..
例如:
127.0.0.1:6379> keys *
1) "qwe"
2) "asd"
3) "we"
127.0.0.1:6379> del asd qwe
(integer) 2
127.0.0.1:6379> keys *
1) "we"
exists key1 key2
例如:
127.0.0.1:6379> exists we
(integer) 1
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> set qwe 2
OK
127.0.0.1:6379> exists we qwe
(integer) 2
127.0.0.1:6379> exists we name
(integer) 1
dbsize
例如:
127.0.0.1:6379> dbsize
(integer) 2
127.0.0.1:6379> keys *
1) "qwe"
2) "we"
type key
例如:
127.0.0.1:6379> type we
string
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> type list1
list
sort key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
例如:
127.0.0.1:6379> lrange list 0 -1
1) "5"
2) "7"
3) "2"
4) "4"
5) "3"
6) "1"
sort list desc limit 0 5
1) "7"
2) "5"
3) "4"
4) "4"
5) "3"
127.0.0.1:6379> lpush list2 asd qwe zxc
(integer) 3
sort list2 desc limit 0 5 alpha
1) "zxc"
2) "qwe"
3) "asd"
flushdb //清空当前数据库
flushall //清空所有数据库
例如:
127.0.0.1:6379[11]> set a 1
OK
127.0.0.1:6379[11]> get a
"1"
127.0.0.1:6379[11]> flushdb
OK
127.0.0.1:6379[11]> get a
(nil)
move key db
例如:
127.0.0.1:6379[11]> set a 1
OK
127.0.0.1:6379[11]> move a 2
(integer) 1
127.0.0.1:6379[11]> select 2
OK
127.0.0.1:6379[2]> get a
"1"
字符串类型是Redis最基础的数据结构,字符串类型是其他几种数据类型的基础,他能存储任何形式的字符串,包括二进制数据
set key value [EX seconds] [PX milliseconds] [NX|XX]
get key
例如:
127.0.0.1:6379> set name wanger
OK
127.0.0.1:6379> get name
"wanger"
127.0.0.1:6379> setnx name wanger
(integer) 0
127.0.0.1:6379> set name wang xx
OK
mset key1 value1 key2 value2 ..
mget key1 key2
例如:
127.0.0.1:6379> mset key1 1 key2 2
OK
127.0.0.1:6379> mget key1 key2
1) "1"
2) "2"
incr key
decr key
例如:
127.0.0.1:6379> incr key1
(integer) 2
127.0.0.1:6379> incr key2
(integer) 3
127.0.0.1:6379> get key1
"2"
127.0.0.1:6379> get key2
"3"
127.0.0.1:6379> get we
"hello"
127.0.0.1:6379> incr we
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr key1
(integer) 1
127.0.0.1:6379> decr key2
(integer) 2
append key value
例如:
127.0.0.1:6379> append key hello
(integer) 5
127.0.0.1:6379> append key world
(integer) 10
127.0.0.1:6379> get key
"helloworld"
strlen key
例如:
127.0.0.1:6379> get key
"helloworld"
127.0.0.1:6379> strlen key
(integer) 10
127.0.0.1:6379> set name "王二"
OK
127.0.0.1:6379> strlen name
(integer) 6
setrange key offset value
getrange key start end
例如:
127.0.0.1:6379> SET key1 "Hello World"
OK
127.0.0.1:6379> setrange key1 6 "Redis"
(integer) 11
127.0.0.1:6379> get key1
"Hello Redis"
127.0.0.1:6379> getrange key1 6 12
"Redis"
字符串类型的内部编码有3种:
例如:
127.0.0.1:6379> set num 123456
OK
127.0.0.1:6379> object encoding num
"int"
127.0.0.1:6379> set short qweasd
OK
127.0.0.1:6379> object encoding short
"embstr"
127.0.0.1:6379> set raw "when you love me I have lost of plot wow wow"
OK
127.0.0.1:6379> object encoding raw
"raw"
Redis列表可以存储一个有序的字符串列表,内部使用双向链表实现,双向链表作为一种常见的数据结构,双向链表的每个数据节点都有两个指针,分别指向后继与前驱节点,从双向链表中的任意一个节点开始都可以很方便地访问其前驱与后继节点,因此获取越接近两端的元素就越快
lpush key value1 value2 value3
rpush key value1 value2 value3
linsert key BEFORE|AFTER pivot value
例如:
127.0.0.1:6379> lpush names 1 2 3 4
(integer) 4
127.0.0.1:6379> lrange names 0 4
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> rpush nums 1 2 3 4
(integer) 4
127.0.0.1:6379> lrange nums 0 4
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> linsert nums before 2 5
(integer) 5
127.0.0.1:6379> lrange nums 0 5
1) "1"
2) "5"
3) "2"
4) "3"
5) "4"
lpop key
rpop key
例如:
127.0.0.1:6379> lrange nums 0 5
1) "1"
2) "5"
3) "2"
4) "3"
5) "4"
127.0.0.1:6379> lpop nums
"1"
127.0.0.1:6379> rpop nums
"4"
127.0.0.1:6379> lrange nums 0 5
1) "5"
2) "2"
3) "3"
lindex key index
例如:
127.0.0.1:6379> lrange nums 0 5
1) "5"
2) "2"
3) "3"
127.0.0.1:6379> lindex nums 2
"3"
127.0.0.1:6379> lindex nums 1
"2"
127.0.0.1:6379> lindex nums -1
"3"
lrange key start stop
例如:
127.0.0.1:6379> lrange nums 0 1
1) "5"
2) "2"
127.0.0.1:6379> lrange nums 0 2
1) "5"
2) "2"
3) "3"
llen key
例如:
127.0.0.1:6379> lrange nums 0 3
1) "5"
2) "2"
3) "3"
127.0.0.1:6379> llen nums
(integer) 3
lrem key count value
例如:
127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "2"
7) "3"
8) "4"
9) "5"
10) "5"
127.0.0.1:6379> lrem mylist 1 5
(integer) 1
127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "2"
6) "3"
7) "4"
8) "5"
9) "5"
127.0.0.1:6379> lrem mylist -2 5
(integer) 2
127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "2"
6) "3"
7) "4"
127.0.0.1:6379> lrem mylist 0 2
(integer) 2
127.0.0.1:6379> lrange mylist 0 11
1) "1"
2) "3"
3) "4"
4) "3"
5) "4"
lset key index value
例如:
127.0.0.1:6379> lrange mylist 0 6
1) "1"
2) "3"
3) "4"
4) "3"
5) "4"
127.0.0.1:6379> lset mylist 1 5
OK
127.0.0.1:6379> lrange mylist 0 6
1) "1"
2) "5"
3) "4"
4) "3"
5) "4"
blpop和brpop分别是lpop和rpop的阻塞版本,功能类似,当列表为空时,会发生阻塞,timeout可定义阻塞时间,timeout为0时将一直阻塞,直到在另一个客户端中往列表中加入元素
blpop key1 key2 timeout
brpop key1 key2 timeout
例如:
127.0.0.1:6379> lrange list2 0 4
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> lrange list1 0 4
1) "8"
2) "7"
127.0.0.1:6379> blpop list1 list2 0
1) "list1"
2) "8"
127.0.0.1:6379> brpop list1 list2 0
1) "list1"
2) "7"
127.0.0.1:6379> brpop list1 list2 0
1) "list2"
2) "2"
127.0.0.1:6379> brpop list1 list2 0
1) "list2"
2) "3"
127.0.0.1:6379> brpop list1 list2 0
1) "list2"
2) "4"
127.0.0.1:6379> brpop list1 list2 0
1) "list2"
2) "1"
(18.49s)
在另一个客户端执行
127.0.0.1:6379> lpush list2 1
(integer) 1
参考张铁蕾http://zhangtielei.com/posts/blog-redis-quicklist.html
哈希是由与值关联的字段组成的映射。字段和值都是字符串,哈希类型中的映射关系叫作field-value
hset key field value
hget key field
例如:
127.0.0.1:6379> hset ha name wanger
(integer) 1
127.0.0.1:6379> hget ha name
"wanger"
hmset key field1 value1 field2 value2
hmget key field1 field2
例如:
127.0.0.1:6379> hmset he name wanger sex nan
OK
127.0.0.1:6379> hmget he name sex
1) "wanger"
2) "nan"
hdel key field1 field2
例如:
127.0.0.1:6379> hdel he name
(integer) 1
127.0.0.1:6379> hget he name
(nil)
hlen key
例如:
127.0.0.1:6379> hmset he name wanger sex nan age 18
OK
127.0.0.1:6379> hlen he
(integer) 3
hgetall key
例如:
127.0.0.1:6379> hgetall he
1) "sex"
2) "nan"
3) "name"
4) "wanger"
5) "age"
6) "18"
hkeys key
例如:
127.0.0.1:6379> hkeys he
1) "sex"
2) "name"
3) "age"
hexists key field
例如:
127.0.0.1:6379> hexists he name
(integer) 1
127.0.0.1:6379> hexists he sex
(integer) 1
hincrby key field increment
例如:
127.0.0.1:6379> hincrby asd asdf 2
(integer) 3
127.0.0.1:6379> hget asd asdf
"3"
127.0.0.1:6379> hincrby asd asdf 2
(integer) 5
127.0.0.1:6379> hget asd asdf
"5"
hvals key
例如:
127.0.0.1:6379> hvals he
1) "nan"
2) "wanger"
3) "18"
例如:
127.0.0.1:6379> hset ziplist hash 12335452335235fwgvsfwbhfbwhhfwuesrfhwueywhufgbrewfghusfhwueughsajkifo34ejigji
(integer) 1
127.0.0.1:6379> hmset hash asd fcfg zdf fty
OK
唯一且无序的字符串元素的集合。
sadd key member1 member2
例如
127.0.0.1:6379> sadd set s1 s2
(integer) 0
smembers key
例如:
127.0.0.1:6379> smembers set
1) "s2"
2) "s1"
3) "s3"
srem key member1 members2
例如:
127.0.0.1:6379> srem set s2
(integer) 1
127.0.0.1:6379> smembers set
1) "s1"
2) "s3"
scard key
例如:
127.0.0.1:6379> scard set
(integer) 2
srandmember key [count]
例如:
127.0.0.1:6379> srandmember set 1
1) "s1"
sismember key member
例如:
127.0.0.1:6379> sismember set s2
(integer) 0
127.0.0.1:6379> sismember set s3
(integer) 1
spop key [count]
例如:
127.0.0.1:6379> smembers set
1) "s5"
2) "s4"
3) "s1"
4) "s3"
127.0.0.1:6379> spop set
"s4"
127.0.0.1:6379> spop set 3
1) "s5"
2) "s1"
3) "s3"
127.0.0.1:6379> smembers set
(empty list or set)
sunion key1 key2
例如:
127.0.0.1:6379> sadd set1 1 2 3
(integer) 3
127.0.0.1:6379> sadd set2 2 3 4 5
(integer) 4
127.0.0.1:6379> sunion set1 set2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
sinter key1 key2
例如:
127.0.0.1:6379> sinter set1 set2
1) "2"
2) "3"
sdiff key1 key2
例如:
127.0.0.1:6379> sdiff set1 set2
1) "1"
127.0.0.1:6379> sdiff set2 set1
1) "4"
2) "5"
例如:
127.0.0.1:6379> sadd set3 s1 s2 s3
(integer) 3
127.0.0.1:6379> object encoding set3
"hashtable"
127.0.0.1:6379> sadd set4 1 2 3
(integer) 3
127.0.0.1:6379> object encoding set4
"intset"
每个字符串元素都与一个称为score的浮点值相关联。元素总是按它们的分数排序,因此与Sets不同,可以检索一系列元素
zadd key [NX|XX] [CH] [INCR] score1 member1 score2 member2
例如:
127.0.0.1:6379> zadd score xx 80 wanger 80 huazai 95 dongdong +inf a
(integer) 0
127.0.0.1:6379> zrange score 0 -1
1) "huazai"
2) "wanger"
3) "dongdong"
4) "a"
+inf和-inf分别表示正无穷和负无穷
zscore key member
例如:
127.0.0.1:6379> zscore score dongdong
"95"
zrange key start end [withscores] #升序排列
zrevrange key start end [withscores] #降序排列
例如:
127.0.0.1:6379> zadd score 95 huazai 90 wanger 85 dongdong +inf a
(integer) 0
127.0.0.1:6379> zrank score huazai
(integer) 2
127.0.0.1:6379> zrevrank score huazai
(integer) 1
127.0.0.1:6379> zrevrank score a
(integer) 0
127.0.0.1:6379> zrange score 0 -1 WITHSCORES
1) "dongdong"
2) "85"
3) "wanger"
4) "90"
5) "huazai"
6) "95"
7) "a"
8) "inf"
zcard key
例如:
127.0.0.1:6379> zcard score
(integer) 4
zrem key member1 member2
例如:
127.0.0.1:6379> zrem score dongdong
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "wanger"
2) "huazai"
3) "a"
zincrby key increment member
例如:
127.0.0.1:6379> zincrby score 5 wanger
"95"
zrangebyscore key min max [withscores] [limit offset count] #升序
zrevrangebyscore key max min [withscores] [limit offset count] #降序
例如:
127.0.0.1:6379> zrangebyscore score 80 90 withscores
1) "dongdong"
2) "85"
3) "wanger"
4) "90"
127.0.0.1:6379> zrevrangebyscore score 95 80 withscores
1) "huazai"
2) "95"
3) "wanger"
4) "90"
5) "dongdong"
6) "85"
zcount key min max
例如:
127.0.0.1:6379> zcount score 85 95
(integer) 3
zremrangebyscore key min max
例如:
127.0.0.1:6379> zremrangebyscore score 85 90
(integer) 2
zinterstore destination numkeys key1 key2 [WEIGHTS weight]
最终的结果就是权重乘分数,之后再进行聚合
例如:
127.0.0.1:6379> zadd user 10 wanger 20 huazai 30 dongdong
(integer) 3
127.0.0.1:6379> zadd user1 15 wanger 35 huazai
(integer) 2
127.0.0.1:6379> zinterstore userset 2 user user1
(integer) 2
127.0.0.1:6379> zrange userset 0 -1 withscores
1) "wanger"
2) "25"
3) "huazai"
4) "55"
127.0.0.1:6379> zinterstore user2set 2 user user1 weights 1 0.5 aggregate min
(integer) 2
127.0.0.1:6379> zrange user2set 0 -1 withscores
1) "wanger"
2) "7.5"
3) "huazai"
4) "17.5"
zunionstore destination numkeys key [key ...] [WEIGHTS weight]
例如:
127.0.0.1:6379> zadd user 10 wanger 20 huazai 30 dongdong
(integer) 3
127.0.0.1:6379> zadd user1 15 wanger 35 huazai
(integer) 2
127.0.0.1:6379> zunionstore user3set 2 user user1 weights 1 0.5 aggregate max
(integer) 3
127.0.0.1:6379> zrange user3set 0 -1 withscores
1) "wanger"
2) "10"
3) "huazai"
4) "20"
5) "dongdong"
6) "30"
例如:
127.0.0.1:6379> zadd sortset1 10 a 20 b 30 c
(integer) 3
127.0.0.1:6379> object encoding sortset1
"ziplist"
127.0.0.1:6379> zadd sortset2 10 a 20 b 30 cddddddddddddddddddddddffffffffffffffffffffffffwfwfwggggggggggggggggggwg4yhhhhhhhhhhhhhhhh
(integer) 3
127.0.0.1:6379> object encoding sortset2
"skiplist"
欢迎关注我的公号“没有故事的陈师傅”
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。