Skip to content

Commit 93b6746

Browse files
Merge branch 'master' into original_master
2 parents 9c3d689 + 6bf54be commit 93b6746

19 files changed

+93
-45
lines changed

.travis.yml

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1+
dist: xenial
12
language: python
23
services:
34
- memcached
45
- redis-server
5-
python:
6-
# python selected by tox, so specify only one version here
7-
- "3.6"
86
addons:
9-
postgresql: "9.5"
10-
before_install:
11-
# work around https://github.com/travis-ci/travis-ci/issues/8363
12-
- pyenv global system 3.5
7+
postgresql: "10"
138
before_script:
149
- psql -c 'create database travis_ci_test;' -U postgres
1510
- psql -c 'create database travis_ci_test2;' -U postgres
@@ -22,12 +17,19 @@ script:
2217
- tox -e $TOX_ENV
2318
after_success:
2419
- coveralls
25-
env:
26-
- TOX_ENV="dj18-py27,dj18-py34,dj18-py35"
27-
- TOX_ENV="dj19-py27,dj19-py34,dj19-py35,dj19-py36"
28-
- TOX_ENV="dj110-py27,dj110-py34,dj110-py35,dj110-py36"
29-
- TOX_ENV="dj111-py27,dj111-py34,dj111-py35,dj111-py36"
30-
- TOX_ENV="py27-flake8,py36-flake8"
31-
- TOX_ENV="docs"
20+
21+
matrix:
22+
include:
23+
- python: 2.7
24+
env: TOX_ENV="dj111-py27,py27-flake8"
25+
- python: 3.4
26+
env: TOX_ENV="dj111-py34,dj200-py34"
27+
- python: 3.5
28+
env: TOX_ENV="dj111-py35,dj200-py35,dj210-py35,dj220-py35"
29+
- python: 3.6
30+
env: TOX_ENV="dj111-py36,dj200-py36,dj210-py36,dj220-py36"
31+
- python: 3.7
32+
env: TOX_ENV="dj111-py37,dj200-py37,dj210-py37,dj220-py37,py37-flake8,docs"
33+
3234
# Adding sudo: False tells Travis to use their container-based infrastructure, which is somewhat faster.
3335
sudo: False

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ For full docs, see https://cache-machine.readthedocs.org/en/latest/.
1717
Requirements
1818
------------
1919

20-
Cache Machine works with Django 1.8-1.11 and Python 2.7, 3.4, 3.5 and 3.6.
20+
Cache Machine works with Django 1.11-2.2 and Python 2.7, 3.4, 3.5, 3.6, and 3.7.
2121

2222

2323
Installation

caching/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from __future__ import unicode_literals
22

3-
VERSION = ('1', '0', '1')
3+
VERSION = ('1', '1', '0')
44
__version__ = '.'.join(VERSION)

caching/base.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import functools
44
import logging
55

6+
import django
67
from django.core.cache.backends.base import DEFAULT_TIMEOUT
78
from django.db import models
89
from django.db.models import signals
9-
from django.db.models.sql import query, EmptyResultSet
10+
from django.db.models.sql import EmptyResultSet, query
1011
from django.utils import encoding
1112

1213
from caching import config
13-
from caching.invalidation import invalidator, flush_key, make_key, byid, cache
14+
from caching.invalidation import byid, cache, flush_key, invalidator, make_key
1415

1516
try:
1617
# ModelIterable is defined in Django 1.9+, and if it's present, we use it
@@ -30,7 +31,8 @@ def __iter__(self):
3031

3132
class CachingManager(models.Manager):
3233

33-
# Tell Django to use this manager when resolving foreign keys.
34+
# This option removed in Django 2.0
35+
# Tell Django to use this manager when resolving foreign keys. (Django < 2.0)
3436
use_for_related_fields = True
3537

3638
def get_queryset(self):
@@ -289,15 +291,25 @@ def _cache_keys(self, incl_db=True):
289291
"""Return the cache key for self plus all related foreign keys."""
290292
fks = dict((f, getattr(self, f.attname)) for f in self._meta.fields
291293
if isinstance(f, models.ForeignKey))
292-
keys = [fk.rel.to._cache_key(val, incl_db and self._state.db or None)
293-
for fk, val in list(fks.items())
294-
if val is not None and hasattr(fk.rel.to, '_cache_key')]
294+
295+
keys = []
296+
for fk, val in list(fks.items()):
297+
related_model = self._get_fk_related_model(fk)
298+
if val is not None and hasattr(related_model, '_cache_key'):
299+
keys.append(related_model._cache_key(val, incl_db and self._state.db or None))
300+
295301
return (self.get_cache_key(incl_db=incl_db),) + tuple(keys)
296302

297303
def _flush_keys(self):
298304
"""Return the flush key for self plus all related foreign keys."""
299305
return map(flush_key, self._cache_keys(incl_db=False))
300306

307+
def _get_fk_related_model(self, fk):
308+
if django.VERSION[0] >= 2:
309+
return fk.remote_field.model
310+
else:
311+
return fk.rel.to
312+
301313

302314
class CachingRawQuerySet(models.query.RawQuerySet):
303315

@@ -311,7 +323,10 @@ def __iter__(self):
311323
if self.timeout == config.NO_CACHE:
312324
iterator = iterator()
313325
while True:
314-
yield next(iterator)
326+
try:
327+
yield next(iterator)
328+
except StopIteration:
329+
return
315330
else:
316331
for obj in CachingModelIterable(self, iter_function=iterator, timeout=self.timeout):
317332
yield obj

caching/ext.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from django.conf import settings
44
from django.utils import encoding
5-
65
from jinja2 import nodes
76
from jinja2.ext import Extension
87

caching/invalidation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.core.cache import cache as default_cache
1111
from django.core.cache import caches
1212
from django.core.cache.backends.base import InvalidCacheBackendError
13-
from django.utils import encoding, translation, six
13+
from django.utils import encoding, six, translation
1414
from django.utils.six.moves.urllib.parse import parse_qsl
1515

1616
from caching import config

docs/index.rst

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Here's what a minimal cached model looks like::
7676

7777
objects = CachingManager()
7878

79+
# if you use Django 2.0 or later, you must set base_manager_name
80+
class Meta:
81+
base_manager_name = 'objects' # Attribute name of CachingManager(), above
82+
7983
Whenever you run a query, ``CachingQuerySet`` will try to find that query in
8084
the cache. Queries are keyed by ``{prefix}:{sql}``. If it's there, we return
8185
the cached result set and everyone is happy. If the query isn't in the cache,

docs/releases.rst

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
Release Notes
44
==================
55

6+
v1.1.0 (2019-02-17)
7+
-------------------
8+
9+
- Drop official support for unsupported Django versions (1.8, 1.9, and 1.10)
10+
- Add support for Django 2.0, 2.1, and 2.2 (thanks, @JungleKim and @wetneb!)
11+
- Add support for Python 3.7
12+
- Fix Travis
13+
614
v1.0.0 (2017-10-13)
715
-------------------
816

examples/cache_machine/custom_backend.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .settings import *
23

34
CACHES = {
45
'default': {

examples/cache_machine/django_redis_settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .redis_settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .redis_settings import *
23

34
CACHES = {
45
'default': {

examples/cache_machine/locmem_settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .settings import *
23

34
CACHES = {
45
'default': {
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .settings import *
23

34
FETCH_BY_ID = True

examples/cache_machine/redis_byid.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .redis_settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .redis_settings import *
23

34
FETCH_BY_ID = True
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .settings import * # flake8: noqa
1+
# flake8: noqa
2+
from .settings import *
23

34
CACHE_MACHINE_USE_REDIS = True
45
REDIS_BACKEND = 'redis://'

examples/cache_machine/settings.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22

3+
import django
4+
35
CACHES = {
46
'default': {
57
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
@@ -46,3 +48,6 @@
4648
'django.contrib.messages.middleware.MessageMiddleware',
4749
'django.middleware.clickjacking.XFrameOptionsMiddleware',
4850
)
51+
52+
if django.VERSION[0] >= 2:
53+
MIDDLEWARE = MIDDLEWARE_CLASSES

run_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
DJANGO_SETTINGS_MODULE, so I run these commands which get the right env
55
automatically.
66
"""
7+
import argparse
78
import os
89
import sys
9-
import argparse
1010
from subprocess import call, check_output
1111

1212
NAME = os.path.basename(os.path.dirname(__file__))

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import caching
44

5-
65
setup(
76
name='django-cache-machine',
87
version=caching.__version__,
@@ -31,6 +30,7 @@
3130
'Programming Language :: Python :: 3.4',
3231
'Programming Language :: Python :: 3.5',
3332
'Programming Language :: Python :: 3.6',
33+
'Programming Language :: Python :: 3.7',
3434
'Topic :: Software Development :: Libraries :: Python Modules',
3535
]
3636
)

tests/testapp/models.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import django
34
from django.db import models
45
from django.utils import six
56
from caching.base import CachingMixin, CachingManager, cached_method
@@ -19,11 +20,16 @@ class User(CachingMixin, models.Model):
1920

2021
objects = CachingManager()
2122

23+
if django.VERSION[0] >= 2:
24+
class Meta:
25+
# Tell Django to use this manager when resolving foreign keys. (Django >= 2.0)
26+
base_manager_name = 'objects'
27+
2228

2329
class Addon(CachingMixin, models.Model):
2430
val = models.IntegerField()
25-
author1 = models.ForeignKey(User)
26-
author2 = models.ForeignKey(User, related_name='author2_set')
31+
author1 = models.ForeignKey(User, on_delete=models.CASCADE)
32+
author2 = models.ForeignKey(User, related_name='author2_set', on_delete=models.CASCADE)
2733

2834
objects = CachingManager()
2935

tox.ini

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
[tox]
77
envlist =
8-
dj{18}-py{27,34,35}
9-
dj{19,110,111}-py{27,34,35,36}
10-
py{27,36}-flake8
8+
dj{111}-py{27,34,35,36,37}
9+
dj{200}-py{34,35,36,37}
10+
dj{210}-py{35,36,37}
11+
dj{220}-py{35,36,37}
12+
py{27,37}-flake8
1113
docs
1214

1315
[testenv]
@@ -16,17 +18,18 @@ basepython =
1618
py34: python3.4
1719
py35: python3.5
1820
py36: python3.6
21+
py37: python3.7
1922
commands = {envpython} run_tests.py --with-coverage
2023
deps =
21-
py{26,27}: -rrequirements/py2.txt
22-
py{34,35,36}: -rrequirements/py3.txt
23-
dj18: Django>=1.8,<1.9
24-
dj19: Django>=1.9,<1.10
25-
dj110: Django>=1.10,<1.11
24+
py27: -rrequirements/py2.txt
25+
py{34,35,36,37}: -rrequirements/py3.txt
2626
dj111: Django>=1.11,<2.0
27+
dj200: Django>=2.0,<2.1
28+
dj210: Django>=2.1,<2.2
29+
dj220: Django==2.2b1
2730

2831
[testenv:docs]
29-
basepython = python3.6
32+
basepython = python3.7
3033
deps =
3134
Sphinx
3235
Django
@@ -40,6 +43,6 @@ commands = /usr/bin/make html
4043
deps = flake8
4144
commands = flake8
4245

43-
[testenv:py36-flake8]
46+
[testenv:py37-flake8]
4447
deps = flake8
4548
commands = flake8

0 commit comments

Comments
 (0)