[sqlalchemy] Storing data on an instance that's deleted when the object is expired

2015-09-25 Thread Adrian
For some methods/properties on a model it might be useful to memoize its 
result.
There are some common decoratos such as cached_property from werkzeug which 
simply add the
value to the object's __dict__ after retrieving it the first time (thus not 
calling the property's getter again).
Or you might end up using custom memoization code that stores the value 
somewhere on the object.

Anyway, the problem with all those things is that I'm likely to cache 
things too long in case they depend
on a column/relationship value since expiring the object won't expire my 
cached data.

Is there any place where I could store custom data associated with an 
instance of a mapped object that
is cleared when the object is expired?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Storing data on an instance that's deleted when the object is expired

2015-09-25 Thread Mike Bayer



On 9/25/15 8:22 AM, Adrian wrote:
For some methods/properties on a model it might be useful to memoize 
its result.
There are some common decoratos such as cached_property from werkzeug 
which simply add the
value to the object's __dict__ after retrieving it the first time 
(thus not calling the property's getter again).
Or you might end up using custom memoization code that stores the 
value somewhere on the object.


Anyway, the problem with all those things is that I'm likely to cache 
things too long in case they depend
on a column/relationship value since expiring the object won't expire 
my cached data.


Is there any place where I could store custom data associated with an 
instance of a mapped object that

is cleared when the object is expired?
one difficulty with that is that the "expired' state isn't 
all-or-nothing in some cases; individual attributes can be expired which 
may or may not be part of your attribute's state.


for that reason you want to write an expire handler using the expire 
event: 
http://docs.sqlalchemy.org/en/rel_1_0/orm/events.html#sqlalchemy.orm.events.InstanceEvents.expire 
which will pop your __dict__-cached values when this is intercepted.
In the vast majority of cases this occurs for all attributes so it would 
be feasible to disregard checking the attrs collection.





--
You received this message because you are subscribed to the Google 
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to sqlalchemy+unsubscr...@googlegroups.com 
.
To post to this group, send email to sqlalchemy@googlegroups.com 
.

Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.