Linux安装Nginx、Redis、django
部署Nginx
部署Redis
安裝Redis
redis事物
服務器管理命令
慢查詢?nèi)罩?br />主從復制
Redis-Sentinel
cluser分片集群
安裝python
操作redis數(shù)據(jù)
部署Django程序
部署Nginx
配置epel源
參看阿里網(wǎng)https://opsx.alibaba.com/mirror
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
##epel 配置方法
###1、備份(如有配置其他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
2、下載新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 repolist
[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
安裝nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
yum install pcre-devel openssl-devel -y
#編譯安裝三部曲 : ./configure make make install
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./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
重啟
[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中添加以下內(nèi)容
dbfilename dump.rdb
dir /data/redis
save 900 1
save 300 10
save 60 10000
1
2
3
4
5
6
redis事物
DISCARD
取消事務,放棄執(zhí)行事務塊內(nèi)的所有命令。
EXEC
執(zhí)行所有事務塊內(nèi)的命令。
MULTI
標記一個事務塊的開始。
UNWATCH
取消 WATCH 命令對所有 key 的監(jiān)視。
WATCH key [key …]
監(jiān)視一個(或多個) key ,如果在事務執(zhí)行之前這個(或這些) 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 重置統(tǒng)計
CONFIG GET/SET 動態(tài)修改
Dbsize
FLUSHALL 清空所有數(shù)據(jù)
select 1
FLUSHDB 清空當前庫
MONITOR 監(jiān)控實時指令
SHUTDOWN 關閉服務器
save將當前數(shù)據(jù)保存
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
慢查詢?nèi)罩?br />Slow log 是 Redis 用來記錄查詢執(zhí)行時間的日志系統(tǒng)。
slow log 保存在內(nèi)存里面,讀寫速度非常快
可以通過改寫 redis.conf 文件或者用 CONFIG GET 和 CONFIG SET 命令對它們動態(tài)地進行修改
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
主從復制
使用異步復制。
一個主服務器可以有多個從服務器。
從服務器也可以有自己的從服務器。
復制功能不會阻塞主服務器。
可以通過復制功能來讓主服務器免于執(zhí)行持久化操作,由從服務器去執(zhí)行持久化操作即可。
配置主從復制
準備兩個或兩個以上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
查看啟動狀態(tài)
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
關聯(lián)主從
[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
主從庫數(shù)據(jù)復制
主庫
[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本身(包括它的很多客戶端)都沒有實現(xiàn)自動進行主備切換,而Redis-sentinel本身也是一個獨立運行的進程,它能監(jiān)控多個master-slave集群,發(fā)現(xiàn)master宕機后能進行自動切換
功能:
監(jiān)控(Monitoring):
Sentinel 會不斷地檢查你的主服務器和從服務器是否運作正常。
提醒(Notification):
當被監(jiān)控的某個 Redis 服務器出現(xiàn)問題時, Sentinel 可以通過 API 向管理員或者其他應用程序發(fā)送通知。
自動故障遷移(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
使用國內(nèi)源
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/
配置
創(chuàng)建節(jié)點目錄:
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
啟動
啟動所有節(jié)點
/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
集群創(chuàng)建
[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
集群狀態(tài)查看
/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數(shù)據(jù)
數(shù)據(jù)類型:
Key:value(string\hash\list\set\Sset)
-----全局類操作
KEYS * 查看KEY支持通配符
DEL 刪除給定的一個或多個key
EXISTS 檢查是否存在
RENAME 變更KEY名
TYPE 返回鍵所存儲值的類型
EXPIRE\ PEXPIRE 以秒\毫秒設定生存時間
TTL\ PTTL 以秒\毫秒為單位返回生存時間
PERSIST 取消生存實現(xiàn)設置
--------------------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 的長度 該鍵已經(jīng)存在,返回追加后 Value的長度
incr mykey 值增加1,若該key不存在,創(chuàng)建key,初始值設為0,增加后結果為1
decrby mykey 5 值減少5
setrange mykey 20 dd 把第21和22個字節(jié),替換為dd, 超過value長度,自動補0
查
exists mykey 判斷該鍵是否存在,存在返回 1,否則返回0
get mykey 獲取Key對應的value
strlen mykey 獲取指定 Key 的字符長度
ttl mykey 查看一下指定 Key 的剩余存活時間(秒數(shù))
getrange mykey 1 20 獲取第2到第20個字節(jié),若20超過value長度,則截取第2個和后面所有的
mget key3 key4 批量獲取鍵
-----------------------------
應用場景
常規(guī)計數(shù):微博數(shù),粉絲數(shù)等
incr
incrby
decr
decrby
-----------------------HASH類型--------------------------
增
hset myhash field1 "s"
若字段field1不存在,創(chuàng)建該鍵及與其關聯(lián)的Hashes, Hashes中,key為field1 ,并設value為s ,若存在會覆蓋原value
hsetnx myhash field1 s
若字段field1不存在,創(chuàng)建該鍵及與其關聯(lián)的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鍵的字段數(shù)量
hexists myhash field1 判斷 myhash 鍵中是否存在字段名為 field1 的字段
hmget myhash field1 field2 field3 一次性獲取多個字段
hgetall myhash 返回 myhash 鍵的所有字段及其值
hkeys myhash 獲取myhash 鍵中所有字段的名字
hvals myhash 獲取 myhash 鍵中所有字段的值
應用場景:
存儲部分變更的數(shù)據(jù),如用戶信息等。需要將MySQL表數(shù)據(jù)進行緩存時,可以使用此種數(shù)據(jù)類型。
------------------------------列表類型------------------------------
增
lpush mykey a b 若key不存在,創(chuàng)建該鍵及與其關聯(lián)的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的元素,刪除數(shù)量為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
應用場景
消息隊列系統(tǒng)
比如sina微博:
在Redis中我們的最新微博ID使用了常駐緩存,這是一直更新的。
但是做了限制不能超過5000個ID,因此獲取ID的函數(shù)會一直詢問Redis。只有在start/count參數(shù)超出了這個范圍的時候,才需要去訪問數(shù)據(jù)庫。
系統(tǒng)不會像傳統(tǒng)方式那樣“刷新”緩存,Redis實例中的信息永遠是一致的。
SQL數(shù)據(jù)庫(或是硬盤上的其他類型數(shù)據(jù)庫)只是在用戶需要獲取“很遠”的數(shù)據(jù)時才會被觸發(fā),而主頁或第一個評論頁是不會麻煩到硬盤上的數(shù)據(jù)庫了。
--------------------------- 集合類型-------------------------
集合
增
sadd myset a b c
若key不存在,創(chuàng)建該鍵及與其關聯(lián)的set,依次插入a ,b,若key存在,則插入value中,若a 在myset中已經(jīng)存在,則插入了 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 是否已經(jīng)存在,返回值為 1 表示存在。
smembers myset 查看set中的內(nèi)容
scard myset 獲取Set 集合中元素的數(shù)量
srandmember myset 隨機的返回某一成員
sdiff myset1 myset2 myset3 1和2得到一個結果,拿這個集合和3比較,獲得每個獨有的值
sdiffstore diffkey myset myset2 myset3 3個集和比較,獲取獨有的元素,并存入diffkey 關聯(lián)的Set中
sinter myset myset2 myset3 獲得3個集合中都有的元素
sinterstore interkey myset myset2 myset3 把交集存入interkey 關聯(lián)的Set中
sunion myset myset2 myset3 獲取3個集合中的成員的并集
sunionstore unionkey myset myset2 myset3 把并集存入unionkey 關聯(lián)的Set中
?
應用場景:
案例:
在微博應用中,可以將一個用戶所有的關注人存在一個集合中,將其所有粉絲存在一個集合。
Redis還為集合提供了求交集、并集、差集等操作,可以非常方便的實現(xiàn)如共同關注、共同喜好、二度好友等功能,
對上面的所有集合操作,你還可以使用不同的命令選擇將結果返回給客戶端還是存集到一個新的集合中。
------------------------- 有序集合-----------------------------------------
有序集合
增
zadd myzset 2 "two" 3 "three" 添加兩個分數(shù)分別是 2 和 3 的兩個成員
刪
zrem myzset one two 刪除多個成員變量,返回刪除的數(shù)量
改
zincrby myzset 2 one 將成員 one 的分數(shù)增加 2,并返回該成員更新后的分數(shù)
查
zrange myzset 0 -1 WITHSCORES 返回所有成員和分數(shù),不加WITHSCORES,只返回成員
zrank myzset one 獲取成員one在Sorted-Set中的位置索引值。0表示第一個位置
zcard myzset 獲取 myzset 鍵中成員的數(shù)量
zcount myzset 1 2 獲取分數(shù)滿足表達式 1 <= score <= 2 的成員的數(shù)量
zscore myzset three 獲取成員 three 的分數(shù)
zrangebyscore myzset 1 2 獲取分數(shù)滿足表達式 1 < score <= 2 的成員
#-inf 表示第一個成員,+inf最后一個成員
#limit限制關鍵字
#2 3 是索引號
zrangebyscore myzset -inf +inf limit 2 3 返回索引是2和3的成員
zremrangebyscore myzset 1 2 刪除分數(shù) 1<= score <= 2 的成員,并返回實際刪除的數(shù)量
zremrangebyrank myzset 0 1 刪除位置索引滿足表達式 0 <= rank <= 1 的成員
zrevrange myzset 0 -1 WITHSCORES 按位置索引從高到低,獲取所有成員和分數(shù)
#原始成員:位置索引從小到大
one 0
two 1
#執(zhí)行順序:把索引反轉
位置索引:從大到小
one 1
two 0
#輸出結果: two
one
zrevrange myzset 1 3 獲取位置索引,為1,2,3的成員
#相反的順序:從高到低的順序
zrevrangebyscore myzset 3 0 獲取分數(shù) 3>=score>=0的成員并以相反的順序輸出
zrevrangebyscore myzset 4 0 limit 1 2 獲取索引是1和2的成員,并反轉位置索引
?
應用場景:
排行榜應用,取TOP N操作
這個需求與上面需求的不同之處在于,前面操作以時間為權重,這個是以某個條件為權重,
比如按頂?shù)拇螖?shù)排序,這時候就需要我們的sorted set出馬了,將你要排序的值設置成sorted set的score,將具體的數(shù)據(jù)設置成相應的value,每次只需要執(zhí)行一條ZADD命令即可。
--------------------------------------
PUBLISH channel msg
將信息 message 發(fā)送到指定的頻道 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 ...]]
退訂指定的規(guī)則, 如果沒有
?
退訂指定的規(guī)則, 如果沒有參數(shù)則會退訂所有規(guī)則
PUBSUB sub command [argument [argument ...]]
查看訂閱與發(fā)布系統(tǒng)狀態(tài)
注意:使用發(fā)布訂閱模式實現(xiàn)的消息隊列,當有客戶端訂閱channel后只能收到后續(xù)發(fā)布到該頻道的消息,之前發(fā)送的不會緩存,必須Provider和Consumer同時在線。
---------------------
作者:金丙坤
來源:CSDN
原文:https://blog.csdn.net/u013210620/article/details/80094173
版權聲明:本文為博主原創(chuàng)文章,轉載請附上博文鏈接!
---------------------
作者:金丙坤
來源:CSDN
原文:https://blog.csdn.net/u013210620/article/details/80094173
版權聲明:本文為博主原創(chuàng)文章,轉載請附上博文鏈接!
轉載于:https://www.cnblogs.com/shaozhu520/p/10859119.html
總結
以上是生活随笔為你收集整理的Linux安装Nginx、Redis、django的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手卫生包括什么内容?
- 下一篇: 求一个独特好听的男古风名字!