zoukankan      html  css  js  c++  java
  • django-缓存

    缓存

    缓存的定义

    缓存案例

    查询结束返回给网页消耗时间

    缓存优化的思想

    缓存场景

    配置缓存

    数据库缓存

    image

    settings配置

    image

    本地内存缓存(测试)

    雪花算法
    image

    文件系统缓存

    image

    数据库缓存

    settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': "cache_use",
            'USER':"root",
            "PASSWORD":'password',
            "HOST":"localhost",
            "PORT":'3306'
        }
    }
    
    #数据库缓存配置,需手动执行创建命令 'my_cache_table'
    CACHES={
    "default":{
        "BACKEND":'django.core.cache.backends.db.DatabaseCache',
        "LOCATION":'my_cache_table',
        "TIMEOUT":300,
        "OPTIONS":{
            "MAX_ENTRIES":300,
            "CULL_FREQUENCY":2,
          }
       }
    }
    

    执行命令 createcachetable

    E:django_video_studycache_usecache_use>python manage.py createcachetable
    
    E:django_video_studycache_usecache_use>python manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying admin.0003_logentry_add_action_flag_choices... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying auth.0010_alter_group_name_max_length... OK
      Applying auth.0011_update_proxy_permissions... OK
      Applying sessions.0001_initial... OK
    

    数据库中

    mysql> show tables;
    +----------------------------+
    | Tables_in_cache_use        |
    +----------------------------+
    | auth_group                 |
    | auth_group_permissions     |
    | auth_permission            |
    | auth_user                  |
    | auth_user_groups           |
    | auth_user_user_permissions |
    | django_admin_log           |
    | django_content_type        |
    | django_migrations          |
    | django_session             |
    | my_cache_table             |
    +----------------------------+
    11 rows in set (0.00 sec)
    
    mysql> desc my_cache_table;
    +-----------+--------------+------+-----+---------+-------+
    | Field     | Type         | Null | Key | Default | Extra |
    +-----------+--------------+------+-----+---------+-------+
    | cache_key | varchar(255) | NO   | PRI | NULL    |       |
    | value     | longtext     | NO   |     | NULL    |       |
    | expires   | datetime(6)  | NO   | MUL | NULL    |       |
    +-----------+--------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    

    使用缓存---视图函数中

    传入的参数为当前视图的时间
    image

    使用缓存---路由中

    image

    views.py 中配置

    import time
    from django.shortcuts import HttpResponse
    from django.http import request
    from django.views.decorators.cache import cache_page
    @cache_page(10)
    def test_cache(request):
        t=time.time()
        print(t)
        return HttpResponse("当前时间为%d"%(t))
    

    局部缓存

    缓存api的使用

    image
    image
    image
    image
    image

    In [1]: from django.core.cache import cache
    In [2]: #获取default里面的配置项
    In [8]: cache.set("us","sjs",20)
    In [9]: cache.get("us")
    Out[9]: 'sjs'
    
    mysql> select * from my_cache_table G:;
    *************************** 1. row ***************************
    cache_key: :1:us
        value: gAWVBwAAAAAAAACMA3Nqc5Qu
      expires: 2021-08-31 01:32:32.000000
    

    浏览器缓存策略

    image

    分类:强缓存

    image
    image
    image

    分类:协商缓存

    image
    image
    image
    image

  • 相关阅读:
    K优先队列——对顶堆(两种优先队列的组合)
    斐波那契数列——UPC
    UPC-混合训练第十五场
    隐写术之steghide的使用
    常见的文件头文件尾
    隐写术
    1.6 创建新的虚拟机以及如何开启虚拟机
    1.5 下载和安装VMWare
    1.4 常见的网络协议
    1.3 Windows注册表
  • 原文地址:https://www.cnblogs.com/yescarf/p/15194930.html
Copyright © 2011-2022 走看看