Skip to content

Commit

Permalink
Merge pull request #268 from carlosfunk/cache-key-helpers
Browse files Browse the repository at this point in the history
Cache key helper functions
  • Loading branch information
bernardopires committed Jul 30, 2015
2 parents 6722b34 + 21a94f4 commit 68323c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ If you put this in the same Django project, you can make a new ``settings_public

Or you can create a completely separate project for the main website.

Caching
-------

To enable tenant aware caching you can set the `KEY_FUNCTION <https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-CACHES-KEY_FUNCTION>`_ setting to use the provided ``make_key`` helper function which
adds the tenants ``schema_name`` as the first key prefix.

.. code-block:: python
CACHES = {
"default": {
...
'KEY_FUNCTION': 'tenant_schemas.cache.make_key',
'REVERSE_KEY_FUNCTION': 'tenant_schemas.cache.reverse_key',
},
}
The ``REVERSE_KEY_FUNCTION`` setting is only required if you are using the `django-redis <https://github.com/niwinz/django-redis>`_ cache backend.

Configuring your Apache Server (optional)
=========================================
Here's how you can configure your Apache server to route all subdomains to your django project so you don't have to setup any subdomains manually.
Expand All @@ -238,3 +256,4 @@ formats using `Sphinx <http://pypi.python.org/pypi/Sphinx>`_. To get started
make html
This creates the documentation in HTML format at ``docs/_build/html``.

20 changes: 20 additions & 0 deletions tenant_schemas/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import connection


def make_key(key, key_prefix, version):
"""
Tenant aware function to generate a cache key.
Constructs the key used by all other methods. Prepends the tenant
`schema_name` and `key_prefix'.
"""
return '%s:%s:%s:%s' % (connection.schema_name, key_prefix, version, key)


def reverse_key(key):
"""
Tenant aware function to reverse a cache key.
Required for django-redis REVERSE_KEY_FUNCTION setting.
"""
return key.split(':', 3)[3]
1 change: 1 addition & 0 deletions tenant_schemas/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from test_routes import *
from test_tenants import *
from test_cache import *
13 changes: 13 additions & 0 deletions tenant_schemas/tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tenant_schemas.cache import make_key, reverse_key
from tenant_schemas.test.cases import TenantTestCase


class CacheHelperTestCase(TenantTestCase):
def test_make_key(self):
key = make_key(key='foo', key_prefix='', version=1)
tenant_prefix = key.split(':')[0]
self.assertEqual(self.tenant.schema_name, tenant_prefix)

def test_reverse_key(self):
key = 'foo'
self.assertEqual(key, reverse_key(make_key(key=key, key_prefix='', version=1)))

0 comments on commit 68323c5

Please sign in to comment.