1

With Pyramid / pyramid_tm I end up passing around the dbsession object that joined the current transaction to methods that encapsulate my code. For example

def get_item_name(dbsession, item_id):
    item = dbsession.query(...)
    return item.name

def view_func(request):
    item_name = get_item_name(request.dbsession, request.GET['item_id']
    ...

Now if I wanted to cache the results of get_item_name using the beaker cache decorator @cache_region for example, I would never get a cache hit since dbsession is part of the cache key.

What is a common pattern to solve this elegantly?

barbaz
  • 1,642
  • 2
  • 17
  • 27
  • Since pyramid, I am using dogpile cache for caching. I am pretty sure it has some mechanism to ignore the first argument when calculating the key, but I cannot find any documentation :-( – Petr Blahos May 05 '21 at 06:26

1 Answers1

1

At least one pattern is to decorate a closure that only accepts the args you want to cache and accesses other variables like request/dbsession from nonlocal scope.

def view_func(request):
    @cache.region('short_term')
    def get_item_name(item_id):
        item = request.dbsession.query(...)
        return item.name
    item_name = get_item_name(request.GET['item_id'])

This pattern is common in the beaker docs.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70