我使用新设置来增加memcached中的项目大小,但我不能通过Django后端存储大于1mb的内容。
我知道memcache
模块需要一些设置来实现它,而Django在后端使用这个模块。
答案 0 :(得分:5)
来自Maximum size of object that can be saved in memcached with memcache.py:
memcached常见问题解答中有两个条目:
您可以存储的最大数据大小是多少?为什么物品限于 1兆字节?第一个答案是(引用,强调 我的):
您可以在memcached中存储的值的最大大小为1兆字节。 如果您的数据较大,请考虑客户端压缩或拆分 值可以分为多个键。
所以我猜你的11MB文件太大而不适合一个 memcached条目。
如果你真的想要缓存更大的对象,你必须将Django的MemcachedCache子类化为doesn't allow you to pass in options:
self._client = self._lib.Client(self._servers, pickleProtocol=pickle.HIGHEST_PROTOCOL)
子类实现示例:
from django.core.cache.backends.memcached import MemcachedCache
class LargeMemcachedCache(MemcachedCache):
"Memcached cache for large objects"
@property
def _cache(self):
if getattr(self, '_client', None) is None:
self._client = self._lib.Client(self._servers,
pickleProtocol=pickle.HIGHEST_PROTOCOL,
server_max_value_length = 1024*1024*10)
return self._client
答案 1 :(得分:0)
在最近的 Django 版本中,您不需要子类化缓存类,而是可以在 OPTIONS
中指定传递给缓存类的构造函数的参数:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'KEY_FUNCTION': 'some.path.to.a.function',
'OPTIONS': {
'server_max_value_length': 1024 * 1024 * 10
}
}
}
见docs。
请注意,您还必须通过添加以下行来增加 memcached 本身的大小:
-I 10m
到/etc/memcached.conf
并重新启动它:
sudo service memcached restart