zoukankan      html  css  js  c++  java
  • django缓存优化(一)

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

    当我们进入虚拟环境,在shell中进行操作的时候,往往要导入django的各种配置文件:

    from django.x import xxxx

    这时我们可以借助django_extensions工具

    1、安装

    (newblog-ES3JapFS) E:PycharmProjectsmywebsite>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:PycharmProjectsmywebsite>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')
    
    明月装饰了你的窗子,你装饰了他的梦。
  • 相关阅读:
    I/O多路复用和Socket
    我读过的最好的epoll讲解--转自”知乎“
    gcc和g++的区别
    详解派生类构造函数与析构函数
    C++中构造函数,拷贝构造函数和赋值函数的区别和实现
    浅拷贝和深拷贝的区别?
    曲演杂坛--特殊字符/生僻字与varchar
    SQL SERVER--单回话下的死锁
    曲演杂坛--表变量的预估行数
    曲演杂坛--为什么SELECT语句会被其他SELECT阻塞?
  • 原文地址:https://www.cnblogs.com/zkkysqs/p/9539595.html
Copyright © 2011-2022 走看看