summaryrefslogtreecommitdiffstats
path: root/crmsh/cache.py
blob: 98b539072d507ec4e7395295aaaff0f08440f3d2 (plain)
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
# Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
# Copyright (C) 2018 Kristoffer Gronlund <kgronlund@suse.com>
# See COPYING for license information.
#
# Cache stuff. A naive implementation.
# Used by ra.py to cache named lists of things.

import time


_max_cache_age = 600.0  # seconds
_stamp = time.time()
_lists = {}


def _clear():
    "Clear the cache."
    global _stamp
    global _lists
    _stamp = time.time()
    _lists = {}


def is_cached(name):
    "True if the argument exists in the cache."
    return retrieve(name) is not None


def store(name, lst):
    """
    Stores the given list for the given name.
    Returns the given list.
    """
    _lists[name] = lst
    return lst


def retrieve(name):
    """
    Returns the cached list for name, or None.
    """
    if time.time() - _stamp > _max_cache_age:
        _clear()
    return _lists.get(name)


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