How to tell Django, that memcached running with item-size larger than default?

房东的猫 提交于 2020-01-05 10:07:35

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/16490819/how-to-tell-django-that-memcached-running-with-item-size-larger-than-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!