Django缓存的介绍

配置(settings.py设置不同缓存介质)

应用(全局、视图函数、模板)

实测

Django缓存的介绍

除了Django这个web框架之外、其他框架都没有缓存。Django的配置一下就可以使用。

Django中提供了5种缓存方式:

    • 开发调试(缓存哪里都不放,只都配置好,测试用)
    • 内存
    • 文件
    • 数据库
    • Memcache缓存 
      • (使用 python-memcached模块 连接memcache)
      • (使用 pylibmc模块 连接memcache)

配置(settings.py设置不同缓存介质)

官网:https://docs.djangoproject.com/en/1.9/topics/cache/

http://docs.30c.org/djangobook2/chapter15/

开发调试

# 此为开始调试用,实际内部不做任何操作
    # 配置:
        CACHES = {
            'default': {
                'BACKEND': 'django.core.cache.backends.dummy.DummyCache',     # 引擎
                'TIMEOUT': 300,           # 缓存超时时间(默认300,None表示永不过期,0表示立即过期)
                'OPTIONS':{
                    'MAX_ENTRIES': 300,   # 最大缓存个数(默认300)
                    'CULL_FREQUENCY': 3,  # 缓存到达最大个数之后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认剔除1/3)
                },
                'KEY_PREFIX': '',         # 缓存key的前缀(默认空)
                'VERSION': 1,             # 缓存key的版本(默认1)
                'KEY_FUNCTION' 函数名     # 生成key的函数(默认函数会生成为:【前缀:版本:key】)
            }
        }

    # 自定义key 名
    def default_key_func(key, key_prefix, version):
        """
        Default function to generate keys.

        Constructs the key used by all other methods. By default it prepends
        the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
        function with custom key making behavior.
        """
        return '%s:%s:%s' % (key_prefix, version, key)

    def get_key_func(key_func):
        """
        Function to decide which key function to use.

        Defaults to ``default_key_func``.
        """
        if key_func is not None:
            if callable(key_func):
                return key_func
            else:
                return import_string(key_func)
        return default_key_func
开发调试

相关文章:

  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-01-06
  • 2022-01-16
  • 2021-08-27
猜你喜欢
  • 2021-07-30
  • 2022-01-18
  • 2022-01-24
  • 2021-10-30
  • 2021-07-17
  • 2021-07-22
  • 2021-10-10
相关资源
相似解决方案