MCPcopy Create free account
hub / github.com/NCAR/wrf-python / cache_item

Function cache_item

src/wrf/cache.py:37–91  ·  view source on GitHub ↗

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]

(key, product, value)

Source from the content-addressed store, hash-verified

35
36
37def cache_item(key, product, value):
38 """Store an item in the threadlocal cache.
39
40 The cache should be viewed as two nested dictionaries. The outer key is
41 usually the id for the sequence where the cached item was generated. The
42 inner key is the product type.
43
44 Storing a cached item behaves like:
45
46 cache[key][product] = value
47
48 The cache is thread local, so stored items are only available in
49 the thread that cached them.
50
51 Args:
52
53 key (:obj:`int`): The outer dictionary cache key, which is typically
54 the id of the sequence where the cached item was generated.
55
56 product (:obj:`str`): The inner dictionary cache key, which is a
57 string for the product type.
58
59 value (:obj:`object`): The object to store in the cache.
60
61 Returns:
62
63 None.
64
65 See Also:
66
67 :meth:`get_cached_item`
68
69 """
70 global _local_storage
71
72 _shrink_cache()
73
74 if key is None or get_cache_size() == 0:
75 return
76
77 try:
78 cache = _local_storage.cache
79 except AttributeError:
80 _local_storage.cache = OrderedDict()
81 cache = _local_storage.cache
82
83 try:
84 _ = cache[key]
85 except KeyError:
86 if len(cache) >= get_cache_size():
87 cache.popitem(last=False) # Remove the oldest dataset
88
89 cache[key] = OrderedDict()
90
91 cache[key][product] = value
92
93
94def get_cached_item(key, product):

Callers 5

runMethod · 0.90
is_moving_domainFunction · 0.85
_build_data_arrayFunction · 0.85
_cat_filesFunction · 0.85
_join_filesFunction · 0.85

Calls 2

_shrink_cacheFunction · 0.85
get_cache_sizeFunction · 0.85

Tested by 1

runMethod · 0.72