[ckan-changes] [ckan/ckan] a359bc: Stops Package.get avoiding the cache

GitHub noreply at github.com
Wed Jan 6 21:06:10 UTC 2016


  Branch: refs/heads/2823-use-sqlalchemy-cache-for-packages
  Home:   https://github.com/ckan/ckan
  Commit: a359bce80067569e4f2d0ca7b7c4e00665e11460
      https://github.com/ckan/ckan/commit/a359bce80067569e4f2d0ca7b7c4e00665e11460
  Author: Ross Jones <ross at servercode.co.uk>
  Date:   2016-01-06 (Wed, 06 Jan 2016)

  Changed paths:
    M ckan/model/package.py

  Log Message:
  -----------
  Stops Package.get avoiding the cache

SQLAlchemy has a session cache containing objects that it has retrieved
recently, which are stored in an identity map (pk->object). If you call
session.query(cls).get(pk) the object will be retrieved from the cache
(identity map), if you call session.query(cls).filter() then Sqlalchemy
has to query the database again.

In Package.get() the query to find by id (before looking by name) uses
filter and so will always bypass the session cache. Once upon a time it
used session.query(cls).get(reference), but this was lost in a change
(where eager loading was added, then removed in a later commit).

For a contrived, and probably sub-optimal example ...

for pkg in some_query_that_fetches_packages():
    pkg_dict = logic.get_action('package_show')(context, {'id': pkg.id})

In these cases package_show is issuing another query to the
database, even though the package object is in the session cache
ready for retrieval.

There is more information on how the Session cache works at
http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#is-the-session-a-cache

This should fix #2823 although there are likely other places this could
be done (e.g. group/user)




More information about the ckan-changes mailing list