fastapi-fire-cache 0.2.2
FastAPI Cache with Google Firestore backend
Requires Python: >=3.6.1

# FastAPI Fire Cache

[![Tests](https://github.com/sebustam/fastapi-fire-cache/actions/workflows/tests.yaml/badge.svg)](https://github.com/sebustam/fastapi-fire-cache/actions/workflows/tests.yaml)
[![Known Vulnerabilities](https://snyk-widget.herokuapp.com/badge/pip/fastapi-fire-cache/badge.svg)](https://snyk.io/test/github/sebustam/fastapi-fire-cache)
[![Coverage](https://codecov.io/gh/sebustam/fastapi-fire-cache/branch/main/graph/badge.svg?token=6JPFPOQWX2)](https://codecov.io/gh/sebustam/fastapi-fire-cache)
[![Package version](https://img.shields.io/pypi/v/fastapi-fire-cache?color=%2334D058)](https://pypi.org/project/fastapi-fire-cache)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/fastapi-fire-cache.svg?color=%2334D058)](https://pypi.org/project/fastapi-fire-cache)

FastAPI Fire Cache will cache to Google Firestore successful responses (status
code 200) from a decorated endpoint if the response
is [JSON encodable](https://fastapi.tiangolo.com/tutorial/encoder/) or
a [FastAPI `Response`](https://fastapi.tiangolo.com/advanced/response-directly/).

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [How to use](#how-to-use)
- [Installation](#installation)
- [Additional features](#additional-features)
  - [Namespaces](#namespaces)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## How to use

1. Initialize your FastAPI app

    ```python
    import FastAPI

    app = FastAPI()
    ```

2. Select your cache collection

    ```python
    import firebase_admin
    from firebase_admin import firestore, credentials

    cred = credentials.ApplicationDefault()
    firebase_admin.initialize_app(cred, {
        'projectId': "my-gcp-project"
    })
    db = firestore.client()

    collection = db.collection("my-cache-collection")
    ```

3. Initialize cache backend with FastAPI startup event

    ```python
    from fastapi_fire_cache import FastAPIFireCache

    @app.on_event("startup")
    async def startup():
        FastAPIFireCache.init(collection=collection)
        pass
    ```

4. Decorate your endpoints with `@cache`

    ```python
    from fastapi_fire_cache.decorator import cache

    @app.get("/")
    @cache(expire=60)  # Set expiration time of 60 seconds
    def return_msg(message: str):
        return {"message": message}
    ```

## Installation

```bash
pip install fastapi-fire-cache
```

## Additional features

### Namespaces

You can add the parameter `namespace` on cache initialization
with `FastAPIFireCache.init` to use the same firestore collection as
other applications but with different keys.

```python
@app.on_event("startup")
async def startup():
    FastAPIFireCache.init(
        collection=collection,
        namespace="my-app-namespace"
    )
    pass
```

## License

FastAPI Fire Cache is released under the GNU General Public License v3.0 or later,
see [here](https://choosealicense.com/licenses/gpl-3.0/) for a description of this
license, or see the [LICENSE](./LICENSE) file for the full text.