On Tue, Sep 30, 2014 at 8:52 AM, tonthon <tontho...@gmail.com> wrote: > Hi, > > I'm using dogpile cache to store objects in a redis database, here's the > sample function I use for my tests : > >>>> @region.cache_on_arguments() >>>> def get_my_model(id): >>>> return DBSession().query(Model).get(id) > > I retrieve models : > >>>> model1 = get_my_model(5) >>>> model2 = get_my_model(5) > > model2 is retrieved from the cache. > Since it's not attached to any session, when accessing a lazy-related > object : > >>>> model2.owner > > I get a a DetachedInstanceError > I found a turnaround : > >>>> DBSession().enable_relationship_loading(model2) >>>> model2.owner > > that allows me to do what I want, but I'm not really satisfied with this > design. > > Is it a clean way to go or should I review my caching strategy ? >
The normal approach is to use session.merge() to attach a cached object to a session. Note that session.merge() actually returns a *new instance* attached to the session - it doesn't attach the instance that you passed in: http://docs.sqlalchemy.org/en/latest/orm/session.html#merging There's also a sophisticated example of using dogpile.cache at: http://docs.sqlalchemy.org/en/latest/orm/examples.html#examples-caching Hope that helps, Simon -- 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.