Metadata-Version: 2.1
Name: fastapi-and-caching
Version: 0.0.1
Summary: Easily use caching in FastAPI!
Home-page: https://github.com/heysaeid/fastapi-and-caching
License: MIT
Keywords: fastapi,fastapi caching,fastapi and caching
Author: Saeid Noormohammadi
Author-email: heysaeid92@gmail.com
Requires-Python: >=3.7,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: fastapi
Requires-Dist: redis (>=4.5.0)
Project-URL: Documentation, https://github.com/heysaeid/fastapi-and-caching
Project-URL: Repository, https://github.com/heysaeid/fastapi-and-caching
Description-Content-Type: text/markdown

# fastapi-and-caching
<strong>FastAPI and Caching</strong> is an extension for FastAPI that provides support for various caching mechanisms, allowing you to easily leverage caching within your FastAPI applications.

[![Package version](https://img.shields.io/pypi/v/fastapi-and-logging?color=%2334D058&label=pypi%20package)](https://pypi.org/project/fastapi-and-logging/)
[![Downloads](https://img.shields.io/pypi/dm/fastapi-and-logging)](https://pypi.org/project/fastapi-and-logging/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/fastapi-and-logging.svg?color=%2334D058)](https://pypi.org/project/fastapi-and-logging/)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/heysaeid/fastapi-and-logging/blob/master/LICENSE)

# Install
```
pip install git+https://github.com/heysaeid/fastapi-and-logging/tree/master
```

## How to Use:

### RedisCache
First, configure it as follows:

```python
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi_and_caching import RedisCache
from config import settings


app = FastAPI()
cache = RedisCache(namespace="fastapi") # 

@asynccontextmanager
async def lifespan(app: FastAPI):
    await cache.init(connection_url="redis://localhost")

    yield

    await cache.close()
```

Then you can use it as follows:
‍‍‍
```python
@app.get("/")
@cache.cached(key="root", expire=30, prefix="router")
def root():
    return "FastAPI And Caching"
```

Cached input parameters:
- `key` (str, optional): The key under which to store the cached result. 
  Defaults to the function name if not provided.
- `expire` (int, optional): Time in seconds for the cache to expire. Defaults to 60 seconds.
- `prefix` (str, optional): A prefix to add to the cache key. Defaults to None.
- `none` (bool, optional): Whether to cache None values. Defaults to True.
- `use_params` (bool, optional): Whether to include function parameters in the cache key. Defaults to True.
- `key_builder` (callable, optional): A custom function to build the cache key. Defaults to None.

#### Other methods:
cache.keys()
- `key` (str): The specific key or pattern to search for in the cache.
- `prefix` (str): A prefix to be added to the key before searching.


cache.get()
- `key` (str): The key to be used for retrieving the cached value.
- `prefix` (str, optional): A prefix to be added to the key before retrieving. Defaults to None.
- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.
- `key_builder` (typing.Callable, optional): A custom function for building the cache key. Defaults to None.


cache.set()
- `key` (str): The key under which to store the value in the cache.
- `value` (str): The value to be stored in the cache.
- `expire` (int, optional): Time in seconds for the cache entry to expire. Defaults to None.
- `prefix` (str, optional): A prefix to be added to the key before storing. Defaults to None.
- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.
- `key_builder` (typing.Callable, optional): A custom function for building the cache key. Defaults to None.
- `**kwargs`: Additional keyword arguments to be passed to the cache backend's set method.


cache.exists()
- `key` (str): The key to check for existence in the cache.
- `prefix` (str, optional): A prefix to be added to the key before checking. Defaults to None.


cache.expire()
- `key` (str): The key for which to set the expiration time.
- `seconds` (int): The number of seconds until the key expires.


cache.delete()
- `key` (str): The key to be deleted from the cache.
- `prefix` (str, optional): A prefix to be added to the key before deletion. Defaults to None.
- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.


cache.delete_startswith()
- `key` (str): The pattern to match at the beginning of cache keys.
- `prefix` (str, optional): A prefix to be added to the key before deletion. Defaults to None.
- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.
