Skip to main content

FastAPI simple cache

Project description

FastAPI Cache

Implements simple lightweight cache system as dependencies in FastAPI.

Installation

 $ pip install FastAPI-Cache

Usage example

from fastapi import Depends, FastAPI

from fastapi_cache import caches, close_caches
from fastapi_cache.backends.redis import CACHE_KEY, RedisCacheBackend

app = FastAPI()


def redis_cache():
    return caches.get(CACHE_KEY)


@app.get('/')
async def hello(
    cache: RedisCacheBackend = Depends(redis_cache)
):
    in_cache = await cache.get('some_cached_key')
    if not in_cache:
        await cache.set('some_cached_key', 'new_value', 5)

    return {'response': in_cache or 'default'}


@app.on_event('startup')
async def on_startup() -> None:
    rc = RedisCacheBackend('redis://redis')
    caches.set(CACHE_KEY, rc)


@app.on_event('shutdown')
async def on_shutdown() -> None:
    await close_caches()

TODO

  • Add tests
  • Add registry decorator

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

fastapi-cache-0.0.2.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file fastapi-cache-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi-cache-0.0.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for fastapi-cache-0.0.2.tar.gz
Algorithm Hash digest
SHA256 695dacaf8f71ba239846117e33076b0e59de283d76e7ca64c0805877f53125d6
MD5 ad3cc1bc08e93144bf3706659f1540f1
BLAKE2b-256 695991264571af49463d815e70c8ce743a9866ec7b2d86303c7a098b0769a8cd

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