Skip to content

Commit c904e55

Browse files
committed
version bump and small fix, defaulting unknown version to zero instead of None
1 parent ce6ce5b commit c904e55

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def listify(filename):
55

66
setup(
77
name = "django-cache-sweeper",
8-
version = "0.1.1",
8+
version = "0.1.2",
99
url = 'http://github.com/smn/django-cache-sweeper',
1010
license = 'BSD',
1111
description = "Lazy Django fragment cache sweeping",

src/cachesweeper/tests.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ def __init__(self, *args, **kwargs):
1818
super(FragmentCacheInvalidation, self).__init__(*args, **kwargs)
1919

2020
def setUp(self):
21-
pass
21+
cache.clear()
2222

2323
def tearDown(self):
2424
pass
2525

2626
def test_version_at_creation(self):
2727
comment = Comment.objects.latest()
28+
comment.like_it()
2829
version_cache_key = cache_token_key_for_record(comment)
2930
# the cache version token should be zero as we've just created the record
3031
# by loading the fixtures. At creation the cache version should be
3132
# zero
32-
self.assertTrue(cache.get(version_cache_key))
33+
self.assertEquals(cache.get(version_cache_key), 0)
3334

3435
def test_version_after_save(self):
3536
# get the comment we want to invalidate the cache for
@@ -38,11 +39,9 @@ def test_version_after_save(self):
3839
version_cache_key = cache_token_key_for_record(comment)
3940
# get the original version, should be zero
4041
original_version = cache.get(version_cache_key, None)
41-
self.assertNotEquals(original_version, None)
4242
# change the comment & save, should increment the version value in
4343
# memcached
4444
comment.like_it()
45-
comment.save()
4645
# get the new version value for the comment
4746
new_version = cache.get(version_cache_key)
4847
self.assertNotEquals(original_version, new_version)
@@ -71,7 +70,6 @@ def test_fragment_cache_miss(self):
7170

7271
# modifying the model should change the cache
7372
comment.like_it()
74-
comment.save()
7573

7674
# assert the changed cache key
7775
new_cache_key = generate_fragment_cache_key_for_record(comment, "comment.xml")
@@ -89,7 +87,14 @@ def test_modelsweeper_mixin(self):
8987
tmm.save()
9088
self.assertEquals(tmm.cachesweeper_version, 1)
9189

92-
def test_modelsweeper_attr(self):
93-
tam = TestAttributeModel(text='testing text')
94-
tam.save()
95-
90+
def test_default_version_zero(self):
91+
tmm = TestMixinModel(text='testing text')
92+
tmm.save()
93+
cache.delete(tmm.cachesweeper_version_key)
94+
self.assertEquals(tmm.cachesweeper_version, 0)
95+
tmm.save()
96+
self.assertEquals(tmm.cachesweeper_version, 1)
97+
98+
# def test_modelsweeper_manager(self):
99+
# tmm = TestManagerModel(text='testing text')
100+
# self.assertTrue(hasattr(tmm.cachesweeper,'cachesweeper'))

src/cachesweeper/utils.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,34 @@ def update_cache_token_for_record_with_attribute(instance, token_attr):
5050
token_value = getattr(instance,token_attr)
5151
token_value_hash = md5_constructor(str(token_value)).hexdigest()
5252
cache.set(cache_key, token_value_hash, 0) # 0 = no time based expiry
53+
return token_value_hash
5354

5455
def update_cache_token_for_record_with_counter(instance):
5556
"""
5657
Update the cache token with an internal memcached counter.
5758
"""
5859
cache_key = cache_token_key_for_record(instance)
5960
value = cache.get(cache_key)
60-
if value == None or not isinstance(value, int):
61+
if value == None:
6162
cache.set(cache_key, 0, 0) # 0 = no time based expiry
63+
return 0
6264
else:
63-
cache.incr(cache_key)
65+
return cache.incr(cache_key)
6466

6567

6668

6769
def generate_fragment_cache_key_for_record(record, *cache_keys):
6870
unique_fragment_key = u":".join(map(lambda key: urlquote(str(key)), cache_keys))
6971
unique_fragment_key_hash = md5_constructor(unique_fragment_key)
7072
record_version_key = cache_token_key_for_record(record)
71-
record_current_version = cache.get(record_version_key)
73+
record_current_version = cache.get(record_version_key, None)
74+
75+
# cache miss for a record that hasn't been versioned yet or
76+
# has been expired by memcached
77+
if record_current_version == None:
78+
cache.set(record_version_key, 0, 0)
79+
record_current_version = 0
80+
7281
cache_key = 'cachesweeper.%s.%s.%s' % (
7382
record_version_key,
7483
record_current_version,
@@ -97,5 +106,9 @@ def cachesweeper_version_key(self):
97106

98107
@property
99108
def cachesweeper_version(self):
100-
return cache.get(self.cachesweeper_version_key)
109+
cached_version = cache.get(self.cachesweeper_version_key)
110+
if cached_version is not None:
111+
return cached_version
112+
else:
113+
return update_cache_token_for_record_with_counter(self)
101114

0 commit comments

Comments
 (0)