async-lru-threadsafe


Nameasync-lru-threadsafe JSON
Version 2.0.4 PyPI version JSON
download
home_pagehttps://github.com/aio-libs/async-lru
SummarySimple threadsafe LRU cache for asyncio
upload_time2023-09-26 19:16:43
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT License
keywords asyncio lru lru_cache
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            async-lru
=========

:info: Simple lru cache for asyncio

.. image:: https://github.com/aio-libs/async-lru/actions/workflows/ci-cd.yml/badge.svg?event=push
   :target: https://github.com/aio-libs/async-lru/actions/workflows/ci-cd.yml?query=event:push
   :alt: GitHub Actions CI/CD workflows status

.. image:: https://img.shields.io/pypi/v/async-lru.svg?logo=Python&logoColor=white
   :target: https://pypi.org/project/async-lru
   :alt: async-lru @ PyPI

.. image:: https://codecov.io/gh/aio-libs/async-lru/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/aio-libs/async-lru

.. image:: https://img.shields.io/matrix/aio-libs:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat
   :target: https://matrix.to/#/%23aio-libs:matrix.org
   :alt: Matrix Room — #aio-libs:matrix.org

.. image:: https://img.shields.io/matrix/aio-libs-space:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs-space%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat
   :target: https://matrix.to/#/%23aio-libs-space:matrix.org
   :alt: Matrix Space — #aio-libs-space:matrix.org

Installation
------------

.. code-block:: shell

    pip install async-lru

Usage
-----

This package is a port of Python's built-in `functools.lru_cache <https://docs.python.org/3/library/functools.html#functools.lru_cache>`_ function for `asyncio <https://docs.python.org/3/library/asyncio.html>`_. To better handle async behaviour, it also ensures multiple concurrent calls will only result in 1 call to the wrapped function, with all ``await``\s receiving the result of that call when it completes.

.. code-block:: python

    import asyncio

    import aiohttp
    from async_lru import alru_cache


    @alru_cache(maxsize=32)
    async def get_pep(num):
        resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
        async with aiohttp.ClientSession() as session:
            try:
                async with session.get(resource) as s:
                    return await s.read()
            except aiohttp.ClientError:
                return 'Not Found'


    async def main():
        for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
            pep = await get_pep(n)
            print(n, len(pep))

        print(get_pep.cache_info())
        # CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)

        # closing is optional, but highly recommended
        await get_pep.cache_close()


    asyncio.run(main())


TTL (time-to-live, expiration on timeout) is supported by accepting `ttl` configuration
parameter (off by default):

.. code-block:: python

    @alru_cache(ttl=5)
    async def func(arg):
        return arg * 2


The library supports explicit invalidation for specific function call by
`cache_invalidate()`:

.. code-block:: python

    @alru_cache(ttl=5)
    async def func(arg1, arg2):
        return arg1 + arg2

    func.cache_invalidate(1, arg2=2)

The method returns `True` if corresponding arguments set was cached already, `False`
otherwise.


Python 3.8+ is required

Thanks
------

The library was donated by `Ocean S.A. <https://ocean.io/>`_

Thanks to the company for contribution.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/aio-libs/async-lru",
    "name": "async-lru-threadsafe",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "asyncio,lru,lru_cache",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/e7/b0/28d9f0f61e2f5895dfa4e2c99f1695cc5fef8fb51dbbbc076444bc156683/async-lru-threadsafe-2.0.4.tar.gz",
    "platform": null,
    "description": "async-lru\n=========\n\n:info: Simple lru cache for asyncio\n\n.. image:: https://github.com/aio-libs/async-lru/actions/workflows/ci-cd.yml/badge.svg?event=push\n   :target: https://github.com/aio-libs/async-lru/actions/workflows/ci-cd.yml?query=event:push\n   :alt: GitHub Actions CI/CD workflows status\n\n.. image:: https://img.shields.io/pypi/v/async-lru.svg?logo=Python&logoColor=white\n   :target: https://pypi.org/project/async-lru\n   :alt: async-lru @ PyPI\n\n.. image:: https://codecov.io/gh/aio-libs/async-lru/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/aio-libs/async-lru\n\n.. image:: https://img.shields.io/matrix/aio-libs:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat\n   :target: https://matrix.to/#/%23aio-libs:matrix.org\n   :alt: Matrix Room \u2014 #aio-libs:matrix.org\n\n.. image:: https://img.shields.io/matrix/aio-libs-space:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs-space%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat\n   :target: https://matrix.to/#/%23aio-libs-space:matrix.org\n   :alt: Matrix Space \u2014 #aio-libs-space:matrix.org\n\nInstallation\n------------\n\n.. code-block:: shell\n\n    pip install async-lru\n\nUsage\n-----\n\nThis package is a port of Python's built-in `functools.lru_cache <https://docs.python.org/3/library/functools.html#functools.lru_cache>`_ function for `asyncio <https://docs.python.org/3/library/asyncio.html>`_. To better handle async behaviour, it also ensures multiple concurrent calls will only result in 1 call to the wrapped function, with all ``await``\\s receiving the result of that call when it completes.\n\n.. code-block:: python\n\n    import asyncio\n\n    import aiohttp\n    from async_lru import alru_cache\n\n\n    @alru_cache(maxsize=32)\n    async def get_pep(num):\n        resource = 'http://www.python.org/dev/peps/pep-%04d/' % num\n        async with aiohttp.ClientSession() as session:\n            try:\n                async with session.get(resource) as s:\n                    return await s.read()\n            except aiohttp.ClientError:\n                return 'Not Found'\n\n\n    async def main():\n        for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:\n            pep = await get_pep(n)\n            print(n, len(pep))\n\n        print(get_pep.cache_info())\n        # CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)\n\n        # closing is optional, but highly recommended\n        await get_pep.cache_close()\n\n\n    asyncio.run(main())\n\n\nTTL (time-to-live, expiration on timeout) is supported by accepting `ttl` configuration\nparameter (off by default):\n\n.. code-block:: python\n\n    @alru_cache(ttl=5)\n    async def func(arg):\n        return arg * 2\n\n\nThe library supports explicit invalidation for specific function call by\n`cache_invalidate()`:\n\n.. code-block:: python\n\n    @alru_cache(ttl=5)\n    async def func(arg1, arg2):\n        return arg1 + arg2\n\n    func.cache_invalidate(1, arg2=2)\n\nThe method returns `True` if corresponding arguments set was cached already, `False`\notherwise.\n\n\nPython 3.8+ is required\n\nThanks\n------\n\nThe library was donated by `Ocean S.A. <https://ocean.io/>`_\n\nThanks to the company for contribution.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Simple threadsafe LRU cache for asyncio",
    "version": "2.0.4",
    "project_urls": {
        "CI: GitHub Actions": "https://github.com/aio-libs/async-lru/actions",
        "Chat: Matrix": "https://matrix.to/#/#aio-libs:matrix.org",
        "Chat: Matrix Space": "https://matrix.to/#/#aio-libs-space:matrix.org",
        "GitHub: repo": "https://github.com/aio-libs/async-lru",
        "Homepage": "https://github.com/aio-libs/async-lru"
    },
    "split_keywords": [
        "asyncio",
        "lru",
        "lru_cache"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1a21d4e5157f207511e332d1746c6e18e36fbd50bd5c00684eaa9fb295de3106",
                "md5": "038b81a96e69d176651f9355a84ca4b8",
                "sha256": "6fef5a5545d1f254738c68db8899a6d39b4e47837ba45ae2291856f69a533a84"
            },
            "downloads": -1,
            "filename": "async_lru_threadsafe-2.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "038b81a96e69d176651f9355a84ca4b8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6208,
            "upload_time": "2023-09-26T19:16:42",
            "upload_time_iso_8601": "2023-09-26T19:16:42.097799Z",
            "url": "https://files.pythonhosted.org/packages/1a/21/d4e5157f207511e332d1746c6e18e36fbd50bd5c00684eaa9fb295de3106/async_lru_threadsafe-2.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e7b028d9f0f61e2f5895dfa4e2c99f1695cc5fef8fb51dbbbc076444bc156683",
                "md5": "3f995c249a5dff7b6887b8d67f596ba6",
                "sha256": "ee6d6ced06e474fa1fabc3a757e50148184bb2c4ce0812e294c68112d1b7ea00"
            },
            "downloads": -1,
            "filename": "async-lru-threadsafe-2.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3f995c249a5dff7b6887b8d67f596ba6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10162,
            "upload_time": "2023-09-26T19:16:43",
            "upload_time_iso_8601": "2023-09-26T19:16:43.724231Z",
            "url": "https://files.pythonhosted.org/packages/e7/b0/28d9f0f61e2f5895dfa4e2c99f1695cc5fef8fb51dbbbc076444bc156683/async-lru-threadsafe-2.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-26 19:16:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "aio-libs",
    "github_project": "async-lru",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "async-lru-threadsafe"
}
        
Elapsed time: 0.33664s