summaryrefslogtreecommitdiff
path: root/dashboard/utils.py
blob: 7065226cc7caf50f9914f548847b1299aa2a0517 (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
from django.core.cache import cache
from django.utils import crypto

GENERATION_KEY_NAME = "metric:generation"


def generation_key(timeout=60 * 60 * 24 * 365):
    """
    A random key to be used in cache calls that allows
    invalidating all values created with it. Use it with
    the version parameter of cache.get/set.
    """
    generation = cache.get(GENERATION_KEY_NAME)
    if generation is None:
        generation = crypto.get_random_string(length=12)
        cache.set(GENERATION_KEY_NAME, generation, timeout)
    return generation


def reset_generation_key():
    """
    Invalidate all cache entries created with the generation key.
    """
    cache.delete(GENERATION_KEY_NAME)