|
6 | 6 |
|
7 | 7 | import os |
8 | 8 | import re |
| 9 | +import copy |
9 | 10 | import shutil |
10 | 11 | import tempfile |
11 | 12 | import threading |
|
15 | 16 |
|
16 | 17 | from django.conf import settings |
17 | 18 | from django.core import management |
18 | | -from django.core.cache import cache, caches, CacheKeyWarning, InvalidCacheBackendError |
| 19 | +from django.core.cache import (cache, caches, CacheKeyWarning, |
| 20 | + InvalidCacheBackendError, DEFAULT_CACHE_ALIAS) |
19 | 21 | from django.db import connection, router, transaction |
20 | 22 | from django.core.cache.utils import make_template_fragment_key |
21 | 23 | from django.http import HttpResponse, StreamingHttpResponse |
@@ -1175,7 +1177,7 @@ def test_custom_key_validation(self): |
1175 | 1177 | class GetCacheTests(IgnorePendingDeprecationWarningsMixin, TestCase): |
1176 | 1178 |
|
1177 | 1179 | def test_simple(self): |
1178 | | - from django.core.cache import caches, DEFAULT_CACHE_ALIAS, get_cache |
| 1180 | + from django.core.cache import caches, get_cache |
1179 | 1181 | self.assertIsInstance( |
1180 | 1182 | caches[DEFAULT_CACHE_ALIAS], |
1181 | 1183 | get_cache('default').__class__ |
@@ -1204,6 +1206,82 @@ def test_close_deprecated(self): |
1204 | 1206 | self.assertTrue(cache.closed) |
1205 | 1207 |
|
1206 | 1208 |
|
| 1209 | +DEFAULT_MEMORY_CACHES_SETTINGS = { |
| 1210 | + 'default': { |
| 1211 | + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', |
| 1212 | + 'LOCATION': 'unique-snowflake', |
| 1213 | + } |
| 1214 | +} |
| 1215 | +NEVER_EXPIRING_CACHES_SETTINGS = copy.deepcopy(DEFAULT_MEMORY_CACHES_SETTINGS) |
| 1216 | +NEVER_EXPIRING_CACHES_SETTINGS['default']['TIMEOUT'] = None |
| 1217 | + |
| 1218 | + |
| 1219 | +class DefaultNonExpiringCacheKeyTests(TestCase): |
| 1220 | + """Tests that verify that settings having Cache arguments with a TIMEOUT |
| 1221 | + set to `None` will create Caches that will set non-expiring keys. |
| 1222 | +
|
| 1223 | + This fixes ticket #22085. |
| 1224 | + """ |
| 1225 | + def setUp(self): |
| 1226 | + # The 5 minute (300 seconds) default expiration time for keys is |
| 1227 | + # defined in the implementation of the initializer method of the |
| 1228 | + # BaseCache type. |
| 1229 | + self.DEFAULT_TIMEOUT = caches[DEFAULT_CACHE_ALIAS].default_timeout |
| 1230 | + |
| 1231 | + def tearDown(self): |
| 1232 | + del(self.DEFAULT_TIMEOUT) |
| 1233 | + |
| 1234 | + def test_default_expiration_time_for_keys_is_5_minutes(self): |
| 1235 | + """The default expiration time of a cache key is 5 minutes. |
| 1236 | +
|
| 1237 | + This value is defined inside the __init__() method of the |
| 1238 | + :class:`django.core.cache.backends.base.BaseCache` type. |
| 1239 | + """ |
| 1240 | + self.assertEquals(300, self.DEFAULT_TIMEOUT) |
| 1241 | + |
| 1242 | + def test_caches_with_unset_timeout_has_correct_default_timeout(self): |
| 1243 | + """Caches that have the TIMEOUT parameter undefined in the default |
| 1244 | + settings will use the default 5 minute timeout. |
| 1245 | + """ |
| 1246 | + cache = caches[DEFAULT_CACHE_ALIAS] |
| 1247 | + self.assertEquals(self.DEFAULT_TIMEOUT, cache.default_timeout) |
| 1248 | + |
| 1249 | + @override_settings(CACHES=NEVER_EXPIRING_CACHES_SETTINGS) |
| 1250 | + def test_caches_set_with_timeout_as_none_has_correct_default_timeout(self): |
| 1251 | + """Memory caches that have the TIMEOUT parameter set to `None` in the |
| 1252 | + default settings with have `None` as the default timeout. |
| 1253 | +
|
| 1254 | + This means "no timeout". |
| 1255 | + """ |
| 1256 | + cache = caches[DEFAULT_CACHE_ALIAS] |
| 1257 | + self.assertIs(None, cache.default_timeout) |
| 1258 | + self.assertEquals(None, cache.get_backend_timeout()) |
| 1259 | + |
| 1260 | + @override_settings(CACHES=DEFAULT_MEMORY_CACHES_SETTINGS) |
| 1261 | + def test_caches_with_unset_timeout_set_expiring_key(self): |
| 1262 | + """Memory caches that have the TIMEOUT parameter unset will set cache |
| 1263 | + keys having the default 5 minute timeout. |
| 1264 | + """ |
| 1265 | + key = "my-key" |
| 1266 | + value = "my-value" |
| 1267 | + cache = caches[DEFAULT_CACHE_ALIAS] |
| 1268 | + cache.set(key, value) |
| 1269 | + cache_key = cache.make_key(key) |
| 1270 | + self.assertNotEquals(None, cache._expire_info[cache_key]) |
| 1271 | + |
| 1272 | + @override_settings(CACHES=NEVER_EXPIRING_CACHES_SETTINGS) |
| 1273 | + def text_caches_set_with_timeout_as_none_set_non_expiring_key(self): |
| 1274 | + """Memory caches that have the TIMEOUT parameter set to `None` will set |
| 1275 | + a non expiring key by default. |
| 1276 | + """ |
| 1277 | + key = "another-key" |
| 1278 | + value = "another-value" |
| 1279 | + cache = caches[DEFAULT_CACHE_ALIAS] |
| 1280 | + cache.set(key, value) |
| 1281 | + cache_key = cache.make_key(key) |
| 1282 | + self.assertEquals(None, cache._expire_info[cache_key]) |
| 1283 | + |
| 1284 | + |
1207 | 1285 | @override_settings( |
1208 | 1286 | CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix', |
1209 | 1287 | CACHE_MIDDLEWARE_SECONDS=1, |
|
0 commit comments