Beispiel #1
0
def test_cache_get_many__ttl_expires_during_call(items: dict,
                                                 iteratee: t.Union[list, str,
                                                                   t.Pattern,
                                                                   t.Callable],
                                                 expected: dict):
    """Test that cache.get_many() returns without error when cache keys expire during call."""
    cache = Cache(ttl=1, timer=lambda: 0)

    cache.set_many(items)
    assert cache.get_many(iteratee) == expected

    cache.timer = lambda: 100
    assert cache.get_many(iteratee) == {}
Beispiel #2
0
def func(a, b):
    pass


func.uncached(1, 2)

assert cache.copy() == {1: "foobar", 2: ("foo", "bar", "baz")}

cache.delete(1)
assert cache.get(1) is None

cache.clear()
assert len(cache) == 0

cache.set_many({"a": 1, "b": 2, "c": 3})
assert cache.get_many(["a", "b", "c"]) == {"a": 1, "b": 2, "c": 3}
cache.delete_many(["a", "b", "c"])
assert cache.count() == 0

import re

cache.set_many({"a_1": 1, "a_2": 2, "123": 3, "b": 4})

# func.uncached(1, 2)

# assert cache.copy() == {1: "foobar", 2: ("foo", "bar", "baz")}

cache.set_many({"a": 1, "b": 2, "c": 3})
assert list(cache.keys()) == ["a", "b", "c"]
assert list(cache.values()) == [1, 2, 3]
assert list(cache.items()) == [("a", 1), ("b", 2), ("c", 3)]
Beispiel #3
0
def test_cache_get_many(cache: Cache, items: dict,
                        iteratee: t.Union[list, str, t.Pattern,
                                          t.Callable], expected: dict):
    """Test that cache.get_many() returns multiple cache key/values filtered by an iteratee."""
    cache.set_many(items)
    assert cache.get_many(iteratee) == expected
Beispiel #4
0
# 复制机制
# assert cache.copy() == {1: 'foobar', 2: ('foo', 'bar', 'baz')}

# 删除缓存中的一个键值对
cache.delete(1)
assert cache.get(1) is None

# 清除整个缓存
print(len(cache))
cache.clear()
assert len(cache) == 0

# 为get, set, delete设置了批量方法
cache.set_many({"a": 1, "b": 2, "c": 3})
assert cache.get_many(["a", "b", "c"])
print(len(cache))
assert cache.delete_many(["a", "b", "c"])
print(len(cache))

# 重置已经初始化的缓存对象
cache.configure(maxsize=1000, ttl=5 * 60)
cache.set_many({'a': 1, 'b': 2, 'c': 3})
assert list(cache.keys()) == ['a', 'b', 'c']
assert list(cache.values()) == [1, 2, 3]
assert list(cache.items()) == [('a', 1), ('b', 2), ('c', 3)]

# 迭代整个缓存的key
for key in cache:
    print(key, cache.get(key))
    # 'a' 1