Skip to content

Commit b1705aa

Browse files
committed
dev: cache: Document functions
1 parent 3c9e489 commit b1705aa

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

crmsh/cache.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
# Copyright (C) 2008-2011 Dejan Muhamedagic <[email protected]>
2+
# Copyright (C) 2018 Kristoffer Gronlund <[email protected]>
23
# See COPYING for license information.
34
#
45
# Cache stuff. A naive implementation.
6+
# Used by ra.py to cache named lists of things.
57

68
import time
79

810

9-
_max_cache_age = 600 # seconds
11+
_max_cache_age = 600.0 # seconds
1012
_stamp = time.time()
1113
_lists = {}
1214

1315

1416
def _clear():
17+
"Clear the cache."
1518
global _stamp
1619
global _lists
1720
_stamp = time.time()
1821
_lists = {}
1922

2023

2124
def is_cached(name):
22-
if time.time() - _stamp > _max_cache_age:
23-
_clear()
24-
return name in _lists
25+
"True if the argument exists in the cache."
26+
return retrieve(name) is not None
2527

2628

2729
def store(name, lst):
30+
"""
31+
Stores the given list for the given name.
32+
Returns the given list.
33+
"""
2834
_lists[name] = lst
2935
return lst
3036

3137

3238
def retrieve(name):
33-
if is_cached(name):
34-
return _lists[name]
35-
return None
39+
"""
40+
Returns the cached list for name, or None.
41+
"""
42+
if time.time() - _stamp > _max_cache_age:
43+
_clear()
44+
return _lists.get(name)
3645

3746

3847
# vim:ts=4:sw=4:et:

0 commit comments

Comments
 (0)