[SQL-CVS] r1242 - in SQLObject/trunk/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-11-12 20:36:05
|
Author: ianb
Date: 2005-11-12 20:35:54 +0000 (Sat, 12 Nov 2005)
New Revision: 1242
Modified:
SQLObject/trunk/sqlobject/cache.py
SQLObject/trunk/sqlobject/dbconnection.py
SQLObject/trunk/sqlobject/main.py
SQLObject/trunk/sqlobject/tests/test_cache.py
Log:
Added .expireAll() methods to sqlmeta and connection objects, to expire all instances in those cases. Also added id key to asDict results
Modified: SQLObject/trunk/sqlobject/cache.py
===================================================================
--- SQLObject/trunk/sqlobject/cache.py 2005-11-11 23:06:02 UTC (rev 1241)
+++ SQLObject/trunk/sqlobject/cache.py 2005-11-12 20:35:54 UTC (rev 1242)
@@ -243,6 +243,19 @@
all.append(id)
return all
+ def getAll(self):
+ """
+ Return all the objects in the cache.
+ """
+ if self.doCache:
+ all = self.cache.values()
+ else:
+ all = []
+ for value in self.expiredCache.values():
+ if value():
+ all.append(value())
+ return all
+
class CacheSet(object):
"""
@@ -305,3 +318,31 @@
def allSubCaches(self):
return self.caches.values()
+
+ def weakrefAll(self, cls=None):
+ """
+ Move all objects in the cls (or if not given, then in all
+ classes) to the weakref dictionary, where they can be
+ collected.
+ """
+ if cls is None:
+ for cache in self.caches.values():
+ cache.expireAll()
+ elif self.caches.has_key(cls.__name__):
+ self.caches[cls.__name__].expireAll()
+
+ def getAll(self, cls=None):
+ """
+ Returns all instances in the cache for the given class or all
+ classes.
+ """
+ if cls is None:
+ results = []
+ for cache in self.caches.values():
+ results.extend(cache.getAll())
+ return results
+ elif cls.__name__ in self.caches:
+ return self.caches[cls.__name__].getAll()
+ else:
+ return []
+
Modified: SQLObject/trunk/sqlobject/dbconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/dbconnection.py 2005-11-11 23:06:02 UTC (rev 1241)
+++ SQLObject/trunk/sqlobject/dbconnection.py 2005-11-12 20:35:54 UTC (rev 1242)
@@ -138,6 +138,15 @@
% (self, name, soClass))
setattr(self, name, ConnWrapper(soClass, self))
+ def expireAll(self):
+ """
+ Expire all instances of objects for this connection.
+ """
+ cache_set = self.cache
+ cache_set.weakrefAll()
+ for item in cache_set.getAll():
+ item.expire()
+
class ConnWrapper(object):
"""
Modified: SQLObject/trunk/sqlobject/main.py
===================================================================
--- SQLObject/trunk/sqlobject/main.py 2005-11-11 23:06:02 UTC (rev 1241)
+++ SQLObject/trunk/sqlobject/main.py 2005-11-12 20:35:54 UTC (rev 1242)
@@ -587,11 +587,28 @@
########################################
def asDict(self):
+ """
+ Return the object as a dictionary of columns to values.
+ """
result = {}
for key in self.columns:
result[key] = getattr(self.instance, key)
+ result['id'] = self.instance.id
return result
+ def expireAll(sqlmeta, connection=None):
+ """
+ Expire all instances of this class.
+ """
+ soClass = sqlmeta.soClass
+ connection = connection or soClass._connection
+ cache_set = connection.cache
+ cache_set.weakrefAll(soClass)
+ for item in cache_set.getAll(soClass):
+ item.expire()
+
+ expireAll = classmethod(expireAll)
+
sqlhub = dbconnection.ConnectionHub()
class _sqlmeta_attr(object):
Modified: SQLObject/trunk/sqlobject/tests/test_cache.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_cache.py 2005-11-11 23:06:02 UTC (rev 1241)
+++ SQLObject/trunk/sqlobject/tests/test_cache.py 2005-11-12 20:35:54 UTC (rev 1242)
@@ -1,3 +1,5 @@
+from sqlobject import *
+from dbtest import *
from sqlobject.cache import CacheSet
class Something(object):
@@ -19,3 +21,28 @@
j = x.get(1, y.__class__)
assert j == None
x.finishPut(y.__class__)
+
+
+class CacheTest(SQLObject):
+ name = StringCol(alternateID=True)
+
+def test_cache():
+ setupClass(CacheTest)
+ s = CacheTest(name='foo')
+ obj_id = id(s)
+ s_id = s.id
+ assert CacheTest.get(s_id) is s
+ assert not s.sqlmeta.expired
+ CacheTest.sqlmeta.expireAll()
+ assert s.sqlmeta.expired
+ del s
+ CacheTest.sqlmeta.expireAll()
+ s = CacheTest.get(s_id)
+ # We should have a new object:
+ assert id(s) != obj_id
+ obj_id2 = id(s)
+ del s
+ CacheTest._connection.expireAll()
+ s = CacheTest.get(s_id)
+ assert id(s) != obj_id and id(s) != obj_id2
+
|