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 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)

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.4.0.tar.gz (25.6 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.4.0-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cachetoolz-0.4.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for cachetoolz-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0d363ead5bcd123ddabb534579f6189ec61f4bde6acc7c834916b6fddafe6290
MD5 3f1b677467a0b4c12cd1ab90b72025b0
BLAKE2b-256 910e136542a28fefc5760766c626eaffe99f89aaf30b8c3ba635ee4c89a05ecd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cachetoolz-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for cachetoolz-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78a05e32152d5d38b4c496ab12993b9b19fd77f84906a062e2bb4a8e6b070957
MD5 5c82b24e5632f5fec4f9bb2208c6a866
BLAKE2b-256 a83c950027f21b89e9b52e42a8bd4d384e1b769690a3e84d54ad46d3df1585ee

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