How to tell Django, that memcached running with it

2019-07-29 15:08发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-07-29 15:35

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
查看更多
登录 后发表回答