forked from NCAR/wrf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
163 lines (100 loc) · 3.46 KB
/
cache.py
File metadata and controls
163 lines (100 loc) · 3.46 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import (absolute_import, division, print_function)
from threading import local
from collections import OrderedDict
from .py3compat import py3range
from .config import get_cache_size
_local_storage = local()
def _shrink_cache():
"""Shrink the cache if applicable.
This only applies if a user has modified the cache size, otherwise it
just returns.
Returns:
None
"""
global _local_storage
try:
cache = _local_storage.cache
except AttributeError:
return
diff = len(cache) - get_cache_size()
if diff > 0:
for _ in py3range(diff):
cache.popitem(last=False)
def cache_item(key, product, value):
"""Store an item in the threadlocal cache.
The cache should be viewed as two nested dictionaries. The outer key is
usually the id for the sequence where the cached item was generated. The
inner key is the product type.
Storing a cached item behaves like:
cache[key][product] = value
The cache is thread local, so stored items are only available in
the thread that cached them.
Args:
key (:obj:`int`): The outer dictionary cache key, which is typically
the id of the sequence where the cached item was generated.
product (:obj:`str`): The inner dictionary cache key, which is a
string for the product type.
value (:obj:`object`): The object to store in the cache.
Returns:
None.
See Also:
:meth:`get_cached_item`
"""
global _local_storage
_shrink_cache()
if key is None or get_cache_size() == 0:
return
try:
cache = _local_storage.cache
except AttributeError:
_local_storage.cache = OrderedDict()
cache = _local_storage.cache
try:
_ = cache[key]
except KeyError:
if len(cache) >= get_cache_size():
cache.popitem(last=False) # Remove the oldest dataset
cache[key] = OrderedDict()
cache[key][product] = value
def get_cached_item(key, product):
"""Return an item from the threadlocal cache.
The cache should be viewed as two nested dictionaries. The outer key is
usually the id for the sequence where the cached item was generated. The
inner key is the product type.
Retrieving a cached item behaves like:
value = cache[key][product]
The cache is thread local, so stored items are only available in
the thread that cached them.
Args:
key (:obj:`int`): The outer dictionary cache key, which is typically
the id of the sequence where the cached item was generated.
product (:obj:`str`): The inner dictionary cache key, which is a
string for the product type.
Returns:
:obj:`object`: The cached object.
See Also:
:meth:`cache_item`
"""
global _local_storage
_shrink_cache()
if key is None or get_cache_size == 0:
return None
cache = getattr(_local_storage, "cache", None)
if cache is None:
return None
if len(cache) == 0:
return None
prod_dict = cache.get(key, None)
if prod_dict is None:
return None
result = prod_dict.get(product, None)
return result
def _get_cache():
"""Return the threadlocal cache.
This is primarily used for testing.
Returns:
:class:`threading.local`
"""
global _local_storage
_shrink_cache()
return getattr(_local_storage, "cache", None)