How to stop repeating myself

2 views
Skip to first unread message

Jacob Lyles

unread,
Nov 23, 2009, 8:03:18 PM11/23/09
to web.py
Hello,

I'm building a small application using web.py with sqlAlchemy. My
application has users. When a user successfully authenticates, I store
his user ID in the session.user_id variable. Then I query the database
every time I want to render a template using the user's information.
However, this seems wasteful. I've already queried the DB once and
retrieved a "User" sqlalchemy object instance, is there any place I
can store this object without having to recreate it every time I GET a
new page? I tried storing this in a session.user_data variable, but
that caused errors.

Also, every time I query the database I am creating another sqlAlchemy
DB engine and another DB session. Is there a way to share a DB engine
across classes?

So, for instance, I have an URL called '/map' that is linked off the
home page. In the GET method of the MAP class, I am creating a new
sqlAlchemy engine, connecting to it to query for user data, doing the
same for other data, etc. It is the same for all my other URLs.


Thanks,
Jacob

Dan Loewenherz

unread,
Nov 24, 2009, 12:37:59 AM11/24/09
to we...@googlegroups.com
You should consider using a fast key-value store to store pickled user information objects. Memcached for one has lots of python libraries. Query the store and do something like `user_info=pickle.loads(value)` whenever a page is loaded. That should be a lot quicker than instantiating an ORM each request.

-Dan
Reply all
Reply to author
Forward
0 new messages