Repository navigation
Expand file tree
/
Copy pathcache.py
More file actions
42 lines (36 loc) · 1.39 KB
/
Copy pathcache.py
File metadata and controls
42 lines (36 loc) · 1.39 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
"""A tiny in-memory response cache with the call shape the google_hotels
module uses. Results live in this process only and expire after `ttl` (a
datetime.timedelta), so hotels/details + hotels/prices on the same hotel and
stay cost one fetch from Google.
Every hit is a deep copy, like a row read back from a database: a caller
that edits its result (routes.paginate pops `pagination`) never changes
what the next caller gets.
"""
import copy
import json
import threading
import time
_store = {}
_lock = threading.Lock()
MAX_ENTRIES = 2000
def _key(namespace, data):
return namespace + ":" + json.dumps(data, sort_keys=True, default=str)
def cached_call(namespace, data, fn, ttl, cache=True, refresh=False):
"""Serve fn(**data) from memory for `ttl`, else run it and keep the result.
Exceptions from fn propagate uncached."""
if not (cache and ttl):
return fn(**data)
key = _key(namespace, data)
now = time.time()
if not refresh:
with _lock:
hit = _store.get(key)
if hit and hit[0] > now:
return copy.deepcopy(hit[1])
result = fn(**data)
with _lock:
if len(_store) >= MAX_ENTRIES:
for stale in [k for k, (expires, _) in _store.items() if expires <= now] or list(_store)[:100]:
_store.pop(stale, None)
_store[key] = (now + ttl.total_seconds(), copy.deepcopy(result))
return result