Skip to main content

Fastapi Redis client with decorator for controllers

Project description

Fastapi Redis

This package provides a client that integrates with Fastapi and provides a decorator to cache fastapi controllers responses.

Installation

$ pip install fastapi_redis

Usage

Client

Setting the data

import redis_client from fastapi_redis

redis_client.set('some_key', 'some_data')

Models can be saved as well and the client will serialize them to json:

import redis_client from fastapi_redis
from pydantic.main import BaseModel

MyModel(BaseModel):
    data: str

redis_client.set('some_key', MyModel(data='some_data'))
redis_client.set('some_key', MyModel(data='some_data_that_expires'), expiration=timedelta(days=1))

The data can be saved using the Fastapi Background Tasks so it wouldn't generate any delay on the response to the user:

import redis_client from fastapi_redis

@router.get('/')
async def my_controller_method(background_tasks: BackgroundTasks):
    redis_client.set_in_background(background_tasks, 'some_key', 'some_data')
    redis_client.set_in_background(background_tasks, 'some_key', MyModel(data='some_data_that_expires'), expiration=timedelta(days=1))

Getting the data

import redis_client from fastapi_redis

some_data = redis_client.get('some_key')

@redis_cache decorator

The @redis_cache decorator can be used to cache the response from a controller. Any argument of the controller can be used to specify the key of the data to store and if background_tasks: BackgroundTasks is defined as an argument it will store the data in the background as well:

import redis_client from fastapi_redis


@router.get('/resource/{id}')
@router_cache('resource_{id}_{user.id}', timedelta(days=1))
async def my_controller_method(id: str,
                               user: User = Depends(get_current_user)
                               background_tasks: BackgroundTasks):
    return some_data_that_will_be_cached

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-redis-0.1.11.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file fastapi-redis-0.1.11.tar.gz.

File metadata

  • Download URL: fastapi-redis-0.1.11.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for fastapi-redis-0.1.11.tar.gz
Algorithm Hash digest
SHA256 2a10b5dfa364478272f4fffb1c1aa322622cab24573b4335c1fd0d91b7820d8e
MD5 f5419e105312f4be2b721b917c3b2c1d
BLAKE2b-256 0477092167e49d64261810c2d1d1605c244615a3f54baebef6e0fd3093997210

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