Configuring Memcached to Cache Django Sessions in App Engine

when configuring Django to use Memcached for caching (in my case I want to use session caching), settings.pywe set

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

I will run the project in App Engine, so my question is what am I doing for recording LOCATION?

+4
source share
3 answers

As this happens, I have migrated the Django application (1.6.5) to GAE in the last few days (GAE Development SDK 1.9.6). I don't have much need for caching right now, but it's good to know this if I need it.

django.core.cache.backends.memcached.MemcachedCache ( , , python-memcached libs)

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

GAE :

RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.

...

... , , API Google Django Memcached, , lib python-memcached, SDK. python, my_project/backends.py:

import pickle
from django.core.cache.backends.memcached import BaseMemcachedCache


class GaeMemcachedCache(BaseMemcachedCache):
    "An implementation of a cache binding using google app engine memcache lib (compatible with python-memcached)"
    def __init__(self, server, params):
        from google.appengine.api import memcache
        super(GaeMemcachedCache, self).__init__(server, params,
                                             library=memcache,
                                             value_not_found_exception=ValueError)

    @property
    def _cache(self):
        if getattr(self, '_client', None) is None:
            self._client = self._lib.Client(self._servers, pickleProtocol=pickle.HIGHEST_PROTOCOL)
        return self._client

:

CACHES = {
    'default': {
        'BACKEND': 'my_project.backends.GaeMemcachedCache',
    }
}

! , , , .

google.appengine.api.memcache.__init__.py GAE SDK, :

  def __init__(self, servers=None, debug=0,
               pickleProtocol=cPickle.HIGHEST_PROTOCOL,
               pickler=cPickle.Pickler,
               unpickler=cPickle.Unpickler,
               pload=None,
               pid=None,
               make_sync_call=None,
               _app_id=None):
    """Create a new Client object.

    No parameters are required.

    Arguments:
      servers: Ignored; only for compatibility.
    ...

. LOCATION memcache , Google .

+15

ip , memcache.

django.

LOCATION ip: , ip - IP- Memcached - , Memcached, unix: path, path - Memcached Unix .

https://docs.djangoproject.com/en/dev/topics/cache/

0
0
source

Source: https://habr.com/ru/post/1548077/


All Articles