[jToolkit-cvs] jToolkit/data database.py,1.5,1.6
Brought to you by:
davidfraser,
friedelwolff
From: <dav...@us...> - 2004-02-09 12:23:20
|
Update of /cvsroot/jtoolkit/jToolkit/data In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19649 Modified Files: database.py Log Message: added code to create separate caches for separate tables (not thoroughly tested yet!) Index: database.py =================================================================== RCS file: /cvsroot/jtoolkit/jToolkit/data/database.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** database.py 9 Feb 2004 12:17:31 -0000 1.5 --- database.py 9 Feb 2004 12:19:59 -0000 1.6 *************** *** 83,87 **** class dbwrapper: ! def __init__(self, instance, errorhandler=None): """initialises the dbwrapper class...""" if errorhandler is None: --- 83,87 ---- class dbwrapper: ! def __init__(self, instance, errorhandler=None, cachetables=None): """initialises the dbwrapper class...""" if errorhandler is None: *************** *** 103,113 **** self.lower = lower self.dbtypenames = dbtypenames[self.DBTYPE] ! self.singlerowcache = timecache.timecache(120) ! self.allrowscache = timecache.timecache(170) ! def clearcache(self): ! # TODO: purge only part of cache based on table name... ! self.singlerowcache.clear() ! self.allrowscache.clear() def importdriver(self): --- 103,139 ---- self.lower = lower self.dbtypenames = dbtypenames[self.DBTYPE] ! # set up caches ! self.cachetables = [] ! self.singlerowcaches = cidict.cidict({'': timecache.timecache(120)}) ! self.allrowscaches = cidict.cidict({'': timecache.timecache(170)}) ! if cachetables is not None: ! for cachetable, expirytime in self.cachetables: ! self.cachetables.append(cachetable) ! self.singlerowcaches[cachetable] = timecache.timecache(expirytime) ! self.allrowscaches[cachetable] = timecache.timecache(expirytime) ! def clearcache(self, cachetables): ! """clears all the caches for the given sql command""" ! if len(cachetables) == 0: ! self.singlerowcaches[''].clear() ! self.allrowscaches[''].clear() ! else: ! for cachetable in cachetables: ! # TODO: handle ripple-through effects... ! if not cachetable in self.cachetables: ! self.singlerowcaches[''].clear() ! self.allrowscaches[''].clear() ! else: ! self.singlerowcaches[cachetable].clear() ! self.allrowscaches[cachetable].clear() ! ! def getcachetables(self, sql): ! """returns the list of cacheable tables that occur in the sql statement""" ! cachetables = [] ! sql = sql.lower() ! for cachetable in self.cachetables: ! if sql.find(cachetable.lower()) != -1: ! cachetables.append(cachetable) ! return cachetables def importdriver(self): *************** *** 151,155 **** cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache() self.db.commit() return cursor --- 177,181 ---- cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache(self.getcachetables(sql)) self.db.commit() return cursor *************** *** 164,167 **** --- 190,194 ---- try: cursor = self.db.cursor() + self.errorhandler.logtrace("uncacheable sql query: " + str(sql)) cursor.execute(sql) self.db.commit() *************** *** 251,255 **** cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache() self.db.commit() except self.dberror: --- 278,282 ---- cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache([tablename]) self.db.commit() except self.dberror: *************** *** 296,300 **** cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache() self.db.commit() except self.dberror: --- 323,327 ---- cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache([tablename]) self.db.commit() except self.dberror: *************** *** 311,315 **** cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache() self.db.commit() except self.dberror: --- 338,342 ---- cursor = self.db.cursor() cursor.execute(sql) ! self.clearcache([tablename]) self.db.commit() except self.dberror: *************** *** 343,349 **** print def singlerow(self, sql, withdescription=0): """runs sql and retrieves a single row""" ! cachedrow, description = self.singlerowcache.get(sql, (None, None)) if cachedrow is not None: if withdescription: --- 370,400 ---- print + def getsinglerowcache(self, sql): + """returns cached single row result for the sql statement""" + cachetables = self.getcachetables(sql) + if len(cachetables) == 0: + cachedrow, description = self.singlerowcaches[''].get(sql, (None, None)) + elif len(cachetables) == 1: + cachedrow, description = self.singlerowcaches.get(cachetables[0], self.singlerowcaches['']).get(sql, (None, None)) + else: + # TODO: check that it's the same for all of them + cachedrow, description = self.singlerowcaches.get(cachetables[0], self.singlerowcaches['']).get(sql, (None, None)) + return cachedrow, description + + def setsinglerowcache(self, sql, cachedrow, description): + """caches single row result for the sql statement""" + cachetables = self.getcachetables(sql) + if len(cachetables) == 0: + self.singlerowcaches[''][sql] = (cachedrow, description) + else: + for cachetable in cachetables: + if cachetable in self.cachetables: + self.singlerowcaches[cachetables][sql] = (cachedrow, description) + else: + self.singlerowcaches[''][sql] = (cachedrow, description) + def singlerow(self, sql, withdescription=0): """runs sql and retrieves a single row""" ! cachedrow, description = self.getsinglerowcache(sql) if cachedrow is not None: if withdescription: *************** *** 355,359 **** # return None for each field row = [None] * len(cursor.description) ! self.singlerowcache[sql] = row, cursor.description if withdescription: return row, cursor.description --- 406,410 ---- # return None for each field row = [None] * len(cursor.description) ! self.setsinglerowcache(sql, row, cursor.description) if withdescription: return row, cursor.description *************** *** 365,371 **** return row[0] def allrows(self, sql, withdescription=0): """runs sql and retrieves all rows""" ! cachedrows, description = self.allrowscache.get(sql, (None, None)) if cachedrows is not None: if withdescription: --- 416,446 ---- return row[0] + def getallrowscache(self, sql): + """returns cached all rows result for the sql statement""" + cachetables = self.getcachetables(sql) + if len(cachetables) == 0: + cachedrows, description = self.allrowscaches[''].get(sql, (None, None)) + elif len(cachetables) == 1: + cachedrows, description = self.allrowscaches.get(cachetables[0], self.allrowscaches['']).get(sql, (None, None)) + else: + # TODO: check that it's the same for all of them + cachedrows, description = self.allrowscaches.get(cachetables[0], self.allrowscaches['']).get(sql, (None, None)) + return cachedrows, description + + def setallrowscache(self, sql, cachedrows, description): + """caches all rows result for the sql statement""" + cachetables = self.getcachetables(sql) + if len(cachetables) == 0: + self.allrowscaches[''][sql] = (cachedrows, description) + else: + for cachetable in cachetables: + if cachetable in self.cachetables: + self.allrowscaches[cachetables][sql] = (cachedrows, description) + else: + self.allrowscaches[''][sql] = (cachedrows, description) + def allrows(self, sql, withdescription=0): """runs sql and retrieves all rows""" ! cachedrows, description = self.getallrowscache(sql) if cachedrows is not None: if withdescription: *************** *** 374,378 **** cursor = self.query(sql) rows = cursor.fetchall() ! self.allrowscache[sql] = rows, cursor.description if withdescription: return rows, cursor.description --- 449,453 ---- cursor = self.query(sql) rows = cursor.fetchall() ! self.setallrowscache(sql, rows, cursor.description) if withdescription: return rows, cursor.description |