HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/396/lib/third_party/cachetools/func.py
"""`functools.lru_cache` compatible memoizing function decorators."""

from __future__ import absolute_import

import collections
import functools
import random

try:
  from time import monotonic as default_timer
except ImportError:
  from time import time as default_timer

try:
  from threading import RLock
except ImportError:  # pragma: no cover
  from dummy_threading import RLock

from . import keys
from .lfu import LFUCache
from .lru import LRUCache
from .rr import RRCache
from .ttl import TTLCache

__all__ = ('lfu_cache', 'lru_cache', 'rr_cache', 'ttl_cache')

_CacheInfo = collections.namedtuple('CacheInfo',
                                    ['hits', 'misses', 'maxsize', 'currsize'])


class _UnboundCache(dict):

  maxsize = None

  @property
  def currsize(self):
    return len(self)


class _UnboundTTLCache(TTLCache):

  def __init__(self, ttl, timer):
    TTLCache.__init__(self, float('inf'), ttl, timer)

  @property
  def maxsize(self):
    return None


def _cache(cache, typed=False):

  def decorator(func):
    key = keys.typedkey if typed else keys.hashkey
    lock = RLock()
    stats = [0, 0]

    def cache_info():
      with lock:
        hits, misses = stats
        maxsize = cache.maxsize
        currsize = cache.currsize
      return _CacheInfo(hits, misses, maxsize, currsize)

    def cache_clear():
      with lock:
        try:
          cache.clear()
        finally:
          stats[:] = [0, 0]

    def wrapper(*args, **kwargs):
      k = key(*args, **kwargs)
      with lock:
        try:
          v = cache[k]
          stats[0] += 1
          return v
        except KeyError:
          stats[1] += 1
      v = func(*args, **kwargs)
      try:
        with lock:
          cache[k] = v
      except ValueError:
        pass  # value too large
      return v

    functools.update_wrapper(wrapper, func)
    if not hasattr(wrapper, '__wrapped__'):
      wrapper.__wrapped__ = func  # Python 2.7
    wrapper.cache_info = cache_info
    wrapper.cache_clear = cache_clear
    return wrapper

  return decorator


def lfu_cache(maxsize=128, typed=False):
  """Decorator to wrap a function with a memoizing callable that saves

    up to `maxsize` results based on a Least Frequently Used (LFU)
    algorithm.

    """
  if maxsize is None:
    return _cache(_UnboundCache(), typed)
  else:
    return _cache(LFUCache(maxsize), typed)


def lru_cache(maxsize=128, typed=False):
  """Decorator to wrap a function with a memoizing callable that saves

    up to `maxsize` results based on a Least Recently Used (LRU)
    algorithm.

    """
  if maxsize is None:
    return _cache(_UnboundCache(), typed)
  else:
    return _cache(LRUCache(maxsize), typed)


def rr_cache(maxsize=128, choice=random.choice, typed=False):
  """Decorator to wrap a function with a memoizing callable that saves

    up to `maxsize` results based on a Random Replacement (RR)
    algorithm.

    """
  if maxsize is None:
    return _cache(_UnboundCache(), typed)
  else:
    return _cache(RRCache(maxsize, choice), typed)


def ttl_cache(maxsize=128, ttl=600, timer=default_timer, typed=False):
  """Decorator to wrap a function with a memoizing callable that saves

    up to `maxsize` results based on a Least Recently Used (LRU)
    algorithm with a per-item time-to-live (TTL) value.
    """
  if maxsize is None:
    return _cache(_UnboundTTLCache(ttl, timer), typed)
  else:
    return _cache(TTLCache(maxsize, ttl, timer), typed)