forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaching.py
More file actions
42 lines (39 loc) · 1.04 KB
/
caching.py
File metadata and controls
42 lines (39 loc) · 1.04 KB
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
import collections
import hashlib
from eth_utils import (
is_boolean,
is_bytes,
is_dict,
is_list_like,
is_null,
is_number,
is_text,
to_bytes,
)
def generate_cache_key(value):
"""
Generates a cache key for the *args and **kwargs
"""
if is_bytes(value):
return hashlib.md5(value).hexdigest()
elif is_text(value):
return generate_cache_key(to_bytes(text=value))
elif is_boolean(value) or is_null(value) or is_number(value):
return generate_cache_key(repr(value))
elif is_dict(value):
return generate_cache_key((
(key, value[key])
for key
in sorted(value.keys())
))
elif is_list_like(value) or isinstance(value, collections.abc.Generator):
return generate_cache_key("".join((
generate_cache_key(item)
for item
in value
)))
else:
raise TypeError("Cannot generate cache key for value {0} of type {1}".format(
value,
type(value),
))