Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

long2ice/fastapi-cache

Repository files navigation

pypilicenseCI/CD

Introduction

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

Features

  • Supportsredis,memcache,dynamodb, andin-memory backends.
  • Easy integration withFastAPI.
  • Support for HTTP cache headers likeETag andCache-Control, as well as conditionalIf-Match-None requests.

Requirements

  • FastAPI
  • redis when usingRedisBackend.
  • memcache when usingMemcacheBackend.
  • aiobotocore when usingDynamoBackend.

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

fromcollections.abcimportAsyncIteratorfromcontextlibimportasynccontextmanagerfromfastapiimportFastAPIfromstarlette.requestsimportRequestfromstarlette.responsesimportResponsefromfastapi_cacheimportFastAPICachefromfastapi_cache.backends.redisimportRedisBackendfromfastapi_cache.decoratorimportcachefromredisimportasyncioasaioredis@asynccontextmanagerasyncdeflifespan(_:FastAPI)->AsyncIterator[None]:redis=aioredis.from_url("redis://localhost")FastAPICache.init(RedisBackend(redis),prefix="fastapi-cache")yieldapp=FastAPI(lifespan=lifespan)@cache()asyncdefget_cache():return1@app.get("/")@cache(expire=60)asyncdefindex():returndict(hello="world")

Initialization

First you must callFastAPICache.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@cachedecorator between the router decorator and the view function.

Parametertypedefaultdescription
expireintsets the caching time in seconds
namespacestr""namespace to use to store certain cache items
coderCoderJsonCoderwhich coder to use, e.g.JsonCoder
key_builderKeyBuilder callabledefault_key_builderwhich key builder to use
injected_dependency_namespacestr__fastapi_cacheprefix for injected dependency keywords.
cache_status_headerstrX-FastAPI-CacheName for the header on the response indicating if the request was served from cache; eitherHIT orMISS.

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

Injected Request and Response dependencies

Thecache decorator injects dependencies for theRequest andResponseobjects, so that it can add cache control headers to the outgoing response, andreturn a 304 Not Modified response when the incoming request has a matchingIf-Non-Match header. This only happens if the decorated endpoint doesn't alreadylist these dependencies already.

The keyword arguments for these extra dependencies are named__fastapi_cache_request and__fastapi_cache_response to minimize collisions.Use theinjected_dependency_namespace argument to@cache to change theprefix 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. Anannotation is not needed if the return type is a standard JSON-supported Pythontype such as a dictionary or a list.

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

from .modelsimportSomeModel,create_some_model@app.get("/foo")@cache(expire=60)asyncdeffoo()->SomeModel:returncreate_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 thefastapi_cache.coder.PickleCoder or implement a custom coder (see below).

Custom coder

By default useJsonCoder, you can write custom coder to encode and decode cache result, just needinheritfastapi_cache.coder.Coder.

fromtypingimportAnyimportorjsonfromfastapi.encodersimportjsonable_encoderfromfastapi_cacheimportCoderclassORJsonCoder(Coder):@classmethoddefencode(cls,value:Any)->bytes:returnorjson.dumps(value,default=jsonable_encoder,option=orjson.OPT_NON_STR_KEYS|orjson.OPT_SERIALIZE_NUMPY,        )@classmethoddefdecode(cls,value:bytes)->Any:returnorjson.loads(value)@app.get("/")@cache(expire=60,coder=ORJsonCoder)asyncdefindex():returndict(hello="world")

Custom key builder

By default thedefault_key_builder builtin key builder is used; this creates acache key from the function module and name, and the positional and keywordarguments converted to theirrepr() representations, encoded as a MD5 hash.You can provide your own by passing a key builder in to@cache(), or toFastAPICache.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:

defrequest_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)asyncdefindex():returndict(hello="world")

Backend notes

InMemoryBackend

TheInMemoryBackend stores cache data in memory and only deletes when anexpired key is accessed. This means that if you don't access a function afterdata 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 doesnot decode responses (decode_responsesmust beFalse, which is the default). Cached data is stored asbytes (binary), decoding these in the Redis client would break caching.

Tests and coverage

coverage run -m pytestcoverage htmlxdg-open htmlcov/index.html

License

This project is licensed under theApache-2.0 License.

Sponsor

Powered by DartNode

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors26


    [8]ページ先頭

    ©2009-2025 Movatter.jp