Skip to content

Commit

Permalink
rename project and update README (#11)
Browse files Browse the repository at this point in the history
Rename the project to 'fastapi-redis-cache-reborn'
  • Loading branch information
seapagan authored Mar 20, 2024
1 parent 79ec562 commit ad55104
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
86 changes: 60 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
# fastapi-redis-cache <!-- omit in toc -->
# fastapi-redis-cache-reborn <!-- omit in toc -->

<!-- [![PyPI version](https://badge.fury.io/py/fastapi-redis-cache.svg)](https://badge.fury.io/py/fastapi-redis-cache)
<!-- [![PyPI version](https://badge.fury.io/py/fastapi-redis-cache-reborn.svg)](https://badge.fury.io/py/fastapi-redis-cache-reborn)
![PyPI - Downloads](https://img.shields.io/pypi/dm/fastapi-redis-cache?color=%234DC71F)
![PyPI - License](https://img.shields.io/pypi/l/fastapi-redis-cache?color=%25234DC71F)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-redis-cache)
[![Maintainability](https://api.codeclimate.com/v1/badges/ec0b1d7afb21bd8c23dc/maintainability)](https://codeclimate.com/github/a-luna/fastapi-redis-cache/maintainability)
[![codecov](https://codecov.io/gh/a-luna/fastapi-redis-cache/branch/main/graph/badge.svg?token=dUaILJcgWY)](https://codecov.io/gh/a-luna/fastapi-redis-cache) -->
-->

- [Important](#important)
- [Migrating from `fastapi-redis-cache`](#migrating-from-fastapi-redis-cache)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Initialize Redis](#initialize-redis)
- [Redis Server](#redis-server)
- [Initialize Redis in your FastAPI application](#initialize-redis-in-your-fastapi-application)
- [`@cache` Decorator](#cache-decorator)
- [Response Headers](#response-headers)
- [Pre-defined Lifetimes](#pre-defined-lifetimes)
- [Cache Keys](#cache-keys)
- [Cache Keys Pt 2](#cache-keys-pt-2)
- [Questions/Contributions](#questionscontributions)

## Important
## Migrating from `fastapi-redis-cache`

This project is a continuation of
[fastapi-redis-cache](https://github.com/a-luna/fastapi-redis-cache) which seems
to no longer be maintained. I decided to split this as a separate repository
rather than a fork, since the original project has had no activity for a over
three years.
to no longer be maintained and had fallen behind in both `Redis` and `FastAPI`
versions. I decided to split this as a separate repository rather than a fork,
since the original project has had no activity for a over three years.

Right now the code is exactly the same as the original project, but I have
Right now the code is basically the same as the original project, but I have
updated the Package management system to use `Poetry`, the dependencies and the
CI/CD pipeline. I'll also merge any useful PR's that are open in the original
project.
CI/CD pipeline, and added type-hinting. I've also merged some open PRs from the
original project that fixed some issues.

Currently, even though the tests all pass there is no guarantee that the
project works as expected until I have tested it in a real project.
See the [TODO File](TODO.md) file for a list of things I plan to do in the near
future.

The package still has the same interface and classes as the original. You will
still import the package as `fastapi_redis_cache` in your code, the name has
only changed on PyPI to avoid conflicts with the original package. This is to
make it transparent to migrate to this version.

However, it is important to make sure that the old package is uninstalled before
installing this one. The package name has changed, but the module name is still
`fastapi_redis_cache`. **The best way is to remove your old virtual environment
and run `pip install` or `poetry install` again**.

## Features

Expand All @@ -46,36 +56,60 @@ project works as expected until I have tested it in a real project.

## Installation

`pip install fastapi-redis-cache`
if you are using `poetry` (recommended):

```bash
poetry add fastapi-redis-cache-reborn
```

Otherwise you can use `pip`:

```bash
pip install fastapi-redis-cache-reborn
```

## Usage

### Initialize Redis
### Redis Server

You will need access to a Redis server. If you don't have one running locally,
you can use `Docker` or even a cloud service like `RedisLabs` or `AWS
ElastiCache`.

Create a `FastApiRedisCache` instance when your application starts by [defining
an event handler for the `"startup"`
event](https://fastapi.tiangolo.com/advanced/events/) as shown below:
Replace the `REDIS_SERVER_URL` with the address and port of your Redis server as
in the example below:

### Initialize Redis in your FastAPI application

Create a `FastApiRedisCache` instance when your application starts by defining
a ['lifespan' event handler](<https://fastapi.tiangolo.com/advanced/events/>) as shown below:

```python {linenos=table}
import os

from contextlib import asynccontextmanager

from fastapi import FastAPI, Request, Response
from fastapi_redis_cache import FastApiRedisCache, cache
from sqlalchemy.orm import Session

LOCAL_REDIS_URL = "redis://127.0.0.1:6379"

app = FastAPI(title="FastAPI Redis Cache Example")
REDIS_SERVER_URL = "redis://127.0.0.1:6379"

@app.on_event("startup")
def startup():
@asynccontextmanager
async def lifespan():
redis_cache = FastApiRedisCache()
redis_cache.init(
host_url=os.environ.get("REDIS_URL", LOCAL_REDIS_URL),
host_url=os.environ.get("REDIS_URL", REDIS_SERVER_URL),
prefix="myapi-cache",
response_header="X-MyAPI-Cache",
ignore_arg_types=[Request, Response, Session]
)
yield

app = FastAPI(title="FastAPI Redis Cache Example",lifespan=lifespan)

# routes and more code

```

After creating the instance, you must call the `init` method. The only required
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "fastapi-redis-cache"
name = "fastapi-redis-cache-reborn"
version = "0.3.0"
description = "A simple and robust caching solution for FastAPI endpoints, fueled by the unfathomable power of Redis."
description = "A simple and robust caching solution for FastAPI endpoints using Redis."
authors = ["Grant Ramsay <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand Down

0 comments on commit ad55104

Please sign in to comment.