async-lru


Nameasync-lru JSON
Version 2.0.4 PyPI version JSON
download
home_pagehttps://github.com/aio-libs/async-lru
SummarySimple LRU cache for asyncio
upload_time2023-07-27 19:12:18
maintaineraiohttp team <team@aiohttp.org>
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",
    "maintainer": "aiohttp team <team@aiohttp.org>",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "team@aiohttp.org",
    "keywords": "asyncio,lru,lru_cache",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/80/e2/2b4651eff771f6fd900d233e175ddc5e2be502c7eb62c0c42f975c6d36cd/async-lru-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 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": "fa9f3c3503693386c4b0f245eaf5ca6198e3b28879ca0a40bde6b0e319793453",
                "md5": "de1e9e7559810690de8b7084b372d9a2",
                "sha256": "ff02944ce3c288c5be660c42dbcca0742b32c3b279d6dceda655190240b99224"
            },
            "downloads": -1,
            "filename": "async_lru-2.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "de1e9e7559810690de8b7084b372d9a2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6111,
            "upload_time": "2023-07-27T19:12:17",
            "upload_time_iso_8601": "2023-07-27T19:12:17.164619Z",
            "url": "https://files.pythonhosted.org/packages/fa/9f/3c3503693386c4b0f245eaf5ca6198e3b28879ca0a40bde6b0e319793453/async_lru-2.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "80e22b4651eff771f6fd900d233e175ddc5e2be502c7eb62c0c42f975c6d36cd",
                "md5": "cd57e4d7f51bcbe2b940c523a5851b40",
                "sha256": "b8a59a5df60805ff63220b2a0c5b5393da5521b113cd5465a44eb037d81a5627"
            },
            "downloads": -1,
            "filename": "async-lru-2.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "cd57e4d7f51bcbe2b940c523a5851b40",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10019,
            "upload_time": "2023-07-27T19:12:18",
            "upload_time_iso_8601": "2023-07-27T19:12:18.631313Z",
            "url": "https://files.pythonhosted.org/packages/80/e2/2b4651eff771f6fd900d233e175ddc5e2be502c7eb62c0c42f975c6d36cd/async-lru-2.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-27 19:12:18",
    "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"
}
        
Elapsed time: 0.42401s