<commit_before>"""
This module is a wrapper for flask_caching. It configures it and rename
the memoize function. The aim with that cache is to minimize the requests
made on the target database.
"""
import redis

from functools import wraps
from flask_caching import Cache
from zou.app import config


cache = None

try:
    redis_cache = redis.StrictRedis(
        host=config.KEY_VALUE_STORE["host"],
        port=config.KEY_VALUE_STORE["port"],
        db=config.MEMOIZE_DB_INDEX,
        decode_responses=True
    )
    redis_cache.get("test")
    cache = Cache(config={
        "CACHE_TYPE": "redis",
        "CACHE_REDIS_HOST": config.KEY_VALUE_STORE["host"],
        "CACHE_REDIS_PORT": config.KEY_VALUE_STORE["port"],
        "CACHE_REDIS_DB": config.MEMOIZE_DB_INDEX
    })

# This is needed to run tests which. This way they do not require a Redis
# instance to work properly
except redis.ConnectionError:
    cache = Cache(config={
        "CACHE_TYPE": "simple"
    })

memoize_function = cache.memoize


def memoize_function(function, timeout=120):
    function = cache.memoize(function, timeout)

    @wraps(function)
    def wrapper(*args, **kwargs):
        result = function(*args, **kwargs)
        if result is None:
            cache.delete_memoized(function)
            result = function(*args, **kwargs)
        return result

    return wrapper


def clear():
    cache.clear()
<commit_msg>Add a function to unvalidate cache<commit_after>"""
This module is a wrapper for flask_caching. It configures it and rename
the memoize function. The aim with that cache is to minimize the requests
made on the target database.
"""
import redis

from functools import wraps
from flask_caching import Cache
from zou.app import config


cache = None

try:
    redis_cache = redis.StrictRedis(
        host=config.KEY_VALUE_STORE["host"],
        port=config.KEY_VALUE_STORE["port"],
        db=config.MEMOIZE_DB_INDEX,
        decode_responses=True
    )
    redis_cache.get("test")
    cache = Cache(config={
        "CACHE_TYPE": "redis",
        "CACHE_REDIS_HOST": config.KEY_VALUE_STORE["host"],
        "CACHE_REDIS_PORT": config.KEY_VALUE_STORE["port"],
        "CACHE_REDIS_DB": config.MEMOIZE_DB_INDEX
    })

# This is needed to run tests which. This way they do not require a Redis
# instance to work properly
except redis.ConnectionError:
    cache = Cache(config={
        "CACHE_TYPE": "simple"
    })

memoize_function = cache.memoize


def memoize_function(function, timeout=120):
    function = cache.memoize(function, timeout)

    @wraps(function)
    def wrapper(*args, **kwargs):
        result = function(*args, **kwargs)
        if result is None:
            cache.delete_memoized(function)
            result = function(*args, **kwargs)
        return result

    return wrapper


def invalidate(*args):
    cache.delete_memoized(*args)


def clear():
    cache.clear()

{'fraction_non_alphanumeric': 0.09776247848537005, 'fraction_numerical': 0.002065404475043029, 'mean_word_length': 3.273529411764706, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1168221', 'n_tokens_mistral': 910, 'n_tokens_neox': 884, 'n_words': 248}