This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add cache_prefix setting (fixes #227) #680
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c88fd24
cache_prefix key added
lavish205 8a9278c
KeyError fixed
lavish205 9f019c5
test cases added and cache_prefix changes added to other backend
lavish205 0731de8
Add prefix to all cache backends.
dce740d
@lavish205 added in contributors.rst
lavish205 c0b0dcf
Test all backends.
ae68a93
@leplatrem review.
d811c18
Share the store between self.cache and backend_prefix for memory tests.
49d187e
changelog updated
lavish205 2cc1d53
changelog updated
lavish205 f83f657
Test prefixed_backend.get + fix MemoryCache expiracy for 0
c4341d3
Add documentation.
9df6b1d
Update changelog.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
|
||
class CacheBaseTest(unittest.TestCase): | ||
def setUp(self): | ||
self.cache = CacheBase() | ||
self.cache = CacheBase(cache_prefix='') | ||
|
||
def test_mandatory_overrides(self): | ||
calls = [ | ||
|
@@ -56,6 +56,16 @@ def tearDown(self): | |
super(BaseTestCache, self).tearDown() | ||
self.cache.flush() | ||
|
||
def get_backend_prefix(self, prefix): | ||
settings_prefix = self.settings.copy() | ||
settings_prefix['cache_prefix'] = prefix | ||
config_prefix = self._get_config(settings=settings_prefix) | ||
|
||
# initiating cache backend with prefix: | ||
backend_prefix = self.backend.load_from_config(config_prefix) | ||
|
||
return backend_prefix | ||
|
||
def test_backend_error_is_raised_anywhere(self): | ||
self.client_error_patcher.start() | ||
calls = [ | ||
|
@@ -143,9 +153,89 @@ def test_ttl_return_none_if_unknown(self): | |
ttl = self.cache.ttl('unknown') | ||
self.assertTrue(ttl < 0) | ||
|
||
def test_cache_prefix_is_set(self): | ||
backend_prefix = self.get_backend_prefix(prefix='prefix_') | ||
|
||
# Set the value | ||
backend_prefix.set('key', 'foo') | ||
|
||
# Validate that it was set with the prefix. | ||
obtained = self.cache.get('prefix_key') | ||
self.assertEqual(obtained, 'foo') | ||
|
||
def test_cache_when_prefix_is_not_set(self): | ||
backend_prefix = self.get_backend_prefix(prefix='') | ||
|
||
# Set a value | ||
backend_prefix.set('key', 'foo') | ||
|
||
# Validate that it was set with no prefix | ||
obtained = self.cache.get('key') | ||
self.assertEqual(obtained, 'foo') | ||
|
||
def test_prefix_value_use_to_get_data(self): | ||
backend_prefix = self.get_backend_prefix(prefix='prefix_') | ||
|
||
# Set the value with the prefix | ||
self.cache.set('prefix_key', 'foo') | ||
|
||
# Validate that the prefix was added | ||
obtained = backend_prefix.get('key') | ||
self.assertEqual(obtained, 'foo') | ||
|
||
def test_prefix_value_use_to_delete_data(self): | ||
backend_prefix = self.get_backend_prefix(prefix='prefix_') | ||
# Set the value | ||
self.cache.set('prefix_key', 'foo') | ||
|
||
# Delete the value | ||
backend_prefix.delete('key') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should try to get the key and make sure it has been deleted. |
||
|
||
# Validate that the value was deleted | ||
obtained = self.cache.get('prefix_key') | ||
self.assertEqual(obtained, None) | ||
|
||
def test_prefix_value_used_with_ttl(self): | ||
backend_prefix = self.get_backend_prefix(prefix='prefix_') | ||
|
||
self.cache.set('prefix_key', 'foo', 10) | ||
|
||
# Validate that the ttl add the prefix to the key. | ||
obtained = backend_prefix.ttl('key') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we should validate that redis is always called with the right key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Natim can you tell me how to validate that redis is always called with the right key? |
||
self.assertLessEqual(obtained, 10) | ||
self.assertGreater(obtained, 9) | ||
|
||
def test_prefix_value_used_with_expire(self): | ||
backend_prefix = self.get_backend_prefix(prefix='prefix_') | ||
|
||
self.cache.set('prefix_foobar', 'toto', 10) | ||
|
||
# expiring the ttl of key | ||
backend_prefix.expire('foobar', 0) | ||
|
||
# Make sure the TTL was set accordingly. | ||
ttl = self.cache.ttl('prefix_foobar') | ||
self.assertLessEqual(ttl, 0) | ||
|
||
# The record should have expired | ||
retrieved = self.cache.get('prefix_foobar') | ||
self.assertIsNone(retrieved) | ||
|
||
|
||
class MemoryCacheTest(BaseTestCache, unittest.TestCase): | ||
backend = memory_backend | ||
settings = { | ||
'cache_prefix': '' | ||
} | ||
|
||
def get_backend_prefix(self, prefix): | ||
backend_prefix = BaseTestCache.get_backend_prefix(self, prefix) | ||
|
||
# Share the store between both client for tests. | ||
backend_prefix._ttl = self.cache._ttl | ||
backend_prefix._store = self.cache._store | ||
|
||
return backend_prefix | ||
|
||
def test_backend_error_is_raised_anywhere(self): | ||
pass | ||
|
@@ -161,7 +251,8 @@ class RedisCacheTest(BaseTestCache, unittest.TestCase): | |
backend = redis_backend | ||
settings = { | ||
'cache_url': '', | ||
'cache_pool_size': 10 | ||
'cache_pool_size': 10, | ||
'cache_prefix': '' | ||
} | ||
|
||
def setUp(self): | ||
|
@@ -185,7 +276,8 @@ class PostgreSQLCacheTest(BaseTestCache, unittest.TestCase): | |
backend = postgresql_backend | ||
settings = { | ||
'cache_pool_size': 10, | ||
'cache_url': 'postgres://postgres:postgres@localhost:5432/testdb' | ||
'cache_url': 'postgres://postgres:postgres@localhost:5432/testdb', | ||
'cache_prefix': '' | ||
} | ||
|
||
def setUp(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do you check that redis was called with
prefix_key
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to use two instances, one with prefix and the other one without.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This idea is really good, but doesn't work with the MemoryBackend because they are not sharing the same store.