Name | redsauce JSON |
Version |
0.0.0.0a0
JSON |
| download |
home_page | None |
Summary | None |
upload_time | 2024-04-07 19:53:59 |
maintainer | None |
docs_url | None |
author | Eric DiGioacchino |
requires_python | None |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# RedSauce
RedSauce integrates with FastAPI, leveraging Redis to provide caching functionality with minimal overhead.
The foundation of the project was to reduce code overhead for caching endpoints and logging.
Using this structure you can expect to enable caching with a simple decorator.
[](https://codecov.io/gh/EricDiGi/RedSauce)
**Note: FastAPI is NOT a dependency of this repo**
## TL;DR
```Python
from fastapi import FastAPI
from models import DemoModel
app = FastAPI()
from redsauce import Generator
db = Generator(connection = {
'host':'localhost',
'port':'6379',
'decode_responses':False
})
cache = db()
# Caching is now available
@app.get("demo/rawtypes")
@cache.cache()
def my_endpoint(test:str):
# Do stuff
return test
@app.post("/demo/json")
@cache.cache(ttl=180, json=True)
def post_endpoint(model: DataModel):
return model.model_dump()
```
## Generator
```Python
__init__(
yaml_path:str = None # Path to the YAML configuration file
connection: dict = None # Connection specification to the Redis instance (optional if specified in YAML, will override YAML)
)
```
The Generator class enables users to dynamically initialize databases. Recieving parameters from either the configuration yaml file, or explicitly in the constructor. It returns the `GlobalServices` class which hosts functions like `search` and `cache`.
### Cache Events
Specify callbacks for cache hits and misses on the Generator class. These will be the defaults for all of the GlobalServices classes that are generated.
```Python
from redsauce import Generator
import my_callbacks
db = Generator('config.yaml')
db.on_cache_hit = my_callbacks.on_cache_hit
db.on_cache_miss = my_callbacks.on_cache_miss
```
## GlobalServices
`GlobalSevices` hosts the functions of each instance. Caching and Searching are its primary utilities. Later this will also spawn a Message Queue through `rq`.
```Python
__init__(
connection:dict, # Database connection speicification (db is 0 by default)
index:redis.IndexDefinition = None, # Optional - Required for searching
schema:List[<redis fields>] = None, # Optional - Required for searching
event_handlers:dict = None # Optional - Dictionary of callbacks (on_cache_hit/miss)
)
```
### Caching
By default the caching utility will use the MD5 checksum of the underlying function's KWARGS to generate an index key from the `endpoint` parameter and the checksum: example `demo:aaaBBBccc`.
```Python
cache = GlobalServices(**kwargs)
@fast.method("/endpoint", **api_wargs)
@cache.cache(
endpoint:string = 'cache', # the index key's prefix
ttl:int = None, # Expiration of stored record in seconds
json:bool = False, # Whether or not to expect dict output from wrapped function
logger = rootLogger # Specify the logger to be used by internal logging calls and callbacks
)
def underlying_function(mayparm: Any = None): # Works with pydantic models too
return "Bleh"
```
### Searching
A fun idea; this function turns the endpoint into a search function. The underlying function should specify the `Query` redis should use for searching its database. The one documented here uses `@index` specification for searching JSON documents.
```Python
cache = GlobalServices(**kwargs)
@fast.method("/endpoint", **api_wargs)
@cache.search()
def return_query(param1:int, param2: str): # Works with pydantic models too
p = {"parm1":parm1, "parm2":parm2}
c = lambda x: f"[{x} {x}]" if isinstance(x, int) else f"{x}"
q = ' '.join(
map(
lambda x: f'@{x[0]}:{c(x[1])}',
filter(
lambda x: x[1] is not None,
p.items()
)
)
)
q = '*' if not q else q
print(f'Query: {q}')
return q
```
## YAML Configuration
```YAML filename="saucy.yaml"
redis:
connection:
host: localhost
port: 6379
# Anything specified by [https://redis-py.readthedocs.io/en/stable/connections.html]
db0: # Optional - required for search
schema:
- name: identifier
path: $.doc.term
type: text # also supports: number, tag (More to come!)
# more schema fields ...
index:
prefix:
- "name:"
- "another:"
type: JSON # Also supports: HASH
# db[1-15] ??? Untested beyond this point
# DO NOT SPECIFY SCHEMA OR INDEX BEYOND THIS POINT
```
Raw data
{
"_id": null,
"home_page": null,
"name": "redsauce",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Eric DiGioacchino",
"author_email": "eric.digioacchino01@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/da/d4/cdf726427d3ee1e423a8e470d63377b60a243a9e8ef4f124c758fa960f44/redsauce-0.0.0.0a0.tar.gz",
"platform": null,
"description": "# RedSauce\n\nRedSauce integrates with FastAPI, leveraging Redis to provide caching functionality with minimal overhead.\nThe foundation of the project was to reduce code overhead for caching endpoints and logging. \nUsing this structure you can expect to enable caching with a simple decorator.\n\n[](https://codecov.io/gh/EricDiGi/RedSauce)\n\n**Note: FastAPI is NOT a dependency of this repo**\n\n## TL;DR\n```Python\nfrom fastapi import FastAPI\nfrom models import DemoModel\napp = FastAPI()\n\nfrom redsauce import Generator\n\ndb = Generator(connection = {\n 'host':'localhost',\n 'port':'6379',\n 'decode_responses':False\n})\ncache = db()\n# Caching is now available\n\n@app.get(\"demo/rawtypes\")\n@cache.cache()\ndef my_endpoint(test:str):\n # Do stuff\n return test\n\n@app.post(\"/demo/json\")\n@cache.cache(ttl=180, json=True)\ndef post_endpoint(model: DataModel):\n return model.model_dump()\n\n```\n\n## Generator\n```Python\n__init__(\n yaml_path:str = None # Path to the YAML configuration file\n connection: dict = None # Connection specification to the Redis instance (optional if specified in YAML, will override YAML)\n)\n```\n\nThe Generator class enables users to dynamically initialize databases. Recieving parameters from either the configuration yaml file, or explicitly in the constructor. It returns the `GlobalServices` class which hosts functions like `search` and `cache`. \n### Cache Events\nSpecify callbacks for cache hits and misses on the Generator class. These will be the defaults for all of the GlobalServices classes that are generated.\n```Python\nfrom redsauce import Generator\nimport my_callbacks\n\ndb = Generator('config.yaml')\n\ndb.on_cache_hit = my_callbacks.on_cache_hit\ndb.on_cache_miss = my_callbacks.on_cache_miss\n```\n## GlobalServices\n`GlobalSevices` hosts the functions of each instance. Caching and Searching are its primary utilities. Later this will also spawn a Message Queue through `rq`.\n```Python\n__init__(\n connection:dict, # Database connection speicification (db is 0 by default)\n index:redis.IndexDefinition = None, # Optional - Required for searching\n schema:List[<redis fields>] = None, # Optional - Required for searching\n event_handlers:dict = None # Optional - Dictionary of callbacks (on_cache_hit/miss)\n)\n```\n\n### Caching\nBy default the caching utility will use the MD5 checksum of the underlying function's KWARGS to generate an index key from the `endpoint` parameter and the checksum: example `demo:aaaBBBccc`.\n```Python\ncache = GlobalServices(**kwargs)\n\n@fast.method(\"/endpoint\", **api_wargs)\n@cache.cache(\n endpoint:string = 'cache', # the index key's prefix\n ttl:int = None, # Expiration of stored record in seconds\n json:bool = False, # Whether or not to expect dict output from wrapped function\n logger = rootLogger # Specify the logger to be used by internal logging calls and callbacks\n )\ndef underlying_function(mayparm: Any = None): # Works with pydantic models too\n return \"Bleh\"\n```\n\n\n### Searching\nA fun idea; this function turns the endpoint into a search function. The underlying function should specify the `Query` redis should use for searching its database. The one documented here uses `@index` specification for searching JSON documents.\n\n```Python\ncache = GlobalServices(**kwargs)\n\n@fast.method(\"/endpoint\", **api_wargs)\n@cache.search()\ndef return_query(param1:int, param2: str): # Works with pydantic models too\n p = {\"parm1\":parm1, \"parm2\":parm2}\n c = lambda x: f\"[{x} {x}]\" if isinstance(x, int) else f\"{x}\"\n q = ' '.join(\n map(\n lambda x: f'@{x[0]}:{c(x[1])}', \n filter(\n lambda x: x[1] is not None, \n p.items()\n )\n )\n )\n q = '*' if not q else q\n print(f'Query: {q}')\n return q\n```\n\n## YAML Configuration\n\n```YAML filename=\"saucy.yaml\"\nredis:\n connection:\n host: localhost\n port: 6379\n # Anything specified by [https://redis-py.readthedocs.io/en/stable/connections.html]\n db0: # Optional - required for search\n schema:\n - name: identifier\n path: $.doc.term\n type: text # also supports: number, tag (More to come!)\n # more schema fields ...\n index:\n prefix:\n - \"name:\"\n - \"another:\"\n type: JSON # Also supports: HASH\n # db[1-15] ??? Untested beyond this point\n # DO NOT SPECIFY SCHEMA OR INDEX BEYOND THIS POINT\n```\n",
"bugtrack_url": null,
"license": null,
"summary": null,
"version": "0.0.0.0a0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f902aadc0ba3a6330a3de049f1accebaeb3c9d25c6eef917e31569d6d5049da7",
"md5": "f5e22a134cd9c116a3cd4258c3d34bb0",
"sha256": "94927627bea45443b280ee4f81a2ec67a43207e46ea7e86a1e54aa16f8625ee2"
},
"downloads": -1,
"filename": "redsauce-0.0.0.0a0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f5e22a134cd9c116a3cd4258c3d34bb0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8089,
"upload_time": "2024-04-07T19:53:57",
"upload_time_iso_8601": "2024-04-07T19:53:57.609102Z",
"url": "https://files.pythonhosted.org/packages/f9/02/aadc0ba3a6330a3de049f1accebaeb3c9d25c6eef917e31569d6d5049da7/redsauce-0.0.0.0a0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dad4cdf726427d3ee1e423a8e470d63377b60a243a9e8ef4f124c758fa960f44",
"md5": "3a71d68ea783d677093121cc0ca53453",
"sha256": "3f53419b3816b23d3f62b2992a48b5791f80f2ec8cf82597412d9166be024a22"
},
"downloads": -1,
"filename": "redsauce-0.0.0.0a0.tar.gz",
"has_sig": false,
"md5_digest": "3a71d68ea783d677093121cc0ca53453",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 10517,
"upload_time": "2024-04-07T19:53:59",
"upload_time_iso_8601": "2024-04-07T19:53:59.163478Z",
"url": "https://files.pythonhosted.org/packages/da/d4/cdf726427d3ee1e423a8e470d63377b60a243a9e8ef4f124c758fa960f44/redsauce-0.0.0.0a0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-07 19:53:59",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "redsauce"
}