コード例 #1
0
ファイル: decorators.py プロジェクト: sodrooome/lru-cache
    def wrapper(func):
        update_time = timedelta(seconds=seconds)
        next_update_time = datetime.utcnow() + update_time
        now_time = datetime.utcnow()
        func = LRUCache(capacity=capacity, seconds=seconds, **kwargs)
        return func

        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            # using nonlocal for defined
            # variable inside nested function
            nonlocal next_update_time, func
            if now_time > next_update_time:
                func.clear_all()
                next_update_time = now_time + update_time
            return func(*args, **kwargs)
        return wrapped
コード例 #2
0
class LRUCacheTest(unittest.TestCase):
    """Initial class for unittest. The test is
    pretty simple, only using True and False expression.
    """
    def setUp(self):
        self.testLRU = LRUCache(3)
        self.testLRU.set(1, "test1")
        self.testLRU.set(2, "test2")
        self.testLRU.set(3, "test3")

    def tearDown(self):
        self.testLRU = None

    def test_get_cache_key(self):
        self.assertTrue(self.testLRU.get_cache(1))
        self.assertFalse(self.testLRU.get_cache(4))

    def test_get_cache_dict(self):
        self.assertTrue(self.testLRU.get_dict())

    def test_get_cache_duration(self):
        self.assertTrue(self.testLRU.get_duration(expired_time=3600))
        # this should be return into False
        # cause the maximum duration
        # is 3600 seconds
        # todo: Fix this test
        self.assertTrue(self.testLRU.get_duration(expired_time=4800))

#    def test_get_least_recently_used(self):
#        self.assertFalse(self.testLRU.get_lru_element())

    def test_get_ttl(self):
        self.assertTrue(self.testLRU.get_ttl(1))

    def test_cache_is_empty(self):
        self.assertFalse(self.testLRU.is_empty())

    def test_remove_the_cache(self):
        self.assertIsNone(self.testLRU.clear_all())

    def test_remove_the_cache_key(self):
        self.assertIsNone(self.testLRU.clear_cache_key(1))

    def test_get_capacity(self):
        self.assertFalse(self.testLRU.get_capacity())

    def test_string_output_for_cache(self):
        self.assertTrue(self.testLRU.__str__())

    def test_set_new_object(self):
        self.assertTrue(self.testLRU.set(1, "test4"))

    def test_scale_capacity(self):
        self.assertTrue(self.testLRU(capacity=300))
コード例 #3
0
 def setUp(self):
     self.testLRU = LRUCache(3)
     self.testLRU.set(1, "test1")
     self.testLRU.set(2, "test2")
     self.testLRU.set(3, "test3")
コード例 #4
0
ファイル: decorators.py プロジェクト: leofuxiaohui/lru-cache
 def wrapper(func):
     return LRUCache(capacity=capacity, **kwargs)
コード例 #5
0
from lru.lrucache import LRUCache

# example test for LRU Cache
# initialize the object with parameters such as:
test_lru = LRUCache(capacity=3, seconds=15, thread_safe=False)

# set a value that wants to be cached
test_lru.set(1, "Collection Number 1")

# check whether the cache list is empty or not
print(test_lru.is_empty())

# get all cache and returned as dictionary
print(test_lru.get_dict())

# check time-to-left of expired duration of cache
print(test_lru.get_ttl(1))

# check current capacity of cache
print(test_lru.get_capacity())

# remove the cache in list based on their key
print(test_lru.clear_cache_key(1))

# check again whether there's still cache in list or not
print(test_lru.is_empty())