예제 #1
0
class TestLRU(unittest.TestCase):
    def setUp(self):
        """set up: init a LRUCache"""
        self.cache = LRUCache(3)

    def test_lru_basic(self):
        test_value = [0, 1, 2, 3, 4]
        expected_value = [2, 3, 4]
        removed_value = [0, 1]
        for i in test_value:
            self.cache.add_value(i, i)

        for i in expected_value:
            self.assertEqual(self.cache.get_value(i), i)

        for i in removed_value:
            self.assertEqual(self.cache.get_value(i), None)

    def test_ttl(self):
        cache = LRUCache(3)
        cache.add_value('hello', 'world', ttl=3)
        self.assertEqual(cache.get_value('hello'), 'world')
        sleep(5)
        self.assertEqual(cache.get_value('hello'), None)

    def tearDown(self):
        """Cleaning up after the test"""
예제 #2
0
def main():
    cache = LRUCache(3)
    print 'add hello -> world with ttl=60 into lru'
    cache.add_value('hello', 'world', ttl=3)
    print 'get value from cache: %s -> %s' % ('hello',
                                              cache.get_value('hello'))
예제 #3
0
def main():
    cache = LRUCache(3)
    print 'add hello -> world with ttl=60 into lru'
    cache.add_value('hello', 'world', ttl=3)
    print 'get value from cache: %s -> %s' % ('hello', cache.get_value('hello'))
예제 #4
0
 def setUp(self):
     """set up: init a LRUCache"""
     self.cache = LRUCache(3)
예제 #5
0
 def test_ttl(self):
     cache = LRUCache(3)
     cache.add_value('hello', 'world', ttl=3)
     self.assertEqual(cache.get_value('hello'), 'world')
     sleep(5)
     self.assertEqual(cache.get_value('hello'), None)