标签:ref 排序 内存 操作模式 原子操作 有趣 request 分布式锁 选项
$ wget http://download.redis.io/releases/redis-3.2.5.tar.gz
$ tar xzf redis-3.2.5.tar.gz
$ cd redis-3.2.5
$ make
(centOS)
yum install gcc
(ubuntu)
apt-get install gcc
Redis key值是二进制安全的,可用任何二进制序列作为key. 但是最好遵守一些规则:
127.0.0.1:6379> set key value
OK
127.0.0.1:6379> get key
"value"
127.0.0.1:6379>
set key value [EX seconds] [PX milliseconds] [NX|XX]
EX seconds – 设置键key的过期时间,单位时秒
PX milliseconds – 设置键key的过期时间,单位时毫秒
NX – 只有键key不存在的时候才会设置key的值
XX – 只有键key存在的时候才会设置key的值
127.0.0.1:6379> set age 100
OK
127.0.0.1:6379> get age
"100"
127.0.0.1:6379> incr age
(integer) 101
127.0.0.1:6379> incrby age 20
(integer) 121
127.0.0.1:6379> decr age
(integer) 120
127.0.0.1:6379> decrby age 10
(integer) 110
127.0.0.1:6379> mset age 100 name ddy
OK
127.0.0.1:6379> mget age name
1) "100"
2) "ddy"
127.0.0.1:6379> getset age 101
"100"
127.0.0.1:6379> exists age
(integer) 1
127.0.0.1:6379> type age
string
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> exists age
(integer) 0
127.0.0.1:6379> type age
none
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> get name
(nil)
List指的是一序列有序元素。Reids lists基于linked Lists实现的(在头部或尾部添加数据,时间复杂度是常量级别的,但相比于Array Lists,按下标取数据较慢)
基于linked Lists实现,原因是:能非常快地在很大的列表上添加元素,在常数的时间取得长度。如果快速访问集合元素很重要,建议使用可排序集合(sorted sets)
127.0.0.1:6379> rpush mylist aa
(integer) 1
127.0.0.1:6379> rpush mylist bb
(integer) 2
127.0.0.1:6379> lrange mylist 0 -1
1) "aa"
2) "bb"
127.0.0.1:6379> lpush mylist 11
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "11"
2) "aa"
3) "bb"
127.0.0.1:6379> rpush mylist cc dd
(integer) 5
127.0.0.1:6379> lpush mylist 00 -1
(integer) 7
127.0.0.1:6379> lrange mylist 0 -1
1) "-1"
2) "00"
3) "11"
4) "aa"
5) "bb"
6) "cc"
7) "dd"
127.0.0.1:6379> rpop mylist
"dd"
127.0.0.1:6379> lpop mylist
"-1"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "00"
2) "11"
3) "aa"
4) "bb"
5) "cc"
127.0.0.1:6379> BLPOP mytask 0
会一直等待,直到在有数据进入mytask
127.0.0.1:6379> lpush mytask task1
(integer) 1
127.0.0.1:6379
刚刚那个才执行完
127.0.0.1:6379> BLPOP mytask 0
1) "mytask"
2) "task1"
(141.62s)
127.0.0.1:6379> LTRIM mylist 0 2
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "00"
2) "11"
3) "aa"
127.0.0.1:6379> sadd myset 1 2 3
(integer) 3
127.0.0.1:6379> smembers myset
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> srandmember myset 2
1) "2"
2) "3"
127.0.0.1:6379> sismember myset 1
(integer) 0
127.0.0.1:6379> sismember myset 4
(integer) 0
127.0.0.1:6379> scard myset
(integer) 3
127.0.0.1:6379> srem myset 1
(integer) 0
127.0.0.1:6379> sismember myset 1
(integer) 0
127.0.0.1:6379> scard myset
(integer) 2
假设
梅花clubs 方块diamonds 红桃hearts 黑桃spades
127.0.0.1:6379> hmset user:1001 name wdanny age 18 gender 女
OK
127.0.0.1:6379> hget user:1001 name
"wdanny"
127.0.0.1:6379> hmget user:1001 name gender
1) "wdanny"
2) "\xe5\xa5\xb3"
127.0.0.1:6379> hgetall user:1001
1) "name"
2) "wdanny"
3) "age"
4) "18"
5) "gender"
6) "\xe5\xa5\xb3"
127.0.0.1:6379> hincrby user:1001 age 2
(integer) 20
127.0.0.1:6379> hget user:1001 age
"20"
[root@t106 src]# ./redis-cli hincrby user:1001 age 2
(integer) 21
[root@t106 src]# ./redis-cli --raw hincrby user:1001 age 2
23
[root@t106 src]# ./redis-cli -h 192.168.0.25 -p 6379 ping
PONG
[root@t106 src]# ./redis-cli lpush mylist a b c d
(integer) 11
[root@t106 src]# ./redis-cli --csv lrange mylist 0 -1
"d","c","b","a","-1","00","11","aa","bb","cc","dd"
[root@t106 src]# ./redis-cli
127.0.0.1:6379> dbsize
(integer) 12
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 0
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> dbsize
(integer) 12
127.0.0.1:6379>
127.0.0.1:6379> 5 incr mycounter
(integer) 1
(integer) 2
(integer) 3
(integer) 4
(integer) 5
127.0.0.1:6379> help @set
[root@t106 src]# ./redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
13 803.05K 1 0 172 (+0) 15
[root@t106 src]# ./redis-cli --bigkeys
[root@t106 src]# ./redis-cli --scan
[root@t106 src]# ./redis-cli monitor
[root@t106 src]# ./redis-cli --latency
keyword argument1 argument2 ... argumentN
slaveof 127.0.0.1 6380
requirepass "hello world"
./redis-server --port 6380 --slaveof 127.0.0.1
help config set
help config get
127.0.0.1:6379> config get port
1) "port"
2) "6379"
127.0.0.1:6379> config get *
127.0.0.1:6379> config get slowlog-max-len
1) "slowlog-max-len"
2) "128"
127.0.0.1:6379> config set slowlog-max-len 256
OK
127.0.0.1:6379> config get slowlog-max-len
1) "slowlog-max-len"
2) "256"
127.0.0.1:6379> config set slowlog-max-len 128
OK
127.0.0.1:6379> config get slowlog-max-len
1) "slowlog-max-len"
2) "128"
这两种方法各有优缺点,数据一般分别保存为dump.rdb文件与.aof文件,建议定期备份。
数据持久化,空难恢复相关内容,下次再讲。
标签:ref 排序 内存 操作模式 原子操作 有趣 request 分布式锁 选项
原文地址:https://www.cnblogs.com/wood-life/p/10290905.html