• Stars
    star
    1,264
  • Rank 37,048 (Top 0.8 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.

fastapi-cache

pypi license CI/CD

Introduction

fastapi-cache is a tool to cache FastAPI endpoint and function results, with backends supporting Redis, Memcached, and Amazon DynamoDB.

Features

  • Supports redis, memcache, dynamodb, and in-memory backends.
  • Easy integration with FastAPI.
  • Support for HTTP cache headers like ETag and Cache-Control, as well as conditional If-Match-None requests.

Requirements

  • FastAPI
  • redis when using RedisBackend.
  • memcache when using MemcacheBackend.
  • aiobotocore when using DynamoBackend.

Install

> pip install fastapi-cache2

or

> pip install "fastapi-cache2[redis]"

or

> pip install "fastapi-cache2[memcache]"

or

> pip install "fastapi-cache2[dynamodb]"

Usage

Quick Start

from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache

from redis import asyncio as aioredis

app = FastAPI()


@cache()
async def get_cache():
    return 1


@app.get("/")
@cache(expire=60)
async def index():
    return dict(hello="world")


@app.on_event("startup")
async def startup():
    redis = aioredis.from_url("redis://localhost")
    FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")

Initialization

First you must call FastAPICache.init during startup FastAPI startup; this is where you set global configuration.

Use the @cache decorator

If you want cache a FastAPI response transparently, you can use the @cache decorator between the router decorator and the view function.

Parameter type default description
expire int sets the caching time in seconds
namespace str "" namespace to use to store certain cache items
coder Coder JsonCoder which coder to use, e.g. JsonCoder
key_builder KeyBuilder callable default_key_builder which key builder to use
injected_dependency_namespace str __fastapi_cache prefix for injected dependency keywords.
cache_status_header str X-FastAPI-Cache Name for the header on the response indicating if the request was served from cache; either HIT or MISS.

You can also use the @cache decorator on regular functions to cache their result.

Injected Request and Response dependencies

The cache decorator injects dependencies for the Request and Response objects, so that it can add cache control headers to the outgoing response, and return a 304 Not Modified response when the incoming request has a matching If-Non-Match header. This only happens if the decorated endpoint doesn't already list these dependencies already.

The keyword arguments for these extra dependencies are named __fastapi_cache_request and __fastapi_cache_response to minimize collisions. Use the injected_dependency_namespace argument to @cache to change the prefix used if those names would clash anyway.

Supported data types

When using the (default) JsonCoder, the cache can store any data type that FastAPI can convert to JSON, including Pydantic models and dataclasses, provided that your endpoint has a correct return type annotation. An annotation is not needed if the return type is a standard JSON-supported Python type such as a dictionary or a list.

E.g. for an endpoint that returns a Pydantic model named SomeModel, the return annotation is used to ensure that the cached result is converted back to the correct class:

from .models import SomeModel, create_some_model

@app.get("/foo")
@cache(expire=60)
async def foo() -> SomeModel:
    return create_some_model

It is not sufficient to configure a response model in the route decorator; the cache needs to know what the method itself returns. If no return type decorator is given, the primitive JSON type is returned instead.

For broader type support, use the fastapi_cache.coder.PickleCoder or implement a custom coder (see below).

Custom coder

By default use JsonCoder, you can write custom coder to encode and decode cache result, just need inherit fastapi_cache.coder.Coder.

from typing import Any
import orjson
from fastapi.encoders import jsonable_encoder
from fastapi_cache import Coder

class ORJsonCoder(Coder):
    @classmethod
    def encode(cls, value: Any) -> bytes:
        return orjson.dumps(
            value,
            default=jsonable_encoder,
            option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY,
        )

    @classmethod
    def decode(cls, value: bytes) -> Any:
        return orjson.loads(value)


@app.get("/")
@cache(expire=60, coder=ORJsonCoder)
async def index():
    return dict(hello="world")

Custom key builder

By default the default_key_builder builtin key builder is used; this creates a cache key from the function module and name, and the positional and keyword arguments converted to their repr() representations, encoded as a MD5 hash. You can provide your own by passing a key builder in to @cache(), or to FastAPICache.init() to apply globally.

For example, if you wanted to use the request method, URL and query string as a cache key instead of the function identifier you could use:

def request_key_builder(
    func,
    namespace: str = "",
    *,
    request: Request = None,
    response: Response = None,
    *args,
    **kwargs,
):
    return ":".join([
        namespace,
        request.method.lower(),
        request.url.path,
        repr(sorted(request.query_params.items()))
    ])


@app.get("/")
@cache(expire=60, key_builder=request_key_builder)
async def index():
    return dict(hello="world")

Backend notes

InMemoryBackend

The InMemoryBackend stores cache data in memory and only deletes when an expired key is accessed. This means that if you don't access a function after data has been cached, the data will not be removed automatically.

RedisBackend

When using the Redis backend, please make sure you pass in a redis client that does not decode responses (decode_responses must be False, which is the default). Cached data is stored as bytes (binary), decoding these in the Redis client would break caching.

Tests and coverage

coverage run -m pytest
coverage html
xdg-open htmlcov/index.html

License

This project is licensed under the Apache-2.0 License.

More Repositories

1

fastapi-limiter

A request rate limiter for fastapi
Python
452
star
2

synch

Sync data from the other DB to ClickHouse(cluster)
Python
345
star
3

asyncmy

A fast asyncio MySQL/MariaDB driver with replication protocol support
Python
234
star
4

meilisync

Realtime sync data from MySQL/PostgreSQL/MongoDB to Meilisearch
Python
230
star
5

asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
Python
169
star
6

rearq

A distributed task queue built with asyncio and redis, with built-in web interface
Python
147
star
7

swagin

Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Go
68
star
8

trader

A framework that automated cryptocurrency exchange with strategy
Go
62
star
9

databack

Backup your data from MySQL/PostgreSQL/SSH etc. to any other storages
Python
60
star
10

longurl

A self-hosted short url service
Go
51
star
11

meilisync-admin

A web admin dashboard for meilisync
Python
31
star
12

fibers

Fiber + Swagger = Fibers, a web framework dedicated to providing a FastAPI-like development experience
Go
26
star
13

alarmer

A tool focus on error reporting for your application, like sentry but lightweight
Python
19
star
14

fastapi-rest

Fast restful API based on FastAPI and TortoiseORM
Python
10
star
15

gema-web

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
TypeScript
9
star
16

awesome-web

Search awesome projects
TypeScript
8
star
17

chcli

A Terminal Client for ClickHouse with AutoCompletion and Syntax Highlighting.
Python
6
star
18

fettler

Auto refresh cache of redis with MySQL binlog
Python
4
star
19

telsearch-web

Frontend for telsearch
TypeScript
3
star
20

mccabe

Calculate the cyclomatic complexity of the source code
Python
3
star
21

s3web

Serve static files from any S3 compatible object storage endpoints
Go
2
star
22

ClashForiOS

Swift
2
star
23

xiaoai

小爱音箱非官方SDK
Python
2
star
24

awesome

Search for awesome github project
Go
2
star
25

longurl-web

This is frontend for https://github.com/long2ice/longurl
TypeScript
2
star
26

ergo

Python
2
star
27

gema

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
Python
2
star
28

nvim

My personal neovim config
Lua
2
star
29

sponsor

Sponsor page of long2ice
HTML
2
star
30

youtube-dl-api-server

api server for youtube-dl
Python
2
star
31

talkit

A self hosted comment system
1
star
32

long2ice

My Personal README.
1
star
33

vpsmon

Python
1
star
34

homepage

My homepage
TypeScript
1
star
35

kanp

See video by download
Python
1
star
36

devme

Python
1
star
37

creatable

A tool to create table from file/database to another database
Python
1
star
38

devme-web

TypeScript
1
star
39

hugo-theme-pure

Forked from https://github.com/xiaoheiAh/hugo-theme-pure and make improvements
CSS
1
star
40

fastapi-monitor

Python
1
star
41

meilisync-web

Frontend of meilisearch-admin
Vue
1
star