def testPeek(self): timedDict = TimedDictionary(TWO_HOURS) timedDict[1] = "Now you see me, now you don't." timedDict._now = lambda : time() + TWO_HOURS self.assertEquals("Now you see me, now you don't.", timedDict.peek(1)) self.assertRaises(KeyError, timedDict.__getitem__, 1)
def testPeek(self): timedDict = TimedDictionary(TWO_HOURS) timedDict[1] = "Now you see me, now you don't." timedDict._now = lambda: time() + TWO_HOURS self.assertEqual("Now you see me, now you don't.", timedDict.peek(1)) self.assertRaises(KeyError, timedDict.__getitem__, 1)
class CookieMemoryStore(object): def __init__(self, timeout, name=None, secure=False, httpOnly=False): self._timeout = timeout self._store = TimedDictionary(self._timeout) self._name = '{0}{1}'.format('' if name is None else '%s-' % name, uuid4()) self._secure = secure self._httpOnly = httpOnly def createCookie(self, anObject): cookie = str(uuid4()) cookieValues = dict(value=anObject) cookieValues[SECURE] = self._secure cookieValues[HTTPONLY] = self._httpOnly self._store[cookie] = cookieValues return self._result(cookie) def removeCookie(self, cookie): try: del self._store[cookie] except KeyError: pass def removeCookies(self, filter): for k in self._store.keys(): try: if filter(self._store.peek(k)['value']): del self._store[k] except (AttributeError, KeyError): pass def validateCookie(self, cookie): cookieInfo = self._store.get(cookie) if cookieInfo is not None and cookieInfo['value'] is not None: self._store.touch(cookie) return self._result(cookie) return None def cookieName(self): return self._name def _result(self, cookie): cookieInfo = self._store.get(cookie) values = [ "{0}={1}".format(*i) for i in [(self._name, cookie), ( 'path', '/'), ('expires', formatdate(self._now() + self._timeout))] ] values.extend(k for k in [SECURE, HTTPONLY] if cookieInfo.get(k) == True) return dict(cookie=cookie, value=cookieInfo['value'], header='Set-Cookie: {0}'.format('; '.join(values))) def _now(self): return time()
class TimedMessageCache(Transparent): def __init__(self, cacheTimeout=1 * 60 * 60, returnCachedValueInCaseOfException=False, backoffTimeout=None, **kwargs): Transparent.__init__(self, **kwargs) self._cache = TimedDictionary(timeout=cacheTimeout) self._returnCachedValueInCaseOfException = returnCachedValueInCaseOfException self._backoffTimeout = backoffTimeout self._backoffStarted = None def clear(self): self._cache.clear() def setTimeout(self, timeout): self._cache.setTimeout(timeout) def getSize(self): return self._cache.size() def any_unknown(self, message, *args, **kwargs): found = False key = (message, repr(args), repr(kwargs)) try: value = self._cache.peek(key) except KeyError: pass else: found = True if not self._cache.hasExpired(key): return value if self._backoffStarted: if self._backoffStarted + self._backoffTimeout < now(): self._backoffStarted = None elif found: return value else: raise BackoffException() try: value = yield self.any.unknown(message, *args, **kwargs) self._cache[key] = value except (SystemExit, KeyboardInterrupt, AssertionError): raise except Exception as e: if self._backoffTimeout and isinstance(e, TimeoutException): self._backoffStarted = now() if not (self._returnCachedValueInCaseOfException and found): raise BackoffException() if not (self._returnCachedValueInCaseOfException and found): raise return value yield
class TimedMessageCache(Transparent): def __init__(self, cacheTimeout=1*60*60, returnCachedValueInCaseOfException=False, backoffTimeout=None, **kwargs): Transparent.__init__(self, **kwargs) self._cache = TimedDictionary(timeout=cacheTimeout) self._returnCachedValueInCaseOfException = returnCachedValueInCaseOfException self._backoffTimeout = backoffTimeout self._backoffStarted = None def clear(self): self._cache.clear() def setTimeout(self, timeout): self._cache.setTimeout(timeout) def getSize(self): return self._cache.size() def any_unknown(self, message, *args, **kwargs): found = False key = (message, repr(args), repr(kwargs)) try: value = self._cache.peek(key) except KeyError: pass else: found = True if not self._cache.hasExpired(key): raise StopIteration(value) if self._backoffStarted: if self._backoffStarted + self._backoffTimeout < now(): self._backoffStarted = None elif found: raise StopIteration(value) else: raise BackoffException() try: value = yield self.any.unknown(message, *args, **kwargs) self._cache[key] = value except (SystemExit, KeyboardInterrupt, AssertionError): raise except Exception, e: if self._backoffTimeout and isinstance(e, TimeoutException): self._backoffStarted = now() if not (self._returnCachedValueInCaseOfException and found): raise BackoffException() if not (self._returnCachedValueInCaseOfException and found): raise raise StopIteration(value) yield
class CookieMemoryStore(object): def __init__(self, timeout, name=None): self._timeout = timeout self._store = TimedDictionary(self._timeout) self._name = '{0}{1}'.format('' if name is None else '%s-' % name, uuid4()) def createCookie(self, anObject): cookie = str(uuid4()) self._store[cookie] = anObject return self._result(cookie, anObject) def removeCookie(self, cookie): try: del self._store[cookie] except KeyError: pass def removeCookies(self, filter): for k in self._store.keys(): try: if filter(self._store.peek(k)): del self._store[k] except (AttributeError, KeyError): pass def validateCookie(self, cookie): anObject = self._store.get(cookie) if anObject is not None: self._store.touch(cookie) return self._result(cookie, anObject) return None def cookieName(self): return self._name def _result(self, cookie, anObject): return dict( cookie=cookie, value=anObject, header='Set-Cookie: {0}={1}; path=/; expires={2}'.format( self._name, cookie, formatdate(self._now() + self._timeout))) def _now(self): return time()