On 06/07/2010 12:34 AM, Victor Lin wrote:
> Hi,
>
> I use a cache on some pages of my TurboGears2 application,  and I
> encounter a problem.  There is a query in those page method, for
> example:
>
> @beaker_cache(expire=30, type='memory', query_args=True)
> @expose()
> def foo():
>     data = DBSession.query(Bar).all()
>     return dict(data=data)
>   

Your expose decorator suggests to me that you want whole-page caching,
in which case you usually want to cached the rendered output of the
controller instead of the query results. I think you will get this
effect if you added a template argument to the expose decorator or
manually render the output in your controller.

If you do want to cache just the query results, then you should first
expunge each object in the results before adding them to the cache. This
will ensure that they don't hold on to your original session. Then you
will want to merge them into the current session back after loading them
from the cache. Example:

@expose()
def foo():
    @beaker_cache(expire=30, type='memory', query_args=True)
    def real_foo():
        data = DBSession.query(Bar).all()
        for obj in data:
            DBSession.expunge(obj)
        return data

    data = real_foo()
    data = [DBSession.merge(obj, load=False) for obj in data]
    return dict(data=data)

You should also look at SQLAlchemy's beaker_caching example (available
in 0.6) for a more automatic way to do caching.

-Conor

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to