HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/jsons/_cache.py
"""
PRIVATE MODULE: do not import (from) it directly.

This module contains functionality for caching functions.
"""
from collections import deque
from functools import lru_cache, update_wrapper
from typing import Callable


class _Wrapper:
    """
    A wrapper around a function that needs to be cached. This wrapper allows
    for a single point from which cache can be cleared.
    """
    instances = deque([])

    def __init__(self, wrapped):
        self.wrapped = wrapped
        self.instances.append(self)

    @lru_cache(typed=True)
    def __call__(self, *args, **kwargs):
        return self.wrapped(*args, **kwargs)


def cached(decorated: Callable):
    """
    Alternative for ``functools.lru_cache``. By decorating a function with
    ``cached``, you can clear the cache of that function by calling
    ``clear()``.
    :param decorated: the decorated function.
    :return: a wrapped function.
    """
    wrapper = _Wrapper(decorated)
    update_wrapper(wrapper=wrapper, wrapped=decorated)
    return wrapper


def clear():
    """
    Clear all cache of functions that were cached using ``cached``.
    :return: None.
    """
    for w in _Wrapper.instances:
        w.__call__.cache_clear()