Skip to content

Commit

Permalink
Django 1.9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vkurup committed Oct 7, 2017
1 parent 36589e9 commit f6839e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -24,7 +24,7 @@ after_success:
- coveralls
env:
- TOX_ENV="dj18-py27,dj18-py34,dj18-py35"
# - TOX_ENV="dj19-py27,dj19-py34,dj19-py35,dj19-py36"
- TOX_ENV="dj19-py27,dj19-py34,dj19-py35,dj19-py36"
# - TOX_ENV="dj110-py27,dj110-py34,dj110-py35,dj110-py36"
# - TOX_ENV="dj111-py27,dj111-py34,dj111-py35,dj111-py36"
- TOX_ENV="py27-flake8,py36-flake8"
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -17,7 +17,7 @@ For full docs, see https://cache-machine.readthedocs.org/en/latest/.
Requirements
------------

Cache Machine works with Django 1.8 and Python 2.7, 3.4, 3.5 and 3.6.
Cache Machine works with Django 1.8-1.9 and Python 2.7, 3.4, 3.5 and 3.6.


Installation
Expand Down
32 changes: 23 additions & 9 deletions caching/base.py
Expand Up @@ -9,6 +9,13 @@
from django.db.models.sql import query, EmptyResultSet
from django.utils import encoding

try:
from django.db.models.query import ValuesListIterable
except ImportError:
# ValuesListIterable is defined in Django 1.9+, and if it's present, we
# need to workaround a possible infinite recursion. See CachingQuerySet.iterator()
ValuesListIterable = None

from caching import config
from .invalidation import invalidator, flush_key, make_key, byid, cache

Expand Down Expand Up @@ -162,15 +169,22 @@ def iterator(self):
iterator = super(CachingQuerySet, self).iterator
if self.timeout == config.NO_CACHE:
return iter(iterator())
else:
try:
# Work-around for Django #12717.
query_string = self.query_key()
except query.EmptyResultSet:
return iterator()
if config.FETCH_BY_ID:
iterator = self.fetch_by_id
return iter(CacheMachine(self.model, query_string, iterator, self.timeout, db=self.db))

try:
# Work-around for Django #12717.
query_string = self.query_key()
except query.EmptyResultSet:
return iterator()
if config.FETCH_BY_ID:
# fetch_by_id uses a ValuesList to get a list of pks. If we are
# currently about to run that query, we DON'T want to use the
# fetch_by_id iterator or else we will run into an infinite
# recursion. So, if we are about to run that query, use the
# standard iterator.
if ValuesListIterable and self._iterable_class == ValuesListIterable:
return iter(iterator())
iterator = self.fetch_by_id
return iter(CacheMachine(self.model, query_string, iterator, self.timeout, db=self.db))

def fetch_by_id(self):
"""
Expand Down

0 comments on commit f6839e4

Please sign in to comment.