Author: phd
Date: 2006-01-31 08:43:56 -0700 (Tue, 31 Jan 2006)
New Revision: 1555
Modified:
home/phd/SQLObject/paramstyles/sqlobject/cache.py
home/phd/SQLObject/paramstyles/sqlobject/tests/test_cache.py
Log:
Merged patches from the revisions 1553:1554 from the trunk: applied patch 1414091: Fix cache cleanup for new object creation.
Modified: home/phd/SQLObject/paramstyles/sqlobject/cache.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/cache.py 2006-01-31 15:43:08 UTC (rev 1554)
+++ home/phd/SQLObject/paramstyles/sqlobject/cache.py 2006-01-31 15:43:56 UTC (rev 1555)
@@ -43,7 +43,7 @@
"""
self.cullFrequency = cullFrequency
- self.cullCount = cullFrequency
+ self.cullCount = 0
self.cullOffset = 0
self.cullFraction = cullFraction
self.doCache = cache
@@ -97,6 +97,8 @@
# method has a lock, so it's threadsafe.
self.cullCount = 0
self.cull()
+ else:
+ self.cullCount = self.cullCount + 1
try:
return self.cache[id]
@@ -166,6 +168,15 @@
of this situation.
"""
if self.doCache:
+ if self.cullCount > self.cullFrequency:
+ # Two threads could hit the cull in a row, but
+ # that's not so bad. At least by setting cullCount
+ # back to zero right away we avoid this. The cull
+ # method has a lock, so it's threadsafe.
+ self.cullCount = 0
+ self.cull()
+ else:
+ self.cullCount = self.cullCount + 1
self.cache[id] = obj
else:
self.expiredCache[id] = ref(obj)
Modified: home/phd/SQLObject/paramstyles/sqlobject/tests/test_cache.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/tests/test_cache.py 2006-01-31 15:43:08 UTC (rev 1554)
+++ home/phd/SQLObject/paramstyles/sqlobject/tests/test_cache.py 2006-01-31 15:43:56 UTC (rev 1555)
@@ -45,4 +45,9 @@
CacheTest._connection.expireAll()
s = CacheTest.get(s_id)
assert id(s) != obj_id and id(s) != obj_id2
-
+
+def test_cache_cull():
+ setupClass(CacheTest)
+ s = CacheTest(name='test_cache_create')
+ list = [CacheTest(name='test_cache_create %d' % count) for count in range(s._connection.cache.caches['CacheTest'].cullFrequency)]
+ assert len(s._connection.cache.caches['CacheTest'].cache) < s._connection.cache.caches['CacheTest'].cullFrequency
|