Fixed cache behavior so we don't touch the cache within Database.save() if
there's no commit flag. Then, when Database.commit() is called, any pending saves are put into the cache.
This commit is contained in:
parent
6dcad328b0
commit
7a0368b5d9
|
@ -7,6 +7,15 @@ import random
|
||||||
from model.Persistent import Persistent
|
from model.Persistent import Persistent
|
||||||
|
|
||||||
|
|
||||||
|
class Connection_wrapper( object ):
|
||||||
|
def __init__( self, connection ):
|
||||||
|
self.connection = connection
|
||||||
|
self.pending_saves = []
|
||||||
|
|
||||||
|
def __getattr__( self, name ):
|
||||||
|
return getattr( self.connection, name )
|
||||||
|
|
||||||
|
|
||||||
class Database( object ):
|
class Database( object ):
|
||||||
ID_BITS = 128 # number of bits within an id
|
ID_BITS = 128 # number of bits within an id
|
||||||
ID_DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz"
|
ID_DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||||
|
@ -26,6 +35,15 @@ class Database( object ):
|
||||||
# makes SQLite angry.
|
# makes SQLite angry.
|
||||||
os.putenv( "PGTZ", "UTC" )
|
os.putenv( "PGTZ", "UTC" )
|
||||||
|
|
||||||
|
# forcibly replace psycopg's connect() function with another function that returns the psycopg
|
||||||
|
# connection wrapped in a class with a pending_saves member, used in save() and commit() below
|
||||||
|
original_connect = psycopg.connect
|
||||||
|
|
||||||
|
def connect( *args, **kwargs ):
|
||||||
|
return Connection_wrapper( original_connect( *args, **kwargs ) )
|
||||||
|
|
||||||
|
psycopg.connect = connect
|
||||||
|
|
||||||
if connection:
|
if connection:
|
||||||
self.__connection = connection
|
self.__connection = connection
|
||||||
self.__pool = None
|
self.__pool = None
|
||||||
|
@ -72,15 +90,21 @@ class Database( object ):
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
if self.__cache:
|
||||||
# FIXME: we shouldn't touch the cache unless there's actually a commit.
|
self.__cache.set( obj.cache_key, obj )
|
||||||
# the problem is that in self.commit() below, we don't know which objects
|
else:
|
||||||
# to actually save into the cache
|
# no commit yet, so don't touch the cache
|
||||||
if self.__cache:
|
connection.pending_saves.append( obj )
|
||||||
self.__cache.set( obj.cache_key, obj )
|
|
||||||
|
|
||||||
def commit( self ):
|
def commit( self ):
|
||||||
self.__get_connection().commit()
|
connection = self.__get_connection()
|
||||||
|
connection.commit()
|
||||||
|
|
||||||
|
# save any pending saves to the cache
|
||||||
|
for obj in connection.pending_saves:
|
||||||
|
self.__cache.set( obj.cache_key, obj )
|
||||||
|
|
||||||
|
connection.pending_saves = []
|
||||||
|
|
||||||
def load( self, Object_type, object_id, revision = None ):
|
def load( self, Object_type, object_id, revision = None ):
|
||||||
"""
|
"""
|
||||||
|
|
Reference in New Issue