我还不能添加评论,但我想分享我对这个问题的快速修复,因为当你更改SERVER_MAX_VALUE_LENGTH
at import 时间时,python-memcached 的行为异常我遇到了同样的问题。
好吧,除了__init__
FizxMike 建议的编辑之外,您还可以在同一类中编辑 _cache 属性。这样做可以实例化server_max_value_length
显式传递的 python-memcached 客户端,如下所示:
from django.core.cache.backends.memcached import BaseMemcachedCache
DEFAULT_MAX_VALUE_LENGTH = 1024 * 1024
class MemcachedCache(BaseMemcachedCache):
def __init__(self, server, params):
#options from the settings['CACHE'][connection]
self._options = params.get("OPTIONS", {})
import memcache
memcache.SERVER_MAX_VALUE_LENGTH = self._options.get('SERVER_MAX_VALUE_LENGTH', DEFAULT_MAX_VALUE_LENGTH)
super(MemcachedCache, self).__init__(server, params,
library=memcache,
value_not_found_exception=ValueError)
@property
def _cache(self):
if getattr(self, '_client', None) is None:
server_max_value_length = self._options.get("SERVER_MAX_VALUE_LENGTH", DEFAULT_MAX_VALUE_LENGTH)
#one could optionally send more parameters here through the options settings,
#I simplified here for brevity
self._client = self._lib.Client(self._servers,
server_max_value_length=server_max_value_length)
return self._client
我也更喜欢创建另一个继承自 BaseMemcachedCache 的后端并使用它而不是编辑 django 代码。
这是 django memcached 后端模块供参考:
https ://github.com/django/django/blob/master/django/core/cache/backends/memcached.py
感谢您对此线程的所有帮助!