def test_cache_delete_many(cache: Cache, items: dict, iteratee: t.Union[list, str, t.Pattern, t.Callable], expected: dict): """Test that cache.delete_many() deletes multiple cache key/values filtered by an iteratee.""" cache.set_many(items) cache.delete_many(iteratee) assert dict(cache.items()) == expected
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)] for key in cache: print(key, cache.get(key)) # 'a' 1 # 'b' 2 # 'c' 3 assert cache.has("a") assert "a" in cache cacheman = CacheManager({ "a": { "maxsize": 100 }, "b": {
def test_cache_items(cache: Cache): """Test that cache.items() returns all cache key/values.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) assert set(cache.items()) == set(items.items())
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 # 'b' 2 # 'c' 3 assert cache.has('a') assert 'a' in cache