使用Django缓存大量数据时出现内存错误

时间:2016-06-29 00:25:22

标签: python django caching memcached django-sessions

我使用Django来存档网页,其中包含来自大图像堆栈(每个大约150mb)的动态生成的图像。它假设是交互式的,所以我需要保持缓存所选的堆栈。为此,我使用了memcached缓存后端和" django.contrib.sessions.backends.cache"会话引擎。

问题是,当我使用会话数据呈现页面时,我不断得到MemoryError。 这是更有效的方法吗? 我认为这是数据大小的问题。无论如何增加内存大小?解决这个问题的另一种方法是什么?

在views.py

def images(request):

    if request.method == 'POST':

        info_form = InfoForm(request.POST)

        if (info_form.is_valid()):

            if selected_stack_changed:
                matrix = get_matrix(stack_id)
                request.session['matrix'] = matrix
            else:
                matrix = request.session['matrix']

    return render(request, url)

在settings.py中

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

这是我得到的完整错误:

Internal Server Error: /data/
Traceback (most recent call last):
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 235, in get_response
response = middleware_method(request, response)
File "/home/user/.local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py", line 50, in process_response
request.session.save()
File "/home/user/.local/lib/python2.7/site-packages/django/contrib/sessions/backends/cache.py", line 62, in save
self.get_expiry_age())
File "/home/user/.local/lib/python2.7/site-packages/django/core/cache/backends/memcached.py", line 83, in set
if not self._cache.set(key, value, self.get_backend_timeout(timeout)):
File "/home/user/.local/lib/python2.7/site-packages/memcache.py", line 740, in set
return self._set("set", key, val, time, min_compress_len, noreply)
File "/home/user/.local/lib/python2.7/site-packages/memcache.py", line 1060, in _set
return _unsafe_set()
File "/home/user/.local/lib/python2.7/site-packages/memcache.py", line 1034, in _unsafe_set
store_info = self._val_to_store_info(val, min_compress_len)
File "/home/user/.local/lib/python2.7/site-packages/memcache.py", line 998, in _val_to_store_info
pickler.dump(val)
MemoryError

1 个答案:

答案 0 :(得分:0)

理想情况下,memcached服务器不应与Web服务器位于同一主机上。标准的memcached安装将使多个memcached实例独立运行,这些实例位于靠近Web服务器的各个服务器上,Web服务器通过网络与它们通信。更复杂的安装将具有由代理管理的多个memcached实例,该代理将请求路由到memcached服务器主机。一个流行的代理是Mcrouter