Setting up Memcached for Django session caching on App Engine

后端 未结 3 1030
抹茶落季
抹茶落季 2021-02-03 16:04

when setting up Django to use Memcached for caching (in my case, I want to to use session caching), in settings.py we set

CACHES = {
    \'default\         


        
相关标签:
3条回答
  • 2021-02-03 16:19

    As it happens, I have been porting a Django (1.6.5) application to GAE over the last few days (GAE Development SDK 1.9.6). I don't have a big need for caching right now but it's good to know it's available if I need it.

    So I just tried using django.core.cache.backends.memcached.MemcachedCache as my cache backend (set up as you describe in your question, and I put python-memcached in my libs folder) and

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

    to manage my sessions and GAE gave me the error:

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

    Anyway...

    ...even if you could get this to work it's surely better to use Google's API lib and borrow from the Django Memcached implementation, especially as the Google lib has been designed to be compatible with python-memcached and otherwise your app could break at any time with an SDK update. Create a python module such as 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's 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
    

    Then your cache setting becomes:

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

    That's it! This seems to work fine but I should be clear that it is not rigorously tested!

    Aside

    Have a poke around in google.appengine.api.memcache.__init__.py in your GAE SDK folder and you will find:

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

    i.e. Even if you could find a LOCATION for your memcache instance in the cloud, Google's own library would ignore it.

    0 讨论(0)
  • 2021-02-03 16:21

    The location should be set as your ip and port where your memcache daemon is running.

    Check this in django official documentation.

    Set LOCATION to ip:port values, where ip is the IP address of the Memcached daemon and port is the port on which Memcached is running, or to a unix:path value, where path is the path to a Memcached Unix socket file.

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

    0 讨论(0)
  • 2021-02-03 16:37

    If you are following this documentation http://www.allbuttonspressed.com/projects/djangoappengine

    And cloning this (as asked in the above link) https://github.com/django-nonrel/djangoappengine/blob/master/djangoappengine/settings_base.py

    I don't think you need to define a location. Is it throwing an error when you don't define it?

    0 讨论(0)
提交回复
热议问题