Skip to main content

Library that allows you to compose your own cache, pre-packaged with many useful caches

Project description

██████╗ █████╗  ██████╗██╗  ██╗██████╗   
██╔════╝██╔══██╗██╔════╝██║  ██║██╔══██╗  
██║     ███████║██║     ███████║██████╔╝  
██║     ██╔══██║██║     ██╔══██║██╔══██╗  
╚██████╗██║  ██║╚██████╗██║  ██║██║  ██║  
╚═════╝╚═╝  ╚═╝ ╚═════╝╚═╝  ╚═╝╚═╝  ╚═╝  

cachr: superfast, composable caching for Python

Testing Codecov codecov
Package PyPI Latest Release PyPI Downloads
Meta GitHub License

cachr is a Python package that provides superfast, composable caches designed to optimize your applications in an easy and intuitive way. It aims to be the go-to package to use and build caches that make your applications superfast, memory-efficient and user-friendly.

pip install cachr

Table of Contents

Main Features

  • 🐍 Pure Python
  • 🖌 Easily extendable
  • 👨‍🎨 User-friendly

Usage Example

At the moment there are five caches available for use:

  • LFUCache: Removes the least frequently used item in the cache when it overflows
  • LRUCache: A Least Recently Used Cache removes the least recently used item when it overflows
  • RandomReplaceCache: randomly expells an item from the cache when it overflows
  • TTLCache: A Time-To-Live-cache acts like a LRUCache and also removes an entry from the cache after a certain expiration time
  • SlidingWindowCache: Like a TTLCache but it renews the expiration date eacht time an item is requested from the cache.

All caches are added to your code in two ways: by decorator or as a regular object. Decorator:

import time
from cachr import LRUCache


# 1. Decorate your function
@LRUCache(capacity=2)
def add(i, y):
    time.sleep(1)
    print("adding..")
    return i + y


print(add(1, 2))    # <-- takes a second to calculate
print(add(1, 2))    # <-- takes value from cache immediately 
print(add(1, 2))    # <-- takes value from cache immediately 

Object:

from cachr import TTLCache

my_cache =  TTLCache(capacity=25, ttl_seconds=60)

def very_expensive_function(number:int) -> int:
    return number * number

for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 5, 7, 9, 1, 1, 1, 1]:
    value_from_cache = my_cache.get(i)
    if value_from_cache is not None:
        print(f"Answer for {i} is {value_from_cache}")
        continue
    print(f"calculating expensive function for {i}..")
    my_cache.put(key=i, value=very_expensive_function(number=i))

Installation

pip install cachr

The source code is currently hosted on GitHub at: https://github.com/mike-huls/cachr

Binary installers for the latest released version are available at the Python Package Index (PyPI).

Dependencies

Cachr has no Python dependencies

License

MIT

Documentation

🔨 Under construction

Development

Find the changelog and list of upcoming features here.
Contributions are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!


Go to Top

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

cachr-0.0.7.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

cachr-0.0.7-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file cachr-0.0.7.tar.gz.

File metadata

  • Download URL: cachr-0.0.7.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.1.0-26-amd64

File hashes

Hashes for cachr-0.0.7.tar.gz
Algorithm Hash digest
SHA256 d934110c3941d49364209c0c2ce73bb5b35c0af811e3cf336250d8c9a46d8fef
MD5 0b3078d3e649c66d4bb141257e1a85c0
BLAKE2b-256 340c3ee50f86e39dcc3c83a411ca6bdf36c31e4c5fb88a002fb858825d23f662

See more details on using hashes here.

File details

Details for the file cachr-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: cachr-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.1.0-26-amd64

File hashes

Hashes for cachr-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 69e1814c2b7f6b2be0da7255bc7b5b73532187e7841cddb047577775dc4e3688
MD5 b4bf9c08a61051855c3f8f43057a14b2
BLAKE2b-256 c48bafb9cbdcf967ba08b842e265b430b66d4454c31e5cb2f8e9c7b7157d514d

See more details on using hashes here.

Supported by

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