Skip to main content

Simple, Flexible, and Extensible caching utility for modern applications

Project description

Modern Python Caching

Cachex is a modern caching library for Python 3 built from the ground up to support type hints. It has built in Redis, MongoDB and Memcached support and provides both sync and async APIs for all storage backends.

Installation

pip install cachex

Redis Dependencies:

pip install cachex[redis]

MongoDB Dependencies:

pip install cachex[mongo]

Memcached Dependencies:

No additional dependencies are required for Memcached support. Unlike Redis and MongoDB, there is not a single client library supported by the core dev team and most libraries do not support both sync and async API's. For that reason, the Memcached storage backend is implemented as an interface based API. You bring your own client, and as long as that client adheres to the MemcachedClient or AsyncMemcachedClient interfaces it will work with Cachex.

Typing Support

Cachex was built to work with type declarations. This means it offers great editor support for decorated functions. It also means that Cachex plays nicely with other popular libraries that rely heavily on typing. FastAPI and Litestar are two popular ASGI web frameworks that offer powerful dependency injection systems which rely on type hints (via Pydantic). Cachex can be inserted anywhere in a dedendency chain and it will "just work". This offers tremendous flexibility to the developer when designing applications.

Basic Example

The example below is very simple (and mostly useless) web server intended to give you a feel for how Cachex works and how it's native typing support can be leveraged in a dependency chain.

from enum import Enum
from typing import Annotated

import pandas as pd
from cachex import cache_value
from fastapi import Depends, FastAPI, Response
from pydantic import AnyHttpUrl
from pydantic_core import Url


app = FastAPI()

class DataSet(Enum):
    UBER1 = "uber1"
    UBER2 = "uber2"
    UBER3 = "uber3"

URLS: dict[DataSet, AnyHttpUrl] = {
    DataSet.UBER1: AnyHttpUrl("https://github.com/plotly/datasets/raw/master/uber-rides-data1.csv"),
    DataSet.UBER2: AnyHttpUrl("https://github.com/plotly/datasets/raw/master/uber-rides-data2.csv"),
    DataSet.UBER3: AnyHttpUrl("https://github.com/plotly/datasets/raw/master/uber-rides-data3.csv"),
}


def get_url(dataset: DataSet) -> AnyHttpUrl:
    """Get the dataset URL from the global mapping."""
    return URLS[dataset]


# We want to cache the result of the download and use that as a dependency.
# Notice, AnyHttpUrl is not natively hashable by cachex so we need to provide a type encoder
@cache_value(type_encoders={Url: lambda t: t.unicode_string()})
def download_csv_data(url: Annotated[AnyHttpUrl, Depends(get_url)]) -> pd.DataFrame:
    """Download a CSV file from the given URL and convert it to a DataFrame."""
    return pd.read_csv(url.unicode_string())


@app.get("/datasets/{dataset}")
def get_dataset(df: Annotated[pd.DataFrame, Depends(download_csv_data)]):
    """Download a CSV dataset from the web and return the data in JSON form."""
    data = df.iloc[0:1000, :].to_json(orient="records", indent=2)
    return Response(content=data, media_type="application/json")

Save the file, call it main.py. Run the app with uvicorn main:app and head to the docs (/docs). The swagger docs show the correct API arguments and types through the dependency chain...

Alt text

Next, try the /datasets/uber1 endpoint. Depending on your internet connection, this may take 2-30 seconds to run. After it runs once, run it again, the response should load almost instantly! You can then repeat this with the /datasets/uber2 and datasets/uber3 endpoints if you'd like.

Documentation

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

cachex-0.1.1.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cachex-0.1.1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file cachex-0.1.1.tar.gz.

File metadata

  • Download URL: cachex-0.1.1.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cachex-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ddef477cc70afa35a2cc4b04ec953b5de5b7c5cfaa6dea4ed461025c026a548
MD5 2c020b1acfd23dec226256d44213edbd
BLAKE2b-256 bbe8b2ec547c9a684788f13bade5d402686fb9a58650cff7c1354a452db90561

See more details on using hashes here.

File details

Details for the file cachex-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cachex-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cachex-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1e01a1e4be2d9d8d1d4dc6677cb231452583c312d56a12f135d614be3bb4aba
MD5 4a74e77d766f754d7c3d20cf83967316
BLAKE2b-256 14419113443316fae34a70da07dfb8b4d2f9fb9694b74013f8608167735f2278

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page