If you do it properly, you can .get(primary_key) the user from the 
SQLAlchemy session and it only hits the storage once per transaction. If the 
object hasn't been garbage collected, subsequent .get() calls will hit the 
identity map, which can be used as a kind of cache but which is really there 
to make sure your code has a consistent idea of what objects look like 
during a transaction.

The following will execute 100 queries in CPython:

Session = sessionmaker(create_engine('sqlite:///:memory:', echo=True), 
weak_identity_map=True) # default
for i in range(100): # weak referenced user will be gc'd and re-queried
    session.query(stucco_auth.tables.User).get(1)

One query, since we hold a reference to the user 'u':

Session = sessionmaker(create_engine('sqlite:///:memory:', echo=True), 
weak_identity_map=True) # default
for i in range(100): # 'u' keeps user alive
    u = session.query(stucco_auth.tables.User).get(1)

One query, since the session holds on to its objects:

Session = sessionmaker(create_engine('sqlite:///:memory:', echo=True), 
weak_identity_map=False)
for i in range(100): # weak referenced user will be gc'd and re-queried
    session.query(stucco_auth.tables.User).get(1)

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to