Linux安装Nginx、Redis、django

部署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.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中添加以下内容
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(stringhashlistsetSset)

-----全局类操作
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同时在线。

---------------------
作者:金丙坤
来源:CSDN
原文:https://blog.csdn.net/u013210620/article/details/80094173
版权声明:本文为博主原创文章,转载请附上博文链接!


---------------------
作者:金丙坤
来源:CSDN
原文:https://blog.csdn.net/u013210620/article/details/80094173
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/shaozhu520/p/10859119.html