Memory error while caching heavy data with Django
<p>I'm using Django in order to archive a web with dynamically generated images from a big image stack (around 150mb each). It's suppose to be interactive so I need to keep cached the selected stack. For this, I'm using the memcached cache backend and the "django.contrib.sessions.backends.cache" session engine.</p>

<p>The problem is that when I render the page with the session data I keep getting MemoryError.
Is this the more efficient approach?
I'm supposing that is a problem with the size of the data. Is anyway of increase the memory size? Another way of solving this?</p>

<p>In views.py</p>

<pre><code>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)
</code></pre>

<p>In settings.py</p>

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

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
</code></pre>

<p>And here the complete error i get:</p>

<pre><code>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
</code></pre>