[Django 0-1] Core.Cache模块

Caching 源码分析

Django 的 cache 缓存机制,包含了一些代理设计模式(代理了但没完全代理,多此一举)。

通过实现一个CacheHandler的manager类,来实现多缓存后端的统一管理和调用,避免到处实例使用。

缓存的目的

缓存的目的就是为了提高系统的性能.

  1. 存储一些变化少的热点数据,减少对数据库的访问次数
  2. 存储临时数据, 降低数据库的压力
  3. 存储计算结果, 降低计算的压力

缓存框架要考虑的方面

  1. 缓存的淘汰策略, 超过容量 LRU, FIFO, 过期时间
  2. 缓存的存储策略, 如内存缓存, 文件缓存, 数据库缓存
  3. 缓存key的管理

代理模式

通过一个外部 Proxy 来访问真实 cache 对象的属性和方法。

这个ConnectionProxy可以学习他用到的魔法方法,但本质上和设计模式没太多关系。

整个django项目里一共出现两次,一次在cache中作为default cache的入口,一次在db中作为defult db的入口

python 复制代码
# 没啥用,直接用caches['default']代替即可
class ConnectionProxy:
    """Proxy for accessing a connection object's attributes."""

    def __init__(self, connections, alias):
        self.__dict__["_connections"] = connections
        self.__dict__["_alias"] = alias

    # 重写__getattr__方法, 使得ConnectionProxy可以像访问真实的connection对象一样访问属性和方法
    def __getattr__(self, item):
        return getattr(self._connections[self._alias], item)

    # 重写__setattr__方法, 使得ConnectionProxy可以像访问真实的connection对象一样设置属性和方法
    def __setattr__(self, name, value):
        return setattr(self._connections[self._alias], name, value)

    # 重写__delattr__方法, 使得ConnectionProxy可以像访问真实的connection对象一样删除属性和方法
    def __delattr__(self, name):
        return delattr(self._connections[self._alias], name)

    # 重写__contains__方法, 使得ConnectionProxy可以使用 `key in ConnectionProxy`的语法来判断key是否存在于缓存中, 实际实现在BaseCache的各个子类中实现
    def __contains__(self, key):
        return key in self._connections[self._alias]

    # 重写__eq__方法, 使得ConnectionProxy可以使用 `ConnectionProxy == other`的语法来判断两个ConnectionProxy是否指向同一个缓存对象, 实际实现在BaseCache的各个子类中实现
    # 其实可以用total_ordering装饰器来实现__eq__方法, 但是为了保持一致性, 这里还是自己实现
    def __eq__(self, other):
        return self._connections[self._alias] == other

缓存基础类

可以学习的地方

  1. 参数默认值, 通常避免使用一些可变容器对象(list, dict),因为如果代码不严谨,容易出错。
    但是编辑器提示的时候,会告诉你默认值是 None,失去了一定的可读性。

所以可以参照 Django 的做法,使用一个名字对象来代替默认值参数。

python 复制代码
# 通常做法
def get_backend_timeout(self, timeout=None):
    """
    Return the timeout value usable by this backend based upon the provided
    """
    if timeout is None:
        timeout = self.default_timeout
    return timeout

# 改进做法
DEFAULT_TIMEOUT = object() # python模块单例
def get_backend_timeout(self, timeout=DEFAULT_TIMEOUT):
    """
    Return the timeout value usable by this backend based upon the provided
    """
    if timeout is DEFAULT_TIMEOUT: # is 比较内存地址
        timeout = self.default_timeout
    return timeout
  1. contains方法

实现 contains 方法可以改变in操作的结果

python 复制代码
    def __contains__(self, key):
        """
        Return True if the key is in the cache and has not expired.
        """
        # This is a separate method, rather than just a copy of has_key(),
        # so that it always has the same functionality as has_key(), even
        # if a subclass overrides it.
        return self.has_key(key)

总结

其他部分就是 BaseCache 的子类了,用对应的 client 实现缓存的方法。

相关推荐
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
Adolf_19931 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
冯宝宝^1 小时前
基于mongodb+flask(Python)+vue的实验室器材管理系统
vue.js·python·flask
叫我:松哥2 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
Eiceblue2 小时前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
NLP工程化2 小时前
对 Python 中 GIL 的理解
python·gil
极客代码2 小时前
OpenCV Python 深度指南
开发语言·人工智能·python·opencv·计算机视觉
liO_Oil2 小时前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装