Skip to main content
 首页 » 编程设计

python之Django 内存缓存 : Compare and Set

2024年08月30日25kuangbin

结束here at the Django groups Tom Evans explains the method在 Django 中进行比较和设置,如下所示

You can access the memcached client via django though:  
>>> from django.core import cache  
>>> c=cache.get_cache('default')  
>>> help(c._client.cas)  

但不知何故我无法让它工作。

>>> from django.core import cache 
>>> c=cache.get_cache('memcache') 
>>> help(c._client.cas) 
Traceback (most recent call last): 
  File "<console>", line 1, in <module> 
AttributeError: 'MemcachedCache' object has no attribute '_client' 

如果不是上面显示的方法,我如何才能在 Django 中进行比较和设置?

我使用 Django 1.3 版。

请您参考如下方法:

看完源码!我在 BaseMemcachedCache 找到这个:

@property 
def _cache(self): 
    """ 
    Implements transparent thread-safe access to a memcached client. 
    """ 
    if getattr(self, '_client', None) is None: 
        self._client = self._lib.Client(self._servers) 
 
    return self._client 

所以,我会说,这会起作用:

c._cache.cas 

试试,然后告诉我!

了解更多详情:https://code.djangoproject.com/svn/django/trunk/django/core/cache/backends/memcached.py