Beispiel #1
0
 def after_insert(self, mapper, connection, instance):
     cache = get_cache()
     expires = self.get_expires(instance, "create")
     for expire in expires:
         md5key, key = self.get_key_from_expires(instance, expire)
         logging.debug("Invalidando chave[%s] no cache on insert [%s]" % (key, instance))
         cache.delete(md5key)
     return EXT_CONTINUE
Beispiel #2
0
    def after_update(self, mapper, connection, instance):
        if not Session.object_session(instance).is_modified(instance, include_collections=False, passive=True): return EXT_STOP
        
        cache = get_cache()
        expires = self.get_expires(instance, "update")
        for expire in expires:
            md5key, key = self.get_key_from_expires(instance, expire)
            logging.debug("Invalidando chave[%s] no cache on update [%s]" % (key,instance))
            cache.delete(md5key)

        # espira a instancia
        md5key, key = self.get_key_from_mapper(mapper,instance)
        logging.debug("Invalidando chave[%s] no cache on update [%s]" % (key,instance))
        cache.delete(md5key)
        
        return EXT_CONTINUE
Beispiel #3
0
    def get(self, ident, **kw):
    
        mapper = self._mapper_zero()
        session = self.session
        cache = get_cache()
        
#        logging.debug("%s.................................................." % datetime.now().strftime("%H:%M:%S:%f"))
        
        try:
            ident = long(ident)
        except TypeError:
            if type(ident) in (tuple, list):
                ident = long(ident[0])
                            
        key = mapper.identity_key_from_primary_key([ident])
        
        cacheobj = session.identity_map.get(key)
        
        if cacheobj and hasattr(cacheobj, "__no_session__") and cacheobj.__no_session__:
            session.expunge(cacheobj)
            cacheobj = None

        cache_key, keystr = CachedQuery.generate_key(key[0], ident)

        if not cacheobj:

            if not (hasattr(key[0], "__no_cache__") and key[0].__no_cache__):
                cacheobj = cache.get(cache_key)

            if cacheobj is not None:
                logging.debug("CachedQuery [CACHE] -> recuperando do cache e setando na sessao")
                cacheobj.__dict__["_sa_instance_state"] = attributes.instance_state(cacheobj)
                session.add(cacheobj)
                    
            else:
                logging.debug("CachedQuery [BANCO] -> nao existe no cache, pega do banco %s" % keystr)
                cacheobj = super(CachedQuery, self).get(ident)
                if cacheobj is None: 
                    return None 
                logging.debug("CachedQuery [CACHE] -> setando no cache %s" % cacheobj)
                cache.set(cache_key, cacheobj)
        else:
            logging.debug("CachedQuery [SESSION] -> recuperando da sessao ")

#        logging.debug("%s.................................................." % datetime.now().strftime("%H:%M:%S:%f"))
        return cacheobj