-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_file_lru_cache.py
73 lines (57 loc) · 1.75 KB
/
test_file_lru_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# coding: utf8
import logging
import os.path
import sys
from lru_cache.file_lru_cache import FileLRUCacheBuilder
from lru_cache.abstract_lru_cache import (
ProxyCache,
Serializer,
ReturnCode)
LOGGER = logging.getLogger(__name__)
# 测试之前,确保 BASE_DIR 存在
BASE_DIR = "data/"
if not os.path.isdir(BASE_DIR):
sys.exit("please make sure BASE_DIR exists")
class TestSerializer(Serializer):
def loads(self, data):
return data
def dumps(self, obj):
return len(obj), obj
def test():
flc = FileLRUCacheBuilder() \
.with_name("file-lru-cache") \
.with_base_path(BASE_DIR) \
.with_max_entry_count(10000) \
.with_max_size(10*1024*1024*1024) \
.build()
flc.start()
flc.wait_for_usable()
proxy_cache = ProxyCache()
proxy_cache.add_cache(flc)
proxy_cache.set_key_func(
lambda _, key, *a, **kw: key)
proxy_cache.set_call_func_when_failure(False)
proxy_cache.set_serializer(TestSerializer())
@proxy_cache.deco
def f(key):
return key
try:
k = "thisisavalidkey"
for _ in range(2):
ret = f(k)
LOGGER.info("ret = [[%s]]", ret)
for cache in proxy_cache.caches:
ret = cache.purge(k)
LOGGER.info(
"purge result = [[%s]]",
(ret & ReturnCode.OK and True or False))
finally:
for cache in proxy_cache.caches:
cache.stop()
if __name__ == "__main__":
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(threadName)s "
"%(filename)s:%(lineno)d %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
test()