Skip to main content

Simple threadsafe LRU cache for asyncio

Project description

info:

Simple lru cache for asyncio

GitHub Actions CI/CD workflows status async-lru @ PyPI https://codecov.io/gh/aio-libs/async-lru/branch/master/graph/badge.svg Matrix Room — #aio-libs:matrix.org Matrix Space — #aio-libs-space:matrix.org

Installation

pip install async-lru

Usage

This package is a port of Python’s built-in functools.lru_cache function for asyncio. To better handle async behaviour, it also ensures multiple concurrent calls will only result in 1 call to the wrapped function, with all awaits receiving the result of that call when it completes.

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):

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

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

@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.

Thanks to the company for contribution.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async-lru-threadsafe-2.0.4.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

async_lru_threadsafe-2.0.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file async-lru-threadsafe-2.0.4.tar.gz.

File metadata

  • Download URL: async-lru-threadsafe-2.0.4.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for async-lru-threadsafe-2.0.4.tar.gz
Algorithm Hash digest
SHA256 ee6d6ced06e474fa1fabc3a757e50148184bb2c4ce0812e294c68112d1b7ea00
MD5 3f995c249a5dff7b6887b8d67f596ba6
BLAKE2b-256 e7b028d9f0f61e2f5895dfa4e2c99f1695cc5fef8fb51dbbbc076444bc156683

See more details on using hashes here.

File details

Details for the file async_lru_threadsafe-2.0.4-py3-none-any.whl.

File metadata

  • Download URL: async_lru_threadsafe-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for async_lru_threadsafe-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6fef5a5545d1f254738c68db8899a6d39b4e47837ba45ae2291856f69a533a84
MD5 038b81a96e69d176651f9355a84ca4b8
BLAKE2b-256 1a21d4e5157f207511e332d1746c6e18e36fbd50bd5c00684eaa9fb295de3106

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page