def testExpiredLeastRecentlyUsedGracefullyDealtWith(self):
     timedDict = TimedDictionary(TWO_HOURS, lruMaxSize=2)
     timedDict['key1'] = 'five'
     timedDict['key2'] = 'six'
     timedDict['key3'] = 'seven'
     self.assertEquals(set(['key2', 'key3']), set(timedDict.keys()), set(timedDict.keys()))
     self.assertEquals(3, len(timedDict._times))
     self.assertEquals(3, len(timedDict._expirationOrder))
     timedDict.purge()
     self.assertEquals(2, len(timedDict._times))
     self.assertEquals(2, len(timedDict._expirationOrder))
     timedDict._now = lambda : time() + TWO_HOURS
     timedDict.purge()
     self.assertEquals([], list(timedDict.keys()))
     self.assertEquals(0, len(timedDict._times))
     self.assertEquals(0, len(timedDict._expirationOrder))
 def testExpiredLeastRecentlyUsedGracefullyDealtWith(self):
     timedDict = TimedDictionary(TWO_HOURS, lruMaxSize=2)
     timedDict['key1'] = 'five'
     timedDict['key2'] = 'six'
     timedDict['key3'] = 'seven'
     self.assertEqual(set(['key2', 'key3']), set(timedDict.keys()),
                      set(timedDict.keys()))
     self.assertEqual(3, len(timedDict._times))
     self.assertEqual(3, len(timedDict._expirationOrder))
     timedDict.purge()
     self.assertEqual(2, len(timedDict._times))
     self.assertEqual(2, len(timedDict._expirationOrder))
     timedDict._now = lambda: time() + TWO_HOURS
     timedDict.purge()
     self.assertEqual([], list(timedDict.keys()))
     self.assertEqual(0, len(timedDict._times))
     self.assertEqual(0, len(timedDict._expirationOrder))
Exemplo n.º 3
0
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()
 def testLruMaxSize(self):
     timedDict = TimedDictionary(TWO_HOURS, lruMaxSize=2)
     self.assertEqual(0, timedDict.size())
     timedDict['key'] = 'five'
     self.assertEqual(1, timedDict.size())
     timedDict['key1'] = 'six'
     self.assertEqual(2, timedDict.size())
     self.assertEquals('five', timedDict['key'])
     timedDict['key2'] = 'seven'
     self.assertEqual(2, timedDict.size())
     self.assertEquals(set(['key', 'key2']), set(timedDict.keys()))
 def testLruMaxSize(self):
     timedDict = TimedDictionary(TWO_HOURS, lruMaxSize=2)
     self.assertEqual(0, timedDict.size())
     timedDict['key'] = 'five'
     self.assertEqual(1, timedDict.size())
     timedDict['key1'] = 'six'
     self.assertEqual(2, timedDict.size())
     self.assertEqual('five', timedDict['key'])
     timedDict['key2'] = 'seven'
     self.assertEqual(2, timedDict.size())
     self.assertEqual(set(['key', 'key2']), set(timedDict.keys()))
Exemplo n.º 6
0
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()