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中文文档
Django使用Redis进行缓存详细最全流程的更多相关文章
- django使用redis做缓存
Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...
- Django使用redis实现缓存
实现缓存的方式,有多种:本地内存缓存,数据库缓存,文件系统缓存.这里介绍使用Redis数据库进行缓存. 配置 CACHES = { "default": { "BACKE ...
- Django分别使用Memcached和Redis作为缓存的配置(Linux环境)
1 使用memcached 1.1 安装memcached 安装(Linux) sudo apt install memcached 启动 #方式一: service memcached start ...
- django memcached/redis缓存 =====缓存session
全站使用 例如 博客等缓存,通过中间件实现全站缓存. 加缓存中间件,那么多中间件加在什么位置? 请求时:缓存加在中间件里的最后一个,比如一次经过1.2.3.4中间件,加在4 返回事:缓存加在中间件里的 ...
- Django(39)使用redis配置缓存
前言 动态网站的基本权衡是,它们是动态的.每次用户请求页面时,Web服务器都会进行各种计算 - 从数据库查询到模板呈现再到业务逻辑 - 以创建站点访问者看到的页面.从处理开销的角度来看,这比标准的 ...
- django+celery+redis环境搭建
初次尝试搭建django+celery+redis环境,记录下来,慢慢学习~ 1.安装apache 下载httpd-2.0.63.tar.gz,解压tar zxvf httpd-2.0.63.tar. ...
- 知乎技术分享:从单机到2000万QPS并发的Redis高性能缓存实践之路
本文来自知乎官方技术团队的“知乎技术专栏”,感谢原作者陈鹏的无私分享. 1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动 ...
- SpringAOP与Redis搭建缓存
近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做查询部分: 数据查询时每次都需要从数据库 ...
- Redis 作为缓存服务器的配置
随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...
随机推荐
- webconfig 配置 分离
https://blog.csdn.net/zhifeiya/article/details/38828711 如标题,如何把 asp.net webconfig的appSettings分离到单独文件 ...
- Mqtt paho 回调函数触发机制跟踪
Python Mqtt paho 回调函数触发机制跟踪,我使用的是 buildroot 里面的 mqtt paho , 代码在 ''' buildroot-2017.02.8/output/build ...
- 复制pycharm虚拟环境
我还是在内网进行开发,上篇讲了数据库驱动,方便链接数据库. 那么虚拟环境呢?那么多个包,离线下载………… 关键是我自己的笔记本也会进行交叉开发.一会儿在内网,一会儿在公网. 还是复制粘贴比较简单. 下 ...
- [LeetCode] 86. Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- [LeetCode] 502. IPO 上市
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...
- python:找出两个列表中相同和不同的元素(使用推导式)
#接口返回值 list1 = ['张三', '李四', '王五', '老二'] #数据库返回值 list2 = ['张三', '李四', '老二', '王七'] a = [x for x in lis ...
- 六、spring之通过FactoryBean为ioc容器中添加组件
前面我们已经介绍了几种为容器中添加组件的方法,今天一起学习通过FactoryBean添加组件的方法. 首先我们准备一个类,也就是我们需要注册进spring的ioc容器中的类 类Color: // 不必 ...
- QML 下拉列表框的使用
世界上一成不变的东西,只有"任何事物都是在不断变化的"这条真理. -- 斯里兰卡 ComboBox { id:combox x: structureTab_label2.x+str ...