Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Cache Toolz

License GPLv3+ Supported Python versions PyPI version Downloads Documentation Changelog Issue Tracker Contributing Built with Material for MkDocs

This library offers a decorator that enhances the functionality of caching functions.

Caching is a technique commonly used in software development to improve performance by storing the results of expensive or time-consuming function calls. With this library, you can easily apply caching to your functions using a decorator.

The decorator provided by this library automatically checks if the function has been called with the same set of arguments before. If it has, instead of executing the function again, it returns the cached result, saving valuable processing time. However, if the function is called with new or different arguments, it will execute normally and cache the result for future use.

By incorporating this caching decorator into your code, you can optimize the execution of functions that involve complex computations, database queries, API calls, or any other operations that could benefit from caching.

Overall, this library simplifies the implementation of caching in your applications, allowing you to enhance performance and reduce resource consumption effectively.

Installation

Install and update using pip:

pip install cachetoolz

A Example

from dataclasses import asdict, dataclass, field
from datetime import timedelta
from uuid import UUID, uuid4

from cachetoolz import cache
from cachetoolz.coder import coder

TODOS: list['Todo'] = []

@dataclass
class Todo:
    title: str
    status: bool = field(default=False, compare=False)
    id: UUID = field(default_factory=uuid4, compare=False)

# Registering an object coder
@coder.register
class TodoSerializer:
    def encode(self, value: Todo):  # Need type annotated
        return asdict(value)

    def decode(self, value):
        return Todo(**value)

# Adding cache to function with expiry time in seconds
@cache(ttl=120, namespace='todo')
def get_one(id: UUID):
    return next(filter(lambda todo: todo.id == id, TODOS), None)

# Adding expiry time using timedelta
@cache(ttl=timedelta(minutes=30), namespace='todo')
def get_all():
    return TODOS

# Clear all caches on given namesoaces
@cache.clear(namespaces=['todo'])
def add_one(title, status=False):
    if (todo := Todo(title=title, status=status)) not in TODOS:
        TODOS.append(todo)

Metadata

Release files for cachetoolz 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cachetoolz 0.4.1
File Size Uploaded
cachetoolz-0.4.1.tar.gz 26.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cachetoolz 0.4.1
File Interpreter ABI Platform
cachetoolz-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 56.4 kB

Release files / cachetoolz-0.4.1.tar.gz

Download URL cachetoolz-0.4.1.tar.gz
Size 26.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f714e26caa3f26e7eed75b7d0d2cf293b72704a17efa08f47c04bfa1423cbe0
BLAKE2b-256 checksum
How to use checksums
a8e6f51e8991e6380cd496d1265596c5b50f460c46eecac00bfad6c2e9de96b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.2

Release files / cachetoolz-0.4.1-py3-none-any.whl

Download URL cachetoolz-0.4.1-py3-none-any.whl
Size 30.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e94db8a5e253c1aa20e797bbc3ffff7d03e3e91222b705cfa30cce3362aa7c10
BLAKE2b-256 checksum
How to use checksums
6430ae4ca70e4e20154f88ce12d31adabf0cabcdc39ec4167961f1e63566e211
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.2
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page