转载 django中使用redis

服务器数据非经常更新。若每次都从硬盘读取一次,浪费服务器资源、拖慢响应速度。而且数据更新频率较高,服务器负担比较大。若保存到数据库,还需要额外建立一张对应的表存储数据。在Django中建立表通常做法是建立一个模型。看似简单,问题调试麻烦、开发时长久。为了进行服务器的加速,使用Redis进行缓存。

配置步骤如下
1,服务器端安装redis
(1)在Windows安装redis,方便测试
Redis不支持Windows!在它官网写得很清楚。但是开发环境一般是Windows系统。为了方便开发和调试,需要在Windows中安装Redis。微软自己弄了Redis的Windows版本。打开https://github.com/MSOpenTech/redis/releases下载msi安装包。该版本是64位。安装msi过程中,有个选项是否加入系统环境变量,记得勾上。一路下一步,安装。完成之后打开cmd,输入redis-server命令查看是否可以使用。不可以则重启一下即可。直接输入redis-server命令使用的配置文件是安装目录下的redis.windows.conf文件。
若提示错误 “ConnectionError: Error 10061 connecting to None:6379”,可以如下操作,打开cmd输入如下命令:

redis-cli shutdown
1
再执行redis-server即可。
(2)在ubuntu下安装,针对部署

sudo apt-get install redis-server
1
安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

root@VM-60-191-ubuntu:~# ps -aux|grep redis
redis 30543 0.1 0.7 37228 6724 ? Ssl 14:03 0:00 /usr/bin/redis- server 127.0.0.1:6379
root 30660 0.0 0.1 11288 924 pts/1 S+ 14:04 0:00 grep --color=au to redis
1
2
3
查看运行状态

root@VM-60-191-ubuntu:~# sudo /etc/init.d/redis-server status
* redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-03-26 14:03:03 CST; 3min 1s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 30543 (redis-server)
CGroup: /system.slice/redis-server.service
`-30543 /usr/bin/redis-server 127.0.0.1:6379

Mar 26 14:03:03 VM-60-191-ubuntu systemd[1]: Starting Advanced key-value store...
Mar 26 14:03:03 VM-60-191-ubuntu run-parts[30533]: run-parts: executing /etc/redis/redis-server....le
Mar 26 14:03:03 VM-60-191-ubuntu run-parts[30544]: run-parts: executing /etc/redis/redis-server....le
Mar 26 14:03:03 VM-60-191-ubuntu systemd[1]: Started Advanced key-value store.
Hint: Some lines were ellipsized, use -l to show in full.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
通过命令行客户端访问


root@VM-60-191-ubuntu:~# redis-cli
#查看帮助
127.0.0.1:6379> help
redis-cli 3.0.6
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
#设置k-v记录
127.0.0.1:6379> set key1 "helloword"
OK
#根据键查找记录
127.0.0.1:6379> get key1
"helloword"
#展示所有的键
127.0.0.1:6379> keys *
1) "key1"
#删除键
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2,Redis访问控制
默认情况下,访问Redis服务器是不需要密码的,为了让其他服务器使用同时增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为yourpassword。
由于redis默认绑定本机的,所以第一步取消该设置:

sudo vim /etc/redis/redis.conf
1
用vim打开该配置文件,然后注释掉下面这行:

#bind 127.0.0.1
1
然后设置登密码,用vim打开配置文件,配置文件较长,命令模式下输入/requirepass foobared快速搜索该配置项:

#编辑配置文件
sudo vim /etc/redis/redis.conf
#找到下面这一行并去除注释(可以搜索requirepass)我的 是396行
#requirepass foobared 未修改之前
#修改之后
requirepass 123456789 #假设123456789是我的redis密码
1
2
3
4
5
6
7
修改后重启服务器使配置生效:

root@VM-60-191-ubuntu:~# sudo /etc/init.d/redis-server restart
[ ok ] Restarting redis-server (via systemctl): redis-server.service.
1
2
此时在登录redis,权限被控制


root@VM-60-191-ubuntu:~# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
1
2
3
4
用密码登录,具有权限

root@VM-60-191-ubuntu:~# redis-cli -a 941020
127.0.0.1:6379> keys *
1) "key2"
1
2
3
通过以下命令从另一台linux服务器访问redis,password替换为你的密码,host替换为要访问的服务器

redis-cli -a password -h hostip
1
数据库的数量是可以配置的,不知道数据库就是数据库0,默认情况下是16个。修改redis.conf下的databases指令:

databases 64
1
3,安装django-redis和settings配置
pip install django-redis
1
settings.py中加入以下内容,your_host_ip换成你的服务器地址,yoursecret换成你的服务器密码

CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://your_host_ip:6379',
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "yoursecret",
},
},
}


REDIS_TIMEOUT=7*24*60*60
CUBES_REDIS_TIMEOUT=60*60
NEVER_REDIS_TIMEOUT=365*24*60*60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cache中的是必须的,下面三条可有可无

4,测试缓存是否成功
本步骤非必须,只是为了测试看可否正常使用redis。
进入django的后台命令模式:

python manage.py shell
1
逐条输入如下命令测试:

from django.core.cache import cache #引入缓存模块
cache.set('v', '555', 60*60) #写入key为v,值为555的缓存,有效期30分钟
cache.has_key('v') #判断key为v是否存在
cache.get('v') #获取key为v的缓存
1
2
3
4
更多命令请查看网址:django_redis中文文档

get和set方法中已经封装了对数据类型的处理,使用中无需考虑redis中数据类型的问题,只需将数据对象set进去,用时get出来就行了

配置步骤如下
1,服务器端安装redis
(1)在Windows安装redis,方便测试
Redis不支持Windows!在它官网写得很清楚。但是开发环境一般是Windows系统。为了方便开发和调试,需要在Windows中安装Redis。微软自己弄了Redis的Windows版本。打开https://github.com/MSOpenTech/redis/releases下载msi安装包。该版本是64位。安装msi过程中,有个选项是否加入系统环境变量,记得勾上。一路下一步,安装。完成之后打开cmd,输入redis-server命令查看是否可以使用。不可以则重启一下即可。直接输入redis-server命令使用的配置文件是安装目录下的redis.windows.conf文件。
若提示错误 “ConnectionError: Error 10061 connecting to None:6379”,可以如下操作,打开cmd输入如下命令:

redis-cli shutdown
1
再执行redis-server即可。
(2)在ubuntu下安装,针对部署

sudo apt-get install redis-server
1
安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

root@VM-60-191-ubuntu:~# ps -aux|grep redis
redis 30543 0.1 0.7 37228 6724 ? Ssl 14:03 0:00 /usr/bin/redis- server 127.0.0.1:6379
root 30660 0.0 0.1 11288 924 pts/1 S+ 14:04 0:00 grep --color=au to redis
1
2
3
查看运行状态

root@VM-60-191-ubuntu:~# sudo /etc/init.d/redis-server status
* redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-03-26 14:03:03 CST; 3min 1s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 30543 (redis-server)
CGroup: /system.slice/redis-server.service
`-30543 /usr/bin/redis-server 127.0.0.1:6379

Mar 26 14:03:03 VM-60-191-ubuntu systemd[1]: Starting Advanced key-value store...
Mar 26 14:03:03 VM-60-191-ubuntu run-parts[30533]: run-parts: executing /etc/redis/redis-server....le
Mar 26 14:03:03 VM-60-191-ubuntu run-parts[30544]: run-parts: executing /etc/redis/redis-server....le
Mar 26 14:03:03 VM-60-191-ubuntu systemd[1]: Started Advanced key-value store.
Hint: Some lines were ellipsized, use -l to show in full.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
通过命令行客户端访问


root@VM-60-191-ubuntu:~# redis-cli
#查看帮助
127.0.0.1:6379> help
redis-cli 3.0.6
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
#设置k-v记录
127.0.0.1:6379> set key1 "helloword"
OK
#根据键查找记录
127.0.0.1:6379> get key1
"helloword"
#展示所有的键
127.0.0.1:6379> keys *
1) "key1"
#删除键
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2,Redis访问控制
默认情况下,访问Redis服务器是不需要密码的,为了让其他服务器使用同时增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为yourpassword。
由于redis默认绑定本机的,所以第一步取消该设置:

sudo vim /etc/redis/redis.conf
1
用vim打开该配置文件,然后注释掉下面这行:

#bind 127.0.0.1
1
然后设置登密码,用vim打开配置文件,配置文件较长,命令模式下输入/requirepass foobared快速搜索该配置项:

#编辑配置文件
sudo vim /etc/redis/redis.conf
#找到下面这一行并去除注释(可以搜索requirepass)我的 是396行
#requirepass foobared 未修改之前
#修改之后
requirepass 123456789 #假设123456789是我的redis密码
1
2
3
4
5
6
7
修改后重启服务器使配置生效:

root@VM-60-191-ubuntu:~# sudo /etc/init.d/redis-server restart
[ ok ] Restarting redis-server (via systemctl): redis-server.service.
1
2
此时在登录redis,权限被控制


root@VM-60-191-ubuntu:~# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
1
2
3
4
用密码登录,具有权限

root@VM-60-191-ubuntu:~# redis-cli -a 941020
127.0.0.1:6379> keys *
1) "key2"
1
2
3
通过以下命令从另一台linux服务器访问redis,password替换为你的密码,host替换为要访问的服务器

redis-cli -a password -h hostip
1
数据库的数量是可以配置的,不知道数据库就是数据库0,默认情况下是16个。修改redis.conf下的databases指令:

databases 64
1
3,安装django-redis和settings配置
pip install django-redis
1
settings.py中加入以下内容,your_host_ip换成你的服务器地址,yoursecret换成你的服务器密码

CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://your_host_ip:6379',
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "yoursecret",
},
},
}


REDIS_TIMEOUT=7*24*60*60
CUBES_REDIS_TIMEOUT=60*60
NEVER_REDIS_TIMEOUT=365*24*60*60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cache中的是必须的,下面三条可有可无

4,测试缓存是否成功
本步骤非必须,只是为了测试看可否正常使用redis。
进入django的后台命令模式:

python manage.py shell
1
逐条输入如下命令测试:

from django.core.cache import cache #引入缓存模块
cache.set('v', '555', 60*60) #写入key为v,值为555的缓存,有效期30分钟
cache.has_key('v') #判断key为v是否存在
cache.get('v') #获取key为v的缓存
1
2
3
4
更多命令请查看网址:django_redis中文文档

get和set方法中已经封装了对数据类型的处理,使用中无需考虑redis中数据类型的问题,只需将数据对象set进去,用时get出来就行了

猜你喜欢

转载自www.cnblogs.com/dayongge/p/13380777.html
今日推荐