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

当我们进入虚拟环境,在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. Java ——正则表达式

    本节重点思维导图 详细见 ————>  正则表达式 [各种语法和方法]

  2. 【opencv】opencv图像识别的一些基础的基础函数的使用方法

    import cv2 import numpy as np from matplotlib import pyplot as plt pic_path = "deal_with.png&qu ...

  3. Java数组的使用

    一.数组的动态初始化 1.声明数据类型[] 数组名;或数据类型 数组名[];2.开辟空间数组名 = new 数据类型[长度];//长度必不可少3.手动赋值数组名[下标] = 值;4.使用(打印.运算. ...

  4. HDUSTOJ-1558 Flooring Tiles(反素数)

    1558: Flooring Tiles 时间限制: 3 Sec  内存限制: 128 MB提交: 59  解决: 36[提交][状态][讨论版] 题目描述 You want to decorate ...

  5. #10017 传送带(SCOI 2010)(三分套三分)

    [题目描述] 在一个 2 维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段 AB 和线段 CD.lxhgww 在 AB上的移动速度为 P ,在 CD 上的移动速度为 Q,在平 ...

  6. wxpython中单选框的两种创建方式源码展示

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  7. C#for(;;)是什么意思?

    一,正常for循环我们都接触过很多,如下,我们都理解 ,,,,, }; ; i < ; i++) { Console.WriteLine(tt[i]); } 二,但是for(;;)实际上它的含义 ...

  8. 深入理解js闭包【写的通俗易懂,很值的阅读】

    详细内容在下面这个链接里面: https://www.cnblogs.com/uedt/archive/2010/10/28/1863389.html 能写出这样的文章,定是大佬!

  9. Nodejs的模块化

    Node.js中的模块化 好处: 复用性高,一次定义,多次使用 前端模块化 AMD AMD的实现需要使用 require.js CMD CMD的实现需要使用 sea.js [ 不更新 ] Common ...

  10. 使用myBase Desktop来管理电脑上的资料

    下载链接:下载链接:http://www.wjjsoft.com/download.html 选择自己的操作系统下的myBase Desktop 这里是下载的是安装包,有解压的版本的. 这里就简单介绍 ...