Skip to main content

Simple decorator to cache coroutine function results

Project description

aiocached

aioached is a simple package with decorator cached to cache results of ordinary and coroutine functions with configurable TTL and None value support

Latest Version Python Version License

I wrote a simple helper to cache results in one service because I found it easy to do. As soon as I needed the helper in another project, I realized that it should be in a separate package published on PyPI. Having found aiocache project I was disappointed because it wasn't able to cache None values. So I had a reason to create aiocached.

Table of contents

Usage examples

In this example foo(1) will be run just once:

import asyncio
from aiocached import cached

@cached
async def foo(n):
    await asyncio.sleep(n)

async def main():
    await asyncio.gather(*[foo(1) for _ in range(1000)])

asyncio.run(main())

In this example bar(1) will be run twice because of TTL:

import asyncio
from aiocached import cached

@cached(ttl=2)
async def bar(n):
    await asyncio.sleep(n)

async def main():
    await bar(1)
    await asyncio.sleep(2)
    await bar(1)

asyncio.run(main())

If you want to cache an ordinary function, you can do it as well. In this example foobar(1) will be run twice for the same reason as above:

import time
from aiocached import cached

@cached(ttl=2)
def foobar(n):
    time.sleep(n)

def main():
    foobar(1)
    time.sleep(2)
    foobar(1)

main()

Installation

Use pip to install:

$ pip install aiocached

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

aiocached-0.3.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file aiocached-0.3.tar.gz.

File metadata

  • Download URL: aiocached-0.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.0

File hashes

Hashes for aiocached-0.3.tar.gz
Algorithm Hash digest
SHA256 22ab674c067a7cb3c5659da7e48447b546d8f5b5298ec61979612321760f1c69
MD5 cfd7de7d096a843673fd7e0e98ff3900
BLAKE2b-256 1ca5c66db2e30651cc4bf2cadfb2d07cdcf944396a47b08acdd544bc033ab532

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