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一般用来保存一些个性化 ...
随机推荐
- 【ABAP系列】SAP ABAP 生成随机数的函数
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 生成随机数的函数 ...
- 安全运维 - Windows系统攻击回溯
Windows应急事件 病毒.木马.蠕虫 Web服务器入侵事件或第三方服务入侵事件 系统入侵事件 网络攻击事件(DDOS.ARP.DNS劫持等) 通用排查思路 获知异常事件基本情况 发现主机异常现象的 ...
- CEPH安装(CentOS 7)
以包含四个节点的集群为例,其中包括一个 ceph-deploy 管理节点和一个三节点的Ceph存储集群. 下图中每个节点代表一台机器. 安装 CEPH 部署工具 执行如下命令: sudo yum in ...
- AngularJs——基础小知识(二)
AngularJs的过滤器 1.Currency :过滤器(金额货币格式化)
- git 中添加用户名和密码
git 中添加用户名和密码:https://blog.csdn.net/qq_28602957/article/details/52154384 在使用git时,如果用的是HTTPS的方式,则每次提交 ...
- C++对象构造时,构造函数运行时并不知道VT的存在
class A {public: A() { init(); } virtual void init() { printf("A::init\n"); }}; class B : ...
- Linux 修改hostname几种方式
1: hostname DB-Server --运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改 2: echo DB-Server > /proc/sys ...
- 2019-5-28-VisualStudio-扩展开发
title author date CreateTime categories VisualStudio 扩展开发 lindexi 2019-05-28 19:51:49 +0800 2018-2-1 ...
- Object中有哪些公用方法?
clone()方法 实现对象的浅复制,只有实现了Cloneable接口才能调用该方法. toString()方法 返回该对象的字符串表示. equals()方法: 在Object中与“==”的定义是一 ...
- MySQL concat函数里面单引号的使用
通过concat拼字符串的时候,如果语句里面需要使用单引号,可以使用两个单引号来代替一个引号