python - 如何告诉 Django,memcached 运行时项目大小大于默认值?

标签 python django caching memcached

我使用新设置来增加 memcached 中的项目大小,但我无法通过 Django 后端存储大于 1mb 的内容。 我知道 memcache 模块需要一些设置来实现它,而 Django 在后端使用这个模块。

最佳答案

来自 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.

如果你真的想缓存更大的对象,你将不得不继承 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

关于python - 如何告诉 Django,memcached 运行时项目大小大于默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16490819/

相关文章:

javascript - Django 框架,网格行中的增删改查

python - 单元测试通过了 Django 中模型的正则表达式验证器

caching - 使用 SSE/AVX 对压缩双向量的部分进行非时间存储

java - 创建通用对象的缓存

ruby-on-rails - Ruby on Rails : How to set up "find" options in order to not use cache

python - 在无法访问模型类代码的情况下保存 PyTorch 模型

python - 使用 Python 比较 UTC 时间和东部时间

python - 设置 django 到 mysql

python - 在字典中查找并返回键的 pandas 值 - python

python - 如何从第二项迭代python dict