mirror of
https://github.com/django/django.git
synced 2025-08-03 02:23:12 +00:00
Add warning when using cache keys that might not work with memcached.
This means testing with local dev caches (not memcache) will warn developers if they are introducing inadvertent importabilities. There is also the ability to silence the warning if a dev is not planning to use memcache and knows what they are doing with their keys. Thanks to Carl Meyer for the patch. Fixed #6447. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13766 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bfbc259de0
commit
fc26da645a
10 changed files with 167 additions and 10 deletions
62
tests/regressiontests/cache/tests.py
vendored
62
tests/regressiontests/cache/tests.py
vendored
|
@ -8,11 +8,12 @@ import shutil
|
|||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import management
|
||||
from django.core.cache import get_cache
|
||||
from django.core.cache.backends.base import InvalidCacheBackendError
|
||||
from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning
|
||||
from django.http import HttpResponse, HttpRequest
|
||||
from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware
|
||||
from django.utils import translation
|
||||
|
@ -366,6 +367,33 @@ class BaseCacheTests(object):
|
|||
count = count + 1
|
||||
self.assertEqual(count, final_count)
|
||||
|
||||
def test_invalid_keys(self):
|
||||
"""
|
||||
All the builtin backends (except memcached, see below) should warn on
|
||||
keys that would be refused by memcached. This encourages portable
|
||||
caching code without making it too difficult to use production backends
|
||||
with more liberal key rules. Refs #6447.
|
||||
|
||||
"""
|
||||
# On Python 2.6+ we could use the catch_warnings context
|
||||
# manager to test this warning nicely. Since we can't do that
|
||||
# yet, the cleanest option is to temporarily ask for
|
||||
# CacheKeyWarning to be raised as an exception.
|
||||
warnings.simplefilter("error", CacheKeyWarning)
|
||||
|
||||
# memcached does not allow whitespace or control characters in keys
|
||||
self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value')
|
||||
# memcached limits key length to 250
|
||||
self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value')
|
||||
|
||||
# The warnings module has no public API for getting the
|
||||
# current list of warning filters, so we can't save that off
|
||||
# and reset to the previous value, we have to globally reset
|
||||
# it. The effect will be the same, as long as the Django test
|
||||
# runner doesn't add any global warning filters (it currently
|
||||
# does not).
|
||||
warnings.resetwarnings()
|
||||
|
||||
class DBCacheTests(unittest.TestCase, BaseCacheTests):
|
||||
def setUp(self):
|
||||
# Spaces are used in the table name to ensure quoting/escaping is working
|
||||
|
@ -397,6 +425,22 @@ if settings.CACHE_BACKEND.startswith('memcached://'):
|
|||
def setUp(self):
|
||||
self.cache = get_cache(settings.CACHE_BACKEND)
|
||||
|
||||
def test_invalid_keys(self):
|
||||
"""
|
||||
On memcached, we don't introduce a duplicate key validation
|
||||
step (for speed reasons), we just let the memcached API
|
||||
library raise its own exception on bad keys. Refs #6447.
|
||||
|
||||
In order to be memcached-API-library agnostic, we only assert
|
||||
that a generic exception of some kind is raised.
|
||||
|
||||
"""
|
||||
# memcached does not allow whitespace or control characters in keys
|
||||
self.assertRaises(Exception, self.cache.set, 'key with spaces', 'value')
|
||||
# memcached limits key length to 250
|
||||
self.assertRaises(Exception, self.cache.set, 'a' * 251, 'value')
|
||||
|
||||
|
||||
class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
|
||||
"""
|
||||
Specific test cases for the file-based cache.
|
||||
|
@ -429,6 +473,22 @@ class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
|
|||
def test_cull(self):
|
||||
self.perform_cull_test(50, 28)
|
||||
|
||||
class CustomCacheKeyValidationTests(unittest.TestCase):
|
||||
"""
|
||||
Tests for the ability to mixin a custom ``validate_key`` method to
|
||||
a custom cache backend that otherwise inherits from a builtin
|
||||
backend, and override the default key validation. Refs #6447.
|
||||
|
||||
"""
|
||||
def test_custom_key_validation(self):
|
||||
cache = get_cache('regressiontests.cache.liberal_backend://')
|
||||
|
||||
# this key is both longer than 250 characters, and has spaces
|
||||
key = 'some key with spaces' * 15
|
||||
val = 'a value'
|
||||
cache.set(key, val)
|
||||
self.assertEqual(cache.get(key), val)
|
||||
|
||||
class CacheUtils(unittest.TestCase):
|
||||
"""TestCase for django.utils.cache functions."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue