Ejemplo n.º 1
0
    def call(self, callee, callargs, policy, expiration=None, ondefer=None):
        """
        invokes the callee with callargs through the cache, with the
        given cache policy.  This is the main user-space method.  It
        returns a CacheEntry instance.  The entry will only be
        guaranteed to have storage if policy.store is true.
        """
        # the canonical name and cache key are needed both for
        # retrieve and store; just get them once
        debug("policy is %r", policy)
        if expiration is not None:
            expiration = time_convert(expiration)

        args, kwargs = self._unpack_callargs(callargs)

        if policy.defer and ondefer is None:
            warnings.warn(
                "cache policy DEFER in use without a deferral callback")

        if policy.retrieve or policy.store:
            cn = self.getCanonicalName(callee)
            ck = self.getCacheKey((args, kwargs), False)

        if policy.retrieve:
            # has the callee changed since last checked?
            self.validateCallable(callee, cn)

            # get the cache entry if it exists
            entry = self._retrieve(cn, ck)
            if entry and policy.accept(entry):
                entry.retrieved = time()
                if policy.defer and ondefer:
                    ondefer(callee, callargs, expiration)
                return entry

        if policy.calculate or policy.defer:
            val = callee(*args, **kwargs)
            # the callee has a chance to determine the expiration by
            # sending it out of band, through an "expiration"
            # attribute.  otherwise, value passed to this method will
            # be used.

            now = time()
            expiration = getattr(callee, 'expiration', expiration) or 0
            if expiration:
                expiration = time_convert(expiration)
            entry = CacheEntry(val,
                               created=now,
                               retrieved=now,
                               expiration=expiration)

        else:
            raise NotInCache, (callee, callargs)

        if policy.store:
            self._store(entry, cn, ck)
        return entry
Ejemplo n.º 2
0
 def _store(self, entry, canonicalName, cacheKey):
     entry.stored=time.time()
     p=self._path_for_name_and_key(canonicalName, cacheKey)
     debug("cache path is %r", p)
     dname=dirname(p)
     try:
         os.makedirs(dname)
     except OSError, e:
         if e.errno!=errno.EEXIST:
             raise
Ejemplo n.º 3
0
 def _store(self, entry, canonicalName, cacheKey):
     entry.stored = time.time()
     p = self._path_for_name_and_key(canonicalName, cacheKey)
     debug("cache path is %r", p)
     dname = dirname(p)
     try:
         os.makedirs(dname)
     except OSError, e:
         if e.errno != errno.EEXIST:
             raise
Ejemplo n.º 4
0
 def _store(self, entry, canonicalName, cacheKey):
     entry.stored=time.time()
     p=self._path_for_name_and_key(canonicalName, cacheKey)
     debug("cache path is %r", p)
     dname=dirname(p)
     try:
         os.makedirs(dname)
     except OSError, e:
         if e.errno!=errno.EEXIST:
             if self.safe:
                 exception("error trying to create directory %s", dname)
             else:
                 raise
Ejemplo n.º 5
0
    def call(self,
             callee,
             callargs,
             policy,
             expiration=None,
             ondefer=None):
        """
        invokes the callee with callargs through the cache, with the
        given cache policy.  This is the main user-space method.  It
        returns a CacheEntry instance.  The entry will only be
        guaranteed to have storage if policy.store is true.
        """
        # the canonical name and cache key are needed both for
        # retrieve and store; just get them once
        debug("policy is %r", policy)
        if expiration is not None:
            expiration=time_convert(expiration)

        args, kwargs=self._unpack_callargs(callargs)

        if policy.defer and ondefer is None:
            warnings.warn("cache policy DEFER in use without a deferral callback")
        
        if policy.retrieve or policy.store:
            cn=self.getCanonicalName(callee)
            ck=self.getCacheKey((args, kwargs), False)            
        
        if policy.retrieve:
            # has the callee changed since last checked?
            self.validateCallable(callee, cn)
            
            # get the cache entry if it exists
            entry=self._retrieve(cn, ck)
            if entry and policy.accept(entry):
                entry.retrieved=time()
                if policy.defer and ondefer:
                    ondefer(callee, callargs, expiration)
                return entry

        if policy.calculate or policy.defer:
            val=callee(*args, **kwargs)
            # the callee has a chance to determine the expiration by
            # sending it out of band, through an "expiration"
            # attribute.  otherwise, value passed to this method will
            # be used.
            
            now=time()
            expiration=getattr(callee, 'expiration', expiration) or 0
            if expiration:
                expiration=time_convert(expiration)
            entry=CacheEntry(val,
                             created=now,
                             retrieved=now,
                             expiration=expiration)
            

                
        else:
            raise NotInCache, (callee, callargs)

        if policy.store:
            self._store(entry, cn, ck)
        return entry