Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from vijaykatam/django_1_7_caches
Browse files Browse the repository at this point in the history
Use django.core.cache.caches as it stores off cache backend reference…
  • Loading branch information
vijaykatam committed Aug 28, 2015
2 parents ffc19ba + 1957f50 commit 2db8c31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
History
-------

0.2
---------------------
* Use django.core.cache.caches available in django 1.7 for efficient cache backend access

0.1.5
---------------------
* [BUGFIX] - Fix for non-ascii characters in query.
Expand Down
15 changes: 11 additions & 4 deletions django_cache_manager/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import uuid

import django
from django.core.cache import get_cache
from django.conf import settings
from django.db.models.fields.related import RelatedField
Expand All @@ -11,6 +12,9 @@
from .model_cache_sharing import model_cache_backend
from .models import update_model_cache

if django.get_version() >= '1.7':
from django.core.cache import caches

_cache_name = getattr(settings, 'django_cache_manager.cache_backend', 'django_cache_manager.cache_backend')
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -67,16 +71,15 @@ def invalidate_model_cache(self):
logger.info('Invalidating cache for table {0}'.format(self.model._meta.db_table))
related_tables = set([rel.model._meta.db_table for rel in self.model._meta.get_all_related_objects()])
# temporary fix for m2m relations with an intermediate model, goes away after better join caching
related_tables |= set([field.rel.to._meta.db_table for field in self.model._meta.fields if issubclass(type(field), RelatedField)])
related_tables |= set([field.rel.to._meta.db_table for field in self.model._meta.fields if issubclass(type(field), RelatedField)])
logger.debug('Related tables of model {0} are {1}'.format(self.model, related_tables))
update_model_cache(self.model._meta.db_table)
for related_table in related_tables:
update_model_cache(related_table)
update_model_cache(related_table)


class CacheBackendMixin(object):

# TODO - django 1.7 has thread safe module level cache interface
@property
def cache_backend(self):
"""
Expand All @@ -88,5 +91,9 @@ def cache_backend(self):
"""
if not hasattr(self, '_cache_backend'):
self._cache_backend = get_cache(_cache_name)
# determine django version for getting cache backend
if django.get_version() >= '1.7':
self._cache_backend = caches[_cache_name]
else:
self._cache_backend = get_cache(_cache_name)
return self._cache_backend
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='django-cache-manager',
version='0.1.5',
version='0.2',
description='Cache manager for django models',
long_description=readme + '\n\n' + history,
author='Vijay Katam',
Expand Down

0 comments on commit 2db8c31

Please sign in to comment.