MCPcopy Index your code
hub / github.com/authlib/authlib / SimpleCache

Class SimpleCache

tests/flask/cache.py:9–57  ·  view source on GitHub ↗

A SimpleCache for testing. Copied from Werkzeug.

Source from the content-addressed store, hash-verified

7
8
9class SimpleCache:
10 """A SimpleCache for testing. Copied from Werkzeug."""
11
12 def __init__(self, threshold=500, default_timeout=300):
13 self.default_timeout = default_timeout
14 self._cache = {}
15 self.clear = self._cache.clear
16 self._threshold = threshold
17
18 def _prune(self):
19 if len(self._cache) > self._threshold:
20 now = time.time()
21 toremove = []
22 for idx, (key, (expires, _)) in enumerate(self._cache.items()):
23 if (expires != 0 and expires <= now) or idx % 3 == 0:
24 toremove.append(key)
25 for key in toremove:
26 self._cache.pop(key, None)
27
28 def _normalize_timeout(self, timeout):
29 if timeout is None:
30 timeout = self.default_timeout
31 if timeout > 0:
32 timeout = time.time() + timeout
33 return timeout
34
35 def get(self, key):
36 try:
37 expires, value = self._cache[key]
38 if expires == 0 or expires > time.time():
39 return pickle.loads(value)
40 except (KeyError, pickle.PickleError):
41 return None
42
43 def set(self, key, value, timeout=None):
44 expires = self._normalize_timeout(timeout)
45 self._prune()
46 self._cache[key] = (expires, pickle.dumps(value, pickle.HIGHEST_PROTOCOL))
47 return True
48
49 def delete(self, key):
50 return self._cache.pop(key, None) is not None
51
52 def has(self, key):
53 try:
54 expires, value = self._cache[key]
55 return expires == 0 or expires > time.time()
56 except KeyError:
57 return False

Callers 6

create_resource_serverFunction · 0.85
test_init_app_paramsFunction · 0.85

Calls

no outgoing calls

Tested by 4

test_init_app_paramsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…