如何告诉Django,memcached运行的项目大小大于默认值? [英] How to tell Django, that memcached running with item-size larger than default?

查看:106
本文介绍了如何告诉Django,memcached运行的项目大小大于默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的设置来增加memcached中的项目大小,但是我不能通过Django后端存储大于1MB的东西。
我知道 memcache 模块需要一些设置来实现那个,而Django在后台使用这个模块。

解决方案

对象的最大大小可以使用memcache.py 保存在memcached中:


在memcached FAQ中有两个条目: p>


  • 您可以存储的最大数据量是多少?为什么项目限于
    1兆字节大小?第一个的答案是(引用,重点是
    我的):


  • 可以在memcached中存储的值的最大大小是1兆字节。
    如果您的数据较大,请考虑客户端压缩或将
    的值分解为多个密钥。




所以我猜你的11MB文件太大,不能容纳一个
memcached条目。


如果您确实希望缓存较大的对象,您将不得不将Django的MemcachedCache子类化为不允许您传递选项

  self。 _client = self._lib.Client(self._servers,pickleProtocol = pickle.HIGHEST_PROTOCOL)

示例子类实现:

  from django.core.cache.backends.memcached import MemcachedCache 

class LargeMemcachedCache(MemcachedCache) :
大型对象的Memcached缓存

@property
def _cache(self):
if getat tr(self,'_client',None)是None:
self._client = self._lib.Client(self._servers,
pickleProtocol = pickle.HIGHEST_PROTOCOL,
server_max_value_length = 1024 * 1024 * 10)
return self._client


Im using new setting to increase item size in memcached, but i cant store something larger than 1mb through Django backend. I know that memcache module require some setting to achieve thath, and Django use this module in backend.

解决方案

From Maximum size of object that can be saved in memcached with memcache.py:

There are two entries about that in the memcached FAQ :

  • What is the maximum data size you can store? Why are items limited to 1 megabyte in size? The answer to the first one is (quoting, emphasis mine) :

  • The maximum size of a value you can store in memcached is 1 megabyte. If your data is larger, consider clientside compression or splitting the value up into multiple keys.

So I'm guessing your 11MB file is quite too big to fit in one memcached entry.

If you do really want to cache larger objects, you will have to subclass Django's MemcachedCache as it doesn't allow you to pass in options:

self._client = self._lib.Client(self._servers, pickleProtocol=pickle.HIGHEST_PROTOCOL)

Example subclass implementation:

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

这篇关于如何告诉Django,memcached运行的项目大小大于默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆