Skip to content

Commit

Permalink
For issue:
Browse files Browse the repository at this point in the history
noripyt#135

Have the table key generation look for a "CACHE_KEY" configuration in the Django DATABASES configs.

If a "CACHE_KEY" value is found, then we use that instead of the "db_alias".

This allows us to synch Master and Replicas to use the same key namespace to prevent "double-caching" and the need to invalidate 2 cache entries upon an update.

The Django configuration would look like:

DATABASES = {
    'master': {
        'CACHE_KEY': 'WE_ARE_ONE',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'HOST': DB_HOST,
        'PORT': DB_PORT,
        'OPTIONS': {
            'client_encoding': "UTF8"
        }
    },
    'replica' : {
        'CACHE_KEY': 'WE_ARE_ONE',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'HOST': DB_HOST,
        'PORT': DB_PORT,
        'OPTIONS': {
            'client_encoding': "UTF8"
        }
    }
}
  • Loading branch information
langal committed Aug 15, 2019
1 parent f3f8773 commit 9ab9b49
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cachalot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class IsRawQuery(Exception):
}
CACHABLE_PARAM_TYPES.update(integer_types) # Adds long for Python 2
UNCACHABLE_FUNCS = {Now, TransactionNow}
OVERRIDE_DB_ALIAS_KEY = 'CACHE_KEY'

try:
from psycopg2 import Binary
Expand Down Expand Up @@ -91,6 +92,8 @@ def get_table_cache_key(db_alias, table):
:return: A cache key
:rtype: int
"""
if OVERRIDE_DB_ALIAS_KEY in cachalot_settings.DATABASES[db_alias]:
db_alias = cachalot_settings.DATABASES[db_alias][OVERRIDE_DB_ALIAS_KEY]
cache_key = '%s:%s' % (db_alias, table)
return sha1(cache_key.encode('utf-8')).hexdigest()

Expand Down

0 comments on commit 9ab9b49

Please sign in to comment.