在配置之前,先介绍一个实用的工具:

当我们进入虚拟环境,在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缓存优化(一)的更多相关文章

  1. django缓存优化中caches参数如何配置?

    在python开发中,如果运营django进行编写,为了提升效率,常常需要优化缓存,缓存优化中必须掌握的caches参数相关知识: CACHES 配置参数概述 - 格式 CACHES 字典配置格式如下 ...

  2. Django缓存优化之redis

    Redis 概述 Redis 是一个开源的Inmemory key-value 存储系统,性能高,很大程度上补偿了 memcached 的不足.支持多种存储类型,包括 string, list, se ...

  3. django缓存优化(二)

    一.缓存目的: 1.减小过载 2.避免重复计算 3.提高系统性能 二.如何进行缓存 三.缓存类型 四.缓存粒度分类 五.缓存的设置与使用 示例一: CACHES = { 'default': { 'B ...

  4. django缓存优化(三)

    This should give you a feel for how this module operates:: import memcache mc = memcache.Client(['12 ...

  5. django性能优化

    1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...

  6. Django—— 缓存框架

    译者注:1.无用的,吹嘘的说辞不翻译:2.意译,很多地方不准确. 动态网站最为重要的一点就是好,网页是动态的.每一次用户请求页面,网站就要进行各种计算——从数据库查询,到render模板,到各种逻辑运 ...

  7. NSCache和NSURLCache、网络缓存优化

    本文目录 一种缓存优化方案 响应头'Last-Modified'和请求头'If-Modified-Since' 'Keep-Alive'响应头和不离线的URLSession 'Expires'响应头 ...

  8. PHP服务缓存优化之ZendOpcache、xcache、eAccelerator

    PHP服务缓存优化原理 Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程 缓存操作码(opcode) Opcode,PHP编译后的中间文件,缓存给 ...

  9. Django缓存使用方法

    Django缓存分为Session和Cookie:Session为放在服务器端的缓存:Cookie为放在客户端(浏览器)的缓存. Session一般用来保存登录会话:Cookie一般用来保存一些个性化 ...

随机推荐

  1. struts2 基础4 验证器、 国际化

    验证器: 验证器:用户输入验证 1.手动编程方式 )对于动作类中所有方法进行验证 a.动作类继承ActionSuport b.覆盖调用public void validate(){} 方法 c.在va ...

  2. 使用fiddler,提示系统找不到相应的文件FSE2.exe文件

    使用fiddler时候遇到了如下问题: Rules中customize rules 时,提示系统找不到相应的文件FSE2.exe文件. 这个文件的位置可以在Tools->opinions-> ...

  3. 如何通过脚本实现显示版本号、CPU、硬盘和内存条大小

    COLOR="\033[1;$[RANDOM%7+31]m"     COLOR1="\033[1;$[RANDOM%7+31]m"COLOR2="\ ...

  4. ubuntu 虚拟机配置 IP、子网掩码、网关、DNS

    ubuntu 虚拟机配置 IP.子网掩码.网关.DNS 执行 sudo vim /etc/network/interfaces 添加如下配置: auto eth0 iface eth0 inet st ...

  5. 1.go语言目录结构

    [root@localhost ~]# ll /go/ total drwxr-xr-x. root root May : api -rw-r--r--. root root May : AUTHOR ...

  6. <input>/<textarea>输入框设置默认提示文字(隐藏式)

    html代码如下: <tr>    <td>签   名:</td>    <td><input type="text" nam ...

  7. struts2导入多个xml引入报错<include>

    struts.xml <?xml version="1.0" encoding="UTF-8"?> <!-- 指定Struts 2配置文件的D ...

  8. MTCNN 人脸检测

    demo.py import cv2 from detection.mtcnn import MTCNN # 检测图片中的人脸 def test_image(imgpath): mtcnn = MTC ...

  9. pg_controldata - 显示一个 PostgreSQL 集群的控制信息

    SYNOPSIS pg_controldata [ datadir] DESCRIPTION 描述 pg_controldata 打印那些在 initdb 过程中初始化的信息,比如表版本和服务器的区域 ...

  10. Jupyter Notebook 安装与使用

    Ref: https://jupyter.org/install Installing Jupyter Notebook with pip python -m pip install --upgrad ...