`dir()` 不显示 Django 中的缓存对象属性

问题描述 投票:0回答:3

我可以使用这些缓存方法 set(), get(), touch(), incr(), decr(), incr_version(), decr_version(), delete()delete_many()clear()close()如下所示:

from django.core.cache import cache

cache.set("first_name", "John")
cache.set("last_name", "Smith")
cache.set_many({"age": 36, "gender": "Male"})

cache.get("first_name")
cache.get_or_set("last_name", "Doesn't exist")
cache.get_many(["age", "gender"])

cache.touch("first_name", 60)

cache.incr("age")
cache.decr("age")

cache.incr_version("first_name")
cache.decr_version("last_name")

cache.delete("first_name")
cache.delete_many(["last_name", "age"])

cache.clear()

cache.close()

但是,dir()不显示这些缓存方法,如下所示:

from django.core.cache import cache

print(dir(cache)) # Here
[
  '__class__', '__contains__', '__delattr__', '__dict__', '__dir__',
  '__doc__', '__eq__', '__format__', '__ge__', '__getattr__',
  '__getattribute__', '__gt__', '__hash__', '__init__', 
  '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__',
  '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
  '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
  '_alias', '_connections'
]

那么,如何显示这些缓存方法呢?

python django attributes django-cache django-caching
3个回答
0
投票

您可以随时查看源代码...

在这种情况下,这是因为您从此处

导入的 
cache 值实际上是一个 ConnectionProxy
 对象。

cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS)

https://github.com/django/django/blob/main/django/utils/connection.py#L7

...该对象没有任何这些方法,而是使用

__getattr__

 将这些属性的使用“代理”到其他对象实例。

def __getattr__(self, item): return getattr(self._connections[self._alias], item)
除非您设置了一些更复杂的缓存配置,否则您可以通过执行以下操作来检查这些方法:

dir(cache._connections["default"])
    

0
投票
您可以从

BaseCache

 显示所有这些方法

from django.core.cache.backends.base import BaseCache dir(BaseCache) ['__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_missing_key', 'aadd', 'aclear', 'aclose', 'add', 'adecr', 'adecr_version', 'adelete', 'adelete_many', 'aget', 'aget_many', 'aget_or_set', 'ahas_key', 'aincr', 'aincr_version', 'aset', 'aset_many', 'atouch', 'clear', 'close', 'decr', 'decr_version', 'delete', 'delete_many', 'get', 'get_backend_timeout', 'get_many', 'get_or_set', 'has_key', 'incr', 'incr_version', 'make_and_validate_key', 'make_key', 'set', 'set_many', 'touch', 'validate_key']
来自源代码

doc

缓存框架。

这个包定义了一组缓存后端,它们都符合 简单的API。简而言之,缓存是一组值——可以是 任何可以被腌制的对象——由字符串键标识。为了 完整的API,请参见抽象BaseCache类 django.core.cache.backends.base.


0
投票
这是因为

cache

 是默认缓存的代理,
确实[GitHub]

class ConnectionProxy: # … def __getattr__(self, item): return getattr(self._connections[self._alias], item) # …

因此,对于作为属性的方法,它将回退到

__getattr__

,然后不会转发该项目。

您可以通过

monkey patching

来修补
__dir__方法:

from django.utils.connection import ConnectionProxy def some_dir(self): return dir(self._connections[self._alias]) ConnectionProxy.__dir__ = some_dir
例如在

ready

中,使用
dir
时会返回相应的方法。

© www.soinside.com 2019 - 2024. All rights reserved.