Skip to main content

This library provides a decorator for caching functions

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Cache Toolz

License MIT CI codecov Supported Python versions PyPI version Downloads Documentation Changelog Issue Tracker Contributing

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)

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

cachetoolz-0.3.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

cachetoolz-0.3.2-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file cachetoolz-0.3.2.tar.gz.

File metadata

  • Download URL: cachetoolz-0.3.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-39-generic

File hashes

Hashes for cachetoolz-0.3.2.tar.gz
Algorithm Hash digest
SHA256 adbc60ccfee8719310b9d050531e86cdf91512f35aef0181ff956ee7643942ae
MD5 cd59fd9373b4ae2f65495599c3012f7b
BLAKE2b-256 29d263bd645c9c6faeac03e959e4ee8af45c2d41135f0becee6a031784bdc8e2

See more details on using hashes here.

File details

Details for the file cachetoolz-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: cachetoolz-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-39-generic

File hashes

Hashes for cachetoolz-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 37f897857d5cceb01b3c88d8dd2eb4e074ba057df6a016777a9104059d0b22d9
MD5 92d16a8a416a3aba18e39f6234d77b39
BLAKE2b-256 ed06468232a50dff5322783a89d1ad385911e7989215345ba250d107112f420f

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