Commit 756c977f authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add docs for this function

parent 318820f2
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
imgutils.utils.cache
====================================

.. currentmodule:: imgutils.utils.cache

.. automodule:: imgutils.utils.cache



ts_lru_cache
------------------------------

.. autofunction:: ts_lru_cache


+1 −0
Original line number Diff line number Diff line
@@ -9,4 +9,5 @@ imgutils.utils
.. toctree::
    :maxdepth: 3

    cache
    onnxruntime
+41 −0
Original line number Diff line number Diff line
"""
This module provides a thread-safe version of Python's built-in lru_cache decorator.

The main component of this module is the ts_lru_cache decorator, which wraps the standard
lru_cache with thread-safe functionality. This is particularly useful in multi-threaded
environments where cache access needs to be synchronized to prevent race conditions.

Usage:
    >>> from imgutils.utils import ts_lru_cache
    ...
    >>> @ts_lru_cache(maxsize=100)
    >>> def expensive_function(x, y):
    ...     # Some expensive computation
    ...     return x + y
"""

import threading
from functools import lru_cache

@@ -5,6 +21,31 @@ __all__ = ['ts_lru_cache']


def ts_lru_cache(**options):
    """
    A thread-safe version of the lru_cache decorator.

    This decorator wraps the standard lru_cache with a threading lock to ensure
    thread-safety in multithreaded environments. It maintains the same interface
    as the built-in lru_cache, allowing you to specify options like maxsize.

    :param options: Keyword arguments to be passed to the underlying lru_cache.
    :type options: dict

    :return: A thread-safe cached version of the decorated function.
    :rtype: function

    :Example:
        >>> @ts_lru_cache(maxsize=100)
        >>> def my_function(x, y):
        ...     # Function implementation
        ...     return x + y

    .. note::
        While this decorator ensures thread-safety, it may introduce some overhead
        due to lock acquisition. Use it when thread-safety is more critical than
        maximum performance in multithreaded scenarios.
    """

    def _decorator(func):
        @lru_cache(**options)
        def _cached_func(*args, **kwargs):