背景和意义
服务器数据非经常更新。若每次都从硬盘读取一次,浪费服务器资源、拖慢响应速度。而且数据更新频率较高,服务器负担比较大。若保存到数据库,还需要额外建立一张对应的表存储数据。在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进行缓存详细最全流程的更多相关文章

  1. django使用redis做缓存

    Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...

  2. Django使用redis实现缓存

    实现缓存的方式,有多种:本地内存缓存,数据库缓存,文件系统缓存.这里介绍使用Redis数据库进行缓存. 配置 CACHES = { "default": { "BACKE ...

  3. Django分别使用Memcached和Redis作为缓存的配置(Linux环境)

    1 使用memcached 1.1 安装memcached 安装(Linux) sudo apt install memcached 启动 #方式一: service memcached start ...

  4. django memcached/redis缓存 =====缓存session

    全站使用 例如 博客等缓存,通过中间件实现全站缓存. 加缓存中间件,那么多中间件加在什么位置? 请求时:缓存加在中间件里的最后一个,比如一次经过1.2.3.4中间件,加在4 返回事:缓存加在中间件里的 ...

  5. Django(39)使用redis配置缓存

    前言   动态网站的基本权衡是,它们是动态的.每次用户请求页面时,Web服务器都会进行各种计算 - 从数据库查询到模板呈现再到业务逻辑 - 以创建站点访问者看到的页面.从处理开销的角度来看,这比标准的 ...

  6. django+celery+redis环境搭建

    初次尝试搭建django+celery+redis环境,记录下来,慢慢学习~ 1.安装apache 下载httpd-2.0.63.tar.gz,解压tar zxvf httpd-2.0.63.tar. ...

  7. 知乎技术分享:从单机到2000万QPS并发的Redis高性能缓存实践之路

    本文来自知乎官方技术团队的“知乎技术专栏”,感谢原作者陈鹏的无私分享. 1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动 ...

  8. SpringAOP与Redis搭建缓存

    近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做查询部分: 数据查询时每次都需要从数据库 ...

  9. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

随机推荐

  1. [LeetCode] 149. Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  2. centos php7.1 redis4.0.9 安装扩展phpredis

    1.扩展列表 https://github.com/phpredis/phpredis/releases [root@VM_27_0_centos server]# ls data lib.pl my ...

  3. VMware的包格式vmdk转换为virtualBox的ova

    使用winxp的vmdk作为案例 1 使用vmvare导入vmdk的winxp,点击文件---->导出为ovf 2 找到生成的ovf文件 3 打开virtualBox 管理---->导入虚 ...

  4. hihocoder 1566 皇室成员的名字

    #1566 : 皇室成员的名字 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho正在学习世界历史.他发现历史上很多西方国家的皇室成员的名字都是由英文名字加罗马数字组 ...

  5. Influx Sql系列教程一:database 数据库

    对于influxdb而言,database和我们更熟悉的mysql中的dababse没有什么特别的区别,可以将数据库简单理解为一堆表(measurement)的集合,接下来我们将看一下在influxd ...

  6. 查看Linux是CentOS还是Ubuntu

    lsb_release -a

  7. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

  8. libevent实现TCP 客户端

    ibevent实现Tcp Client基于bufferevent实现 #include <stdio.h> #include <unistd.h> #include <s ...

  9. python爬虫-《笔趣看》网小说《悟空看私聊》

    小编是个爱看小说的人,哈哈 # -*- coding:UTF-8 -*- ''' 类说明:下载<笔趣看>网小说<悟空看私聊> ''' from bs4 import Beaut ...

  10. web API .net - .net core 对比学习-使用Swagger

    根据前两篇的介绍,我们知道.net web api 和 .net core web api在配置方面的不同如下: 1. .net web api的配置是在 App_Stat文件夹里面添加对应的配置类, ...