部署Nginx
部署Redis
安裝Redis
redis事物
服務器管理命令
慢查詢日誌
主從複製
Redis-Sentinel
cluser分片集羣
安裝python
操做redis數據
部署Django程序
部署Nginx
配置epel源
參看阿里網https://opsx.alibaba.com/mirror
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.reponode
##epel 配置方法
###一、備份(若有配置其餘epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
二、下載新repo 到/etc/yum.repos.d/
epel(RHEL 7)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
epel(RHEL 6)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
epel(RHEL 5)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repo
1
2
3
4
5
6
7
8
9
10
11
yum repolistpython
[root@safly ~]# yum repolist
Loaded plugins: fastestmirror
epel | 4.7 kB 00:00
(1/3): epel/x86_64/group_gz | 266 kB 00:01
(2/3): epel/x86_64/updateinfo | 917 kB 00:05
(3/3): epel/x86_64/primary_db | 6.3 MB 00:20
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 9,591
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_ 12,509
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 448
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 2,416
repolist: 24,964
[root@safly ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
安裝
yum install sl cowsay -y
安裝nginxlinux
wget http://nginx.org/download/nginx-1.12.2.tar.gz nginx
yum install pcre-devel openssl-devel -y git
#編譯安裝三部曲 : ./configure make make install github
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2 redis
./configure --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module
make
make install
1
2
3
4
5
6
7
8
9
10
11
12
#檢查語法
/application/nginx-1.12.2/sbin/nginx -t數據庫
#啓動nginx
/application/nginx-1.12.2/sbin/nginx
1
2
3
4
5
重啓vim
[root@safly nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -s reload
1
2緩存
部署Redis
安裝Redis
安裝redis
[root@safly /]# ls
application boot dev home lib64 mnt proc run sbin sys usr wyf
bin data etc lib media opt root safly srv tmp var
[root@safly /]# cd /application/
[root@safly application]# ls
nginx-1.12.2
[root@safly application]# wget http://download.redis.io/releases/redis-3.2.10.tar.gz
[root@safly application]# tar xzf redis-3.2.10.tar.gz
[root@safly application]# ls
nginx-1.12.2 redis-3.2.10 redis-3.2.10.tar.gz
[root@safly application]# mv redis-3.2.10 redis
[root@safly application]# rm -rf redis-3.2.10.tar.gz
[root@safly application]# ls
nginx-1.12.2 redis
[root@safly application]# cd redis
[root@safly redis]# make
src/redis-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
客戶端鏈接測試
[root@oldboyedu-s6 redis]# src/redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379>
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379>
127.0.0.1:6379> shutdown
1
2
3
4
5
6
7
8
9
配置redis
daemonize yes
port 1111
logfile /var/log/redis.log
dbfilename dump.rdb
dir /data/redis
appendonly yes
bind 10.0.0.200 127.0.0.1
requirepass 123
1
2
3
4
5
6
7
8
獲取當前配置
CONFIG GET *
1
經過配置啓動redis
添加完成後重啓redis
[root@oldboyedu-s6 ~]# /application/redis/src/redis-cli -p 1111
127.0.0.1:1111> shutdown
/application/redis/src/redis-server /etc/redis.conf
登陸測試:
[root@safly /]# /application/redis/src/redis-cli -h 10.0.0.128 -p 1111
10.0.0.128:1111>
1
2
3
4
5
6
7
8
密碼認證登錄
/application/redis/src/redis-cli -h 10.0.0.200 -a 123 -p 1111
或者:
/application/redis/src/redis-cli -h 10.0.0.200 -p 1111 auth 123
1
2
3
或者
(error) NOAUTH Authentication required.
127.0.0.1:1111> auth 123
OK
1
2
3
4
持久化
在 /etc/redis.conf中添加如下內容
dbfilename dump.rdb
dir /data/redis
save 900 1
save 300 10
save 60 10000
1
2
3
4
5
6
redis事物
DISCARD
取消事務,放棄執行事務塊內的全部命令。
EXEC
執行全部事務塊內的命令。
MULTI
標記一個事務塊的開始。
UNWATCH
取消 WATCH 命令對全部 key 的監視。
WATCH key [key …]
監視一個(或多個) key ,若是在事務執行以前這個(或這些) key 被其餘命令所改動,那麼事務將被打斷。
multi
[root@safly /]# /application/redis/src/redis-cli -p 1111 -a 123
127.0.0.1:1111> multi
OK
127.0.0.1:1111> discard
OK
127.0.0.1:1111> multi
OK
127.0.0.1:1111> set foo bar
QUEUED
127.0.0.1:1111> exec
1) OK
1
2
3
4
5
6
7
8
9
10
11
12
watch
127.0.0.1:1111> get ticket
(nil)
127.0.0.1:1111> set ticket 1
OK
127.0.0.1:1111> get ticket
"1"
127.0.0.1:1111> watch ticket
OK
127.0.0.1:1111> multi
OK
127.0.0.1:1111> decr ticket
QUEUED
127.0.0.1:1111> exec
1) (integer) 0
127.0.0.1:1111>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
另開一個客戶端
127.0.0.1:1111> clear
127.0.0.1:1111> multi
OK
127.0.0.1:1111> decr ticket
QUEUED
127.0.0.1:1111> exec
1) (integer) -1
127.0.0.1:1111> get ticket
"-1"
127.0.0.1:1111>
1
2
3
4
5
6
7
8
9
10
11
服務器管理命令
Info
Clinet list
Client kill ip:port
config get *
CONFIG RESETSTAT 重置統計
CONFIG GET/SET 動態修改
Dbsize
FLUSHALL 清空全部數據
select 1
FLUSHDB 清空當前庫
MONITOR 監控實時指令
SHUTDOWN 關閉服務器
save將當前數據保存
SLAVEOF host port 主從配置
SLAVEOF NO ONE
SYNC 主從同步
ROLE返回主從角色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
慢查詢日誌
Slow log 是 Redis 用來記錄查詢執行時間的日誌系統。
slow log 保存在內存裏面,讀寫速度很是快
能夠經過改寫 redis.conf 文件或者用 CONFIG GET 和 CONFIG SET 命令對它們動態地進行修改
slowlog-log-slower-than 10000 超過多少微秒
CONFIG SET slowlog-log-slower-than 100
CONFIG SET slowlog-max-len 1000 保存多少條慢日誌
CONFIG GET slow*
SLOWLOG GET
SLOWLOG RESET
1
2
3
4
5
6
7
8
9
主從複製
使用異步複製。
一個主服務器能夠有多個從服務器。
從服務器也能夠有本身的從服務器。
複製功能不會阻塞主服務器。
能夠經過複製功能來讓主服務器免於執行持久化操做,由從服務器去執行持久化操做便可。
配置主從複製
準備兩個或兩個以上redis實例
mkdir -p /data/6380/
mkdir -p /data/6381/
mkdir -p /data/6382/
1
2
3
三套配置文件示例:
vim /data/6380/redis.conf
----------------
bind 127.0.0.1 10.0.0.128
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dbfilename dump.rdb
dir /data/6380
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
vim /data/6381/redis.conf
----------------
bind 127.0.0.1 10.0.0.128
port 6381
daemonize yes
pidfile /data/6381/redis.pid
loglevel notice
logfile "/data/6381/redis.log"
dbfilename dump.rdb
dir /data/6381
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
vim /data/6382/redis.conf
----------------
bind 127.0.0.1 10.0.0.128
port 6382
daemonize yes
pidfile /data/6382/redis.pid
loglevel notice
logfile "/data/6382/redis.log"
dbfilename dump.rdb
dir /data/6382
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
啓動:
/application/redis/src/redis-server /data/6380/redis.conf
/application/redis/src/redis-server /data/6381/redis.conf
/application/redis/src/redis-server /data/6382/redis.conf
查看啓動狀態
netstat -lnp|grep 638
ps -ef |grep redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
關聯主從
[root@safly ~]# /application/redis/src/redis-cli -p 6381
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
127.0.0.1:6381> exit
[root@safly ~]# /application/redis/src/redis-cli -p 6382
127.0.0.1:6382> SLAVEOF 127.0.0.1 6380
OK
127.0.0.1:6382> exit
[root@safly ~]#
1
2
3
4
5
6
7
8
9
10
主從庫數據複製
主庫
[root@safly ~]# /application/redis/src/redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> set foo str
OK
1
2
3
4
5
6
從庫
[root@safly ~]# /application/redis/src/redis-cli -p 6381
127.0.0.1:6381> keys *
(empty list or set)
127.0.0.1:6381> get foo
"str"
127.0.0.1:6381>
1
2
3
4
5
6
7
Redis-Sentinel
Redis-Sentinel是Redis官方推薦的高可用性(HA)解決方案,當用Redis作Master-slave的高可用方案時,假如master宕機了,Redis自己(包括它的不少客戶端)都沒有實現自動進行主備切換,而Redis-sentinel自己也是一個獨立運行的進程,它能監控多個master-slave集羣,發現master宕機後能進行自動切換
功能:
監控(Monitoring):
Sentinel 會不斷地檢查你的主服務器和從服務器是否運做正常。
提醒(Notification):
當被監控的某個 Redis 服務器出現問題時, Sentinel 能夠經過 API 向管理員或者其餘應用程序發送通知。
自動故障遷移(Automatic failover):
當一個主服務器不能正常工做時, Sentinel 會開始一次自動故障遷移操做, 它會將失效主服務器的其中一個從服務器升級爲新的主服務器, 並讓失效主服務器的其餘從服務器改成複製新的主服務器; 當客戶端試圖鏈接失效的主服務器時, 集羣也會向客戶端返回新主服務器的地址, 使得集羣可使用新主服務器代替失效服務器。
sentinel配置集羣
mkdir /data/26380
cp /application/redis/src/redis-sentinel /data/26380
cd /data/26380
vim sentinel.conf
port 26380
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6380 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 0
啓動
./redis-sentinel ./sentinel.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
python3鏈接redis sentinel集羣
cluser分片集羣
安裝
EPEL源安裝ruby支持
yum install ruby rubygems -y
使用國內源
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources --remove http://rubygems.org/
gem sources -l
gem install redis -v 3.3.3
1
2
3
4
5
6
7
8
若是沒法使用,可使用aliyun
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources –remove http://rubygems.org/
配置
建立節點目錄:
mkdir -p /data/7000
mkdir -p /data/7001
mkdir -p /data/7002
mkdir -p /data/7003
mkdir -p /data/7004
mkdir -p /data/7005
1
2
3
4
5
6
7
8
配置文件添加:
-------------------------
vim /data/7000/redis.conf
port 7000
daemonize yes
pidfile /data/7000/redis.pid
logfile "/var/log/redis7000.log"
dbfilename dump.rdb
dir /data/7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /data/7001/redis.conf
port 7001
daemonize yes
pidfile /data/7001/redis.pid
logfile "/var/log/redis7001.log"
dbfilename dump.rdb
dir /data/7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /data/7002/redis.conf
port 7002
daemonize yes
pidfile /data/7002/redis.pid
logfile "/var/log/redis7002.log"
dbfilename dump.rdb
dir /data/7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /data/7003/redis.conf
port 7003
daemonize yes
pidfile /data/7003/redis.pid
logfile "/var/log/redis7003.log"
dbfilename dump.rdb
dir /data/7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /data/7004/redis.conf
port 7004
daemonize yes
pidfile /data/7004/redis.pid
logfile "/var/log/redis7004.log"
dbfilename dump.rdb
dir /data/7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /data/7005/redis.conf
port 7005
daemonize yes
pidfile /data/7005/redis.pid
logfile "/var/log/redis7005.log"
dbfilename dump.rdb
dir /data/7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
啓動
啓動全部節點
/application/redis/src/redis-server /data/7000/redis.conf
/application/redis/src/redis-server /data/7001/redis.conf
/application/redis/src/redis-server /data/7002/redis.conf
/application/redis/src/redis-server /data/7003/redis.conf
/application/redis/src/redis-server /data/7004/redis.conf
/application/redis/src/redis-server /data/7005/redis.conf
1
2
3
4
5
6
7
8
集羣建立
[root@safly ~]# ps -ef |grep 700
root 21937 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7000 [cluster]
root 21939 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7001 [cluster]
root 21941 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7002 [cluster]
root 21947 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7003 [cluster]
root 21949 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7004 [cluster]
root 21953 1 0 17:52 ? 00:00:00 /application/redis/src/redis-server *:7005 [cluster]
root 21961 21754 0 17:53 pts/2 00:00:00 grep --color=auto 700
[root@safly ~]# /application/redis/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
> 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
集羣狀態查看
/application/redis/src/redis-cli -p 7000 cluster nodes | grep master
/application/redis/src/redis-cli -p 7000 cluster nodes | grep slave
[root@safly ~]# /application/redis/src/redis-cli -p 7000 cluster nodes | grep master
e418dd7596daebc673bf9359b83569acdf2c917f 127.0.0.1:7002 master - 0 1525514094313 3 connected 10923-16383
914a0969c8bf91116b16fa9ce4da871ac5ed7bee 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
7881306744f38b26b2b86eea2e0343db99cf3fdd 127.0.0.1:7001 master - 0 1525514095337 2 connected 5461-10922
[root@safly ~]# /application/redis/src/redis-cli -p 7000 cluster nodes | grep slave
916a52d1e539f5a538b0e5edfa23c464ff5938d9 127.0.0.1:7004 slave 7881306744f38b26b2b86eea2e0343db99cf3fdd 0 1525514093775 5 connected
932f8956ed1436af96a46287e5babbfc667c9d88 127.0.0.1:7005 slave e418dd7596daebc673bf9359b83569acdf2c917f 0 1525514094828 6 connected
9eaa8ad6447ca92b6215de84af02a3cb5037f3f2 127.0.0.1:7003 slave 914a0969c8bf91116b16fa9ce4da871ac5ed7bee 0 1525514095851 4 connected
1
2
3
4
5
6
7
8
9
Python 鏈接 redis cluster
(1) redis-py並無提供redis-cluster的支持,去github找了一下,有個叫redis-py-cluster的源碼,
可是和redis-py不是一個做者,地址爲:https://github.com/Grokzen/redis-py-cluster
watch,star,fork還算能夠。
(2) 安裝
unzip redis-py-cluster-unstable.zip
cd redis-py-cluster-unstable
python3 setup.py install
1
2
3
4
5
6
7
8
(3)使用
[root@safly 26380]# python3
Python 3.5.2 (default, May 5 2018, 16:56:46)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
>>> rc.set("foo", "bar")
True
>>> print(rc.get("foo"))
bar
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
安裝python
安裝python
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0a1.tar.xz
yum install lrzsz -y 才能拖拽
tar xf Python-3.5.2.tar.xz
cd Python-3.5.2
./configure
make && make install
1
2
3
4
安裝redis的python驅動
unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install
1
2
3
操做redis數據
數據類型:
Key:value(string\hash\list\set\Sset)
-----全局類操做
KEYS * 查看KEY支持通配符
DEL 刪除給定的一個或多個key
EXISTS 檢查是否存在
RENAME 變動KEY名
TYPE 返回鍵所存儲值的類型
EXPIRE\ PEXPIRE 以秒\毫秒設定生存時間
TTL\ PTTL 以秒\毫秒爲單位返回生存時間
PERSIST 取消生存實現設置
--------------------string類型---------------------------
增
set mykey "test" 爲鍵設置新值,並覆蓋原有值
getset mycounter 0 設置值,取值同時進行
setex mykey 10 "hello" 設置指定 Key 的過時時間爲10秒,在存活時間能夠獲取value
setnx mykey "hello" 若該鍵不存在,則爲鍵設置新值
mset key3 "zyx" key4 "xyz" 批量設置鍵
刪
del mykey 刪除已有鍵
改
append mykey "hello" 若該鍵並不存在,返回當前 Value 的長度 該鍵已經存在,返回追加後 Value的長度
incr mykey 值增長1,若該key不存在,建立key,初始值設爲0,增長後結果爲1
decrby mykey 5 值減小5
setrange mykey 20 dd 把第21和22個字節,替換爲dd, 超過value長度,自動補0
查
exists mykey 判斷該鍵是否存在,存在返回 1,不然返回0
get mykey 獲取Key對應的value
strlen mykey 獲取指定 Key 的字符長度
ttl mykey 查看一下指定 Key 的剩餘存活時間(秒數)
getrange mykey 1 20 獲取第2到第20個字節,若20超過value長度,則截取第2個和後面全部的
mget key3 key4 批量獲取鍵
-----------------------------
應用場景
常規計數:微博數,粉絲數等
incr
incrby
decr
decrby
-----------------------HASH類型--------------------------
增
hset myhash field1 "s"
若字段field1不存在,建立該鍵及與其關聯的Hashes, Hashes中,key爲field1 ,並設value爲s ,若存在會覆蓋原value
hsetnx myhash field1 s
若字段field1不存在,建立該鍵及與其關聯的Hashes, Hashes中,key爲field1 ,並設value爲s, 若字段field1存在,則無效
hmset myhash field1 "hello" field2 "world 一次性設置多個字段
刪
hdel myhash field1 刪除 myhash 鍵中字段名爲 field1 的字段
del myhash 刪除鍵
改
hincrby myhash field 1 給field的值加1
查
hget myhash field1 獲取鍵值爲 myhash,字段爲 field1 的值
hlen myhash 獲取myhash鍵的字段數量
hexists myhash field1 判斷 myhash 鍵中是否存在字段名爲 field1 的字段
hmget myhash field1 field2 field3 一次性獲取多個字段
hgetall myhash 返回 myhash 鍵的全部字段及其值
hkeys myhash 獲取myhash 鍵中全部字段的名字
hvals myhash 獲取 myhash 鍵中全部字段的值
應用場景:
存儲部分變動的數據,如用戶信息等。須要將MySQL表數據進行緩存時,可使用此種數據類型。
------------------------------列表類型------------------------------
增
lpush mykey a b 若key不存在,建立該鍵及與其關聯的List,依次插入a ,b, 若List類型的key存在,則插入value中
lpushx mykey2 e 若key不存在,此命令無效, 若key存在,則插入value中
linsert mykey before a a1 在 a 的前面插入新元素 a1
linsert mykey after e e2 在e 的後面插入新元素 e2
rpush mykey a b 在鏈表尾部先插入b,在插入a
rpushx mykey e 若key存在,在尾部插入e, 若key不存在,則無效
rpoplpush mykey mykey2 將mykey的尾部元素彈出,再插入到mykey2 的頭部(原子性的操做)
刪
del mykey 刪除已有鍵
lrem mykey 2 a 從頭部開始找,按前後順序,值爲a的元素,刪除數量爲2個,若存在第3個,則不刪除
ltrim mykey 0 2 從頭開始,索引爲0,1,2的3個元素,其他所有刪除
改
lset mykey 1 e 從頭開始, 將索引爲1的元素值,設置爲新值 e,若索引越界,則返回錯誤信息
rpoplpush mykey mykey 將 mykey 中的尾部元素移到其頭部
查
lrange mykey 0 -1 取鏈表中的所有元素,其中0表示第一個元素,-1表示最後一個元素。
lrange mykey 0 2 從頭開始,取索引爲0,1,2的元素
lrange mykey 0 0 從頭開始,取第一個元素,從第0個開始,到第0個結束
lpop mykey 獲取頭部元素,而且彈出頭部元素,出棧
lindex mykey 6 從頭開始,獲取索引爲6的元素 若下標越界,則返回nil
應用場景
消息隊列系統
好比sina微博:
在Redis中咱們的最新微博ID使用了常駐緩存,這是一直更新的。
可是作了限制不能超過5000個ID,所以獲取ID的函數會一直詢問Redis。只有在start/count參數超出了這個範圍的時候,才須要去訪問數據庫。
系統不會像傳統方式那樣「刷新」緩存,Redis實例中的信息永遠是一致的。
SQL數據庫(或是硬盤上的其餘類型數據庫)只是在用戶須要獲取「很遠」的數據時纔會被觸發,而主頁或第一個評論頁是不會麻煩到硬盤上的數據庫了。
--------------------------- 集合類型-------------------------
集合
增
sadd myset a b c
若key不存在,建立該鍵及與其關聯的set,依次插入a ,b,若key存在,則插入value中,若a 在myset中已經存在,則插入了 d 和 e 兩個新成員。
刪
spop myset 尾部的b被移出,事實上b並非以前插入的第一個或最後一個成員
srem myset a d f 若f不存在, 移出 a、d ,並返回2
改
smove myset myset2 a 將a從 myset 移到 myset2,
查
sismember myset a 判斷 a 是否已經存在,返回值爲 1 表示存在。
smembers myset 查看set中的內容
scard myset 獲取Set 集合中元素的數量
srandmember myset 隨機的返回某一成員
sdiff myset1 myset2 myset3 1和2獲得一個結果,拿這個集合和3比較,得到每一個獨有的值
sdiffstore diffkey myset myset2 myset3 3個集和比較,獲取獨有的元素,並存入diffkey 關聯的Set中
sinter myset myset2 myset3 得到3個集合中都有的元素
sinterstore interkey myset myset2 myset3 把交集存入interkey 關聯的Set中
sunion myset myset2 myset3 獲取3個集合中的成員的並集
sunionstore unionkey myset myset2 myset3 把並集存入unionkey 關聯的Set中
應用場景:
案例:
在微博應用中,能夠將一個用戶全部的關注人存在一個集合中,將其全部粉絲存在一個集合。
Redis還爲集合提供了求交集、並集、差集等操做,能夠很是方便的實現如共同關注、共同喜愛、二度好友等功能,
對上面的全部集合操做,你還可使用不一樣的命令選擇將結果返回給客戶端仍是存集到一個新的集合中。
------------------------- 有序集合-----------------------------------------
有序集合
增
zadd myzset 2 "two" 3 "three" 添加兩個分數分別是 2 和 3 的兩個成員
刪
zrem myzset one two 刪除多個成員變量,返回刪除的數量
改
zincrby myzset 2 one 將成員 one 的分數增長 2,並返回該成員更新後的分數
查
zrange myzset 0 -1 WITHSCORES 返回全部成員和分數,不加WITHSCORES,只返回成員
zrank myzset one 獲取成員one在Sorted-Set中的位置索引值。0表示第一個位置
zcard myzset 獲取 myzset 鍵中成員的數量
zcount myzset 1 2 獲取分數知足表達式 1 <= score <= 2 的成員的數量
zscore myzset three 獲取成員 three 的分數
zrangebyscore myzset 1 2 獲取分數知足表達式 1 < score <= 2 的成員
#-inf 表示第一個成員,+inf最後一個成員
#limit限制關鍵字
#2 3 是索引號
zrangebyscore myzset -inf +inf limit 2 3 返回索引是2和3的成員
zremrangebyscore myzset 1 2 刪除分數 1<= score <= 2 的成員,並返回實際刪除的數量
zremrangebyrank myzset 0 1 刪除位置索引知足表達式 0 <= rank <= 1 的成員
zrevrange myzset 0 -1 WITHSCORES 按位置索引從高到低,獲取全部成員和分數
#原始成員:位置索引從小到大
one 0
two 1
#執行順序:把索引反轉
位置索引:從大到小
one 1
two 0
#輸出結果: two
one
zrevrange myzset 1 3 獲取位置索引,爲1,2,3的成員
#相反的順序:從高到低的順序
zrevrangebyscore myzset 3 0 獲取分數 3>=score>=0的成員並以相反的順序輸出
zrevrangebyscore myzset 4 0 limit 1 2 獲取索引是1和2的成員,並反轉位置索引
應用場景:
排行榜應用,取TOP N操做
這個需求與上面需求的不一樣之處在於,前面操做以時間爲權重,這個是以某個條件爲權重,
好比按頂的次數排序,這時候就須要咱們的sorted set出馬了,將你要排序的值設置成sorted set的score,將具體的數據設置成相應的value,每次只須要執行一條ZADD命令便可。
--------------------------------------
PUBLISH channel msg
將信息 message 發送到指定的頻道 channel
SUBSCRIBE channel [channel ...]
訂閱頻道,能夠同時訂閱多個頻道
PSUBSCRIBE pattern [pattern ...]
訂閱一個或多個符合給定模式的頻道,每一個模式以 * 做爲匹配符,好比 it* 匹配所 有以 it 開頭的頻道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配全部 以 news. 開頭的頻道( news.it 、 news.global.today 等等),諸如此類
PUNSUBSCRIBE [pattern [pattern ...]]
退訂指定的規則, 若是沒有
退訂指定的規則, 若是沒有參數則會退訂全部規則PUBSUB sub command [argument [argument ...]] 查看訂閱與發佈系統狀態注意:使用發佈訂閱模式實現的消息隊列,當有客戶端訂閱channel後只能收到後續發佈到該頻道的消息,以前發送的不會緩存,必須Provider和Consumer同時在線。