Exemplo 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
        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:
                    return ondefer(entry, callee, callargs, expiration)
                return entry

        if policy.calculate or policy.defer:
            # the callee can bypass the cache by raising a BypassCache
            # exception with the return value
            try:
                val=callee(*args, **kwargs)
            except BypassCache, b:
                val=b.value
                now=time()
                return CacheEntry(val,
                                  created=now,
                                  retrieved=now,
                                  expiration=now)
            
            # the callee has a chance to determine the expiration by
            # sending it out of band, through an "__expiration__"
            # attribute on the return value or the callee.  otherwise,
            # value passed to this method will be used.
            
            now=time()
            expiration=getattr(val,
                               '__expiration__',
                               getattr(callee,
                                       '__expiration__',
                                       expiration)) or 0
            if expiration:
                expiration=time_convert(expiration)
            entry=CacheEntry(val,
                             created=now,
                             retrieved=now,
                             expiration=expiration)
Exemplo n.º 2
0
 def expiration(self):
     exp=self.namespace.get('__expiration',
                            Configuration.defaultCacheExpiration)
     if not isinstance(exp, (int,float,long)):
         exp=time_convert(exp)
     return exp
Exemplo n.º 3
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:
                    return ondefer(entry, callee, callargs, expiration)
                return entry

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