Automatic Django memcached configuration on Heroku.
django-heroku-memcacheify, simply run
pip install django-heroku-memcacheify and you'll get the latest version
NOTE: If you'd like to install this locally, you'll need to have the
libmemcached-dev libraries installed for this to compile properly. On
Debian and Ubuntu you can install this by running
sudo aptitude -y install
libmemcached-dev. If you're using a Mac, you can use
homebrew and run
brew install libmemcached.
Modify your Django
settings.py file, and set:
from memcacheify import memcacheify CACHES = memcacheify()
Next, edit your
requirements.txt file (which Heroku reads) and add
pylibmc==1.2.3 to the bottom of the file. This is required for Heroku to
detect the necessary C dependencies and 'bootstrap' your application. This requirement
has to be in the root
requirements.txt file, not in any imported requirements.
(Solution from Stack Overflow)
Assuming you have a memcache server available to your application on Heroku, it
will instantly be available. If you have no memcache addon provisioned for your
memcacheify will default to using local memory caching as a backup :)
Now that you've got Django configured to use memcache, all you need to do is install one memcache addons that Heroku provides!
I personally recommend MemCachier -- they're stable, cheap, great!
Let's say I want to install the
memcachier addon, I could simply run:
$ heroku addons:add memcachier:25 $ heroku config ... MEMCACHIER_SERVERS => memcachier1.example.net MEMCACHIER_USERNAME => bobslob MEMCACHIER_PASSWORD => l0nGr4ndoMstr1Ngo5strang3CHaR4cteRS ...
The example above will provision a free 25m memcache server for your
application. Assuming everything worked,
heroku config's output should show
that you now have 3 new environment variables set.
If you have a memcached server locally for development that doesn't support
authentication, you can still use memcache by setting an environment variable
This will set the default cache to
If there are no environment variables for memcache or memcacheify, the default
cache will be local memory
If you don't trust me, and want to make sure your caching is working as expected, you may do the following:
$ heroku run python manage.py shell Running python manage.py shell attached to terminal... up, run.1 Python 2.7.2 (default, Oct 31 2011, 16:22:04) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.core.cache import cache >>> cache.set('memcache', 'ify!') True >>> cache.get('memcache') 'ify!' >>>
Assuming everything is working, you should be able to set and retrieve cache keys.
If you're confused, you should probably read:
Want to run the tests? No problem:
$ git clone git://github.com/rdegges/django-heroku-memcacheify.git $ cd django-heroku-memcacheify $ python setup.py develop ... $ pip install -r requirements.txt # Install test dependencies. $ nosetests ............. ---------------------------------------------------------------------- Ran 13 tests in 0.166s OK
- Making the timeout option configurable. - Removing Python 2.5 support. - Adding an option to use memcached locally without SASL. - Updating the README, explaining how to use memcached locally.
- Update which allows memcachier users to support multiple servers >:) Thanks @alexlod!
- Fixing broken memcachier support.
- Adding support for memcachier Heroku addon. - Updating documentation. - Refactoring implementation for clarity. - Adding better tests.
- Initial release!