avoiding race conditions in dogpile with recursive caching

35 views
Skip to first unread message

Jonathan Vanasco

unread,
Oct 12, 2012, 8:47:12 PM10/12/12
to pylons-...@googlegroups.com
i'm using dogpile to manage caching, and ran into an issue with "assembled objects" - in which i (pre)compile a bunch of complex data into the cache on first view

an example of the problem i've run into is in the class below... ( it's a very simplified illustration )

class CachingApi(object):
    def __init__(self,request,dbSession):
        self.request = request
        self.dbSession = dbSession
        
    def _get_paintings_by_artist_id( self ):
        (artist_id) = self.query_args
        paintings = dbSession.query( model.Paintings ).filter( artist_id=artist_id ).all()
        return [ i.as_dict() for i in paintings ]
        
    def get_paintings_by_artist_id( self , artist_id ):
        self.query_args = (artist_id,)
        key = "get_paintings_by_artist_id:%s" % self.query_args
        return utils.regions['object'].get_or_create( key , self._get_paintings_by_artist_id )

    def _get_artist_by_id( self ):
        (artist_id) = self.query_args
        artist = dbSession.query( model.Artist ).filter( id=artist_id ).first()
        if artist :
            artist = artist.as_dict()
        paintings = self.get_paintings_by_artist_id(artist_id)
        stash = ( artist , paintings )
        return stash
        
    def get_artist_by_id( self , artist_id ):
        self.query_args = (artist_id,)
        key = "get_artist_by_id:%s" % self.query_args
        return utils.regions['object'].get_or_create( key , self._get_artist_by_id )

( artist , artist_paintings ) = CachingApi( request ,dbSession ).get_artist_by_id( artist_id )

the issue i've run into, is that the call to get_artist_by_id() creates a lock, which keeps get_paintings_by_artist_id() from running.

the workaround i've used is to have multiple caching regions - one for each type.

i'm wondering if anyone has ideas on how to restructure this so i don't have to do that - or can suggest better ways to handle this sort of "recursive" cache.  
Reply all
Reply to author
Forward
0 new messages