django缓存优化(一)
在配置之前,先介绍一个实用的工具:
当我们进入虚拟环境,在shell中进行操作的时候,往往要导入django的各种配置文件:
from django.x import xxxx
这时我们可以借助django_extensions工具
1、安装
(newblog-ES3JapFS) E:\PycharmProjects\mywebsite>pip install django_extensions
2、在settings中配置'django_extensions'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', 'django_extensions', ]
3、在shell中使用:python manage.py shell_plus
结果如下:
(newblog-ES3JapFS) E:\PycharmProjects\mywebsite>python manage.py shell_plus
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from myweb.models import Ad, Article, Category, Comment, Links, Tag, User
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery
from django.utils import timezone
from django.urls import reverse
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
Bingo!成功地导入!
现在我们来操作吧:
>>> from django.core.cache import caches
>>> cache['default']
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'DefaultCacheProxy' object is not subscriptable
>>> caches['default']
<django.core.cache.backends.locmem.LocMemCache object at 0x00000186B3102B00>
>>> from django.core.cache import cache
>>> cache.get('test')
>>> cache.set('mykey','hello',30)
>>> cache.get('mykey')
'hello'
>>> cache.get('mykey')
'hello'
>>> cache.get('mykey')
'hello'
# 30s之后缓存结束了,已经得不到返回值。
>>> cache.get('mykey')
>>> cache.get('mykey')
django缓存优化(一)的更多相关文章
- django缓存优化中caches参数如何配置?
在python开发中,如果运营django进行编写,为了提升效率,常常需要优化缓存,缓存优化中必须掌握的caches参数相关知识: CACHES 配置参数概述 - 格式 CACHES 字典配置格式如下 ...
- Django缓存优化之redis
Redis 概述 Redis 是一个开源的Inmemory key-value 存储系统,性能高,很大程度上补偿了 memcached 的不足.支持多种存储类型,包括 string, list, se ...
- django缓存优化(二)
一.缓存目的: 1.减小过载 2.避免重复计算 3.提高系统性能 二.如何进行缓存 三.缓存类型 四.缓存粒度分类 五.缓存的设置与使用 示例一: CACHES = { 'default': { 'B ...
- django缓存优化(三)
This should give you a feel for how this module operates:: import memcache mc = memcache.Client(['12 ...
- django性能优化
1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...
- Django—— 缓存框架
译者注:1.无用的,吹嘘的说辞不翻译:2.意译,很多地方不准确. 动态网站最为重要的一点就是好,网页是动态的.每一次用户请求页面,网站就要进行各种计算——从数据库查询,到render模板,到各种逻辑运 ...
- NSCache和NSURLCache、网络缓存优化
本文目录 一种缓存优化方案 响应头'Last-Modified'和请求头'If-Modified-Since' 'Keep-Alive'响应头和不离线的URLSession 'Expires'响应头 ...
- PHP服务缓存优化之ZendOpcache、xcache、eAccelerator
PHP服务缓存优化原理 Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程 缓存操作码(opcode) Opcode,PHP编译后的中间文件,缓存给 ...
- Django缓存使用方法
Django缓存分为Session和Cookie:Session为放在服务器端的缓存:Cookie为放在客户端(浏览器)的缓存. Session一般用来保存登录会话:Cookie一般用来保存一些个性化 ...
随机推荐
- <每日一题> Day2:CodeForces-1141C.PolycarpRestoresPermutation(思维题)
原题链接 参考代码: #include <iostream> #include <cstring> using namespace std; + , INF = 0x3f3f3 ...
- Java学习day10-面向对象特征之一:封装和隐藏
一.包package和引用import 1.关键字package package语句作为Java源文件的第一条语句,指明该文件中定义的类所在的包(若缺省该语句,则指定为无名包).包的存在是为了区别同名 ...
- P1522 牛的旅行 (Floyd)
[题目描述] 请编程找出一条连接两个不同牧场的路径,使得连上这条路径后,这个更大的新牧场有最小的直径.输出在所有牧场中最小的可能的直径. [题目链接] https://www.luogu.org/pr ...
- 音频视频的播放的进度调整(以.net为例)
Background:对于音视频在线播放,一些小应用是靠nginx处理访问视频.音频文件的请求,对外应用的一般会托管至各种云上使用相关的服务.前者存在巨大的安全隐患,后者会有一定的成本.有的时候还是需 ...
- Linux下创建虚VIP的方法及相互的区别:
#创建虚VIPifconfig eth1:1 192.168.202.200 broadcast 192.168.202.255 netmask 255.255.255.0 up ip addr ad ...
- css炫酷动画收藏
1.按钮.hover.input动画(cssfx) https://cssfx.dev/ 2.svg 矢量定制icon(ikonate) https://www.ikonate.com/#conten ...
- HBase的访问方式
这里只介绍三种最常用的方式 1.HBase shell HBase的命令行工具是最简单的接口,主要用于HBase管理 首先启动HBase 帮助 hbase(main):001:0> help 查 ...
- linux NFS 服务器的安装
1. 安装 nfs 服务 [root@allentuns ~]# yum -y install nfs-utils rpcbind 2. 启动 nfs 服务 [root@allentuns ~]# ...
- 洛谷 P2783 有机化学之神偶尔会做作弊(Tarjan,LCA)
题目背景 LS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. “第1354题怎么做”<--手语 他问道 ...
- 补比赛——牛客OI周赛9-普及组
比赛地址 A 小Q想撸串 题目分析 普及T1水题惯例.字符串中找子串. Code #include<algorithm> #include<iostream> #include ...