On 30 Sep 2014, at 23:18, tonthon <tontho...@gmail.com> wrote:
> I didn't knew merge was supposes to be used in such a case, it works like a 
> charm.
> Following that tip, I've added this decorator :
> 
> >>> def cache_wrapper(func):                   
> >>>     """ ensure a model returned from a cached function is attached to the 
> >>> current session """                                     
> >>>     def cached_func_wrapper(*args, **kwargs):                             
> >>>       
> >>>         obj = func(*args, **kwargs)                                       
> >>>       
> >>>         if object_session(obj) is None and has_identity(obj):             
> >>>       
> >>>             obj = DBSESSION().merge(obj)                                  
> >>>       
> >>>         return obj                                                        
> >>>       
> >>>     return cached_func_wrapper  

Keep in mind that merge can still hit your SQL database to refresh and 
attributes. If you know your cache is not stale you will want to use the 
load=False parameter for merge() to prevent that from happening.

Wichert.

-- 
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.

Reply via email to