Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Memcached for Django session caching on App Engine

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': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

I will be running the project in App Engine so my question is what do I do to for the LOCATION entry?

like image 863
pbhowmick Avatar asked Dec 03 '22 19:12

pbhowmick


1 Answers

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.

like image 125
Steven Avatar answered Dec 26 '22 09:12

Steven