Skip to main content

The fastest memoizing and caching Python library written in Rust

Project description

Cachebox

Cachebox is a Python library (written in Rust) that provides memoizations and cache implementations with different cache replecement policies.

This library is faster than other libraries and uses lower memory than them, you can see benchmarks here.

from cachebox import cached, TTLCache, LRUCache

# Keep coin price for no longer than a minute
@cached(TTLCache(maxsize=126, ttl=60))
def get_coin_price(coin_name):
    return web3_client.get_price(coin_name)

# Async functions are supported
@cached(LRUCache(maxsize=126))
async def get_coin_price(coin_name):
    return await async_web3_client.get_price(coin_name)

# You can pass `capacity` parameter.
# If `capacity` specified, the cache will be able to hold at least capacity elements without reallocating.
@cached(LRUCache(maxsize=126, capacity=100))
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

Page Content:

What is caching and why to use it?

Wikipeda:

In computing, caching improves performance by keeping recent or often-used data items in memory locations which are faster, or computationally cheaper to access, than normal memory stores. When the cache is full, the algorithm must choose which items to discard to make room for new data.

Researchgate:

Cache replacement policies play important roles in efficiently processing the current big data applications. The performance of any high performance computing system is highly depending on the performance of its cache memory.

Features

Pros:

  • Thread-safe (uses Rusts RwLock)
  • sync and async compatible
  • Various cache algorithms (supports 8 cache algorithms)
  • Super fast (is written in Rust language)
  • High performance

Cons:

  • Does not support iterating (for values, keys and items methods)

Supported:

  • Cache: Simple cache implementation with no policy and algorithm.
  • FIFOCache: First In First Out cache implementation.
  • LFUCache: Least Frequently Used cache implementation.
  • RRCache: Random Replacement cache implementation.
  • LRUCache: Least Recently Used cache implementation.
  • MRUCache: Most Recently Used cache implementation.
  • TTLCache: LRU Cache Implementation With Per-Item TTL Value.
  • TTLCacheNoDefault: Time-aware Cache Implmenetation; With this cache, you can set its own expiration time for each key-value pair.

Installation

You can install cachebox from PyPi:

pip3 install -U cachebox

Tutorial

This package is very easy to use. You can use all implementations like a dictionary; they supported all collections.MutableMapping methods. But there are some new methods you can see in examples.

At first, think about which algorithm do you want to use? import and use it like a dictionary; In this examples we used LRUCache, TTLCache and TTLCacheNoDefault.

LRUCache example:

from cachebox import LRUCache

cache = LRUCache(10)
cache.insert("key", "value") # or cache["key"] = "value"
cache.delete("key") # or `del cache["key"]`

# `.clear()` method has new parameter `reuse`
# pass True to keeps the allocated memory for reuse (default False).
cache.clear(reuse=True)

And there are new methods for TTLCache and TTLCacheNoDefault. You can see those methods in these examples:

TTLCache example:

from cachebox import TTLCache

cache = TTLCache(10, ttl=2)
cache.insert(1, "value1") # or cache[1] = "value1"
cache.insert(2, "value2") # or cache[2] = "value2"
cache.insert(3, "value3") # or cache[3] = "value3"

# It works like `.get()` with the difference that it returns the expiration of item in seconds.
cache.get_with_expire(1)
# Output: ('value1', 1.971873426437378)

# It works like `.popitem()` with the difference that it returns the expiration of item in seconds.
cache.popitem_with_expire()
# Output: (1, 'value1', 1.961873426437378)

# It works like `.pop()` with the difference that it returns the expiration of item in seconds.
cache.pop_with_expire(2)
# Output: ('value2', 1.951873426437378)

# Calling this method removes all items whose time-to-live would have expired by time,
# and if `reuse` be True, keeps the allocated memory for reuse (default False).
cache.expire(reuse=False)

TTLCacheNoDefault example:

from cachebox import TTLCacheNoDefault

# TTLCacheNoDefault have not ttl parameter here.
cache = TTLCacheNoDefault(10)
cache.insert(1, "value1", ttl=10) # this key-pair is available for no longer than 10 seconds
cache.insert(2, "value2", ttl=2) # this key-pair is available for no longer than 2 seconds
cache.setdefault(3, "value3", ttl=6) # this key-pair is available for no longer than 6 seconds
cache.insert(4, "value4", ttl=None) # this key-pair never expire

# It works like `.get()` with the difference that it returns the expiration of item in seconds.
cache.get_with_expire(1)
# Output: ('value1', 9.971873426437378)

# It works like `.popitem()` with the difference that it returns the expiration of item in seconds.
cache.popitem_with_expire()
# Output: (2, 'value2', 1.961873426437378)

# It works like `.pop()` with the difference that it returns the expiration of item in seconds.
cache.pop_with_expire(4) 
# Output: ('value4', 0.0)

Frequently asked questions

What is the difference between TTLCache and TTLCacheNoDefault?

In TTLCache, you set an expiration time for all items, but in TTLCacheNoDefault, you can set a unique expiration time for each item.

TTL Speed
TTLCache One ttl for all items TTLCache is very faster than TTLCacheNoDefault
TTLCacheNoDefault Each item has unique expiration time TTLCacheNoDefault is very slow in inserting

Can we set maxsize to zero?

Yes, if you pass zero to maxsize, means there's no limit for items.

I use cachetools, how to change it to cachebox?

cachebox syntax is very similar to cachetools. Just change these items:

# If you use `isinstance` for cachetools classes, change those.
isinstance(cache, cachetools.Cache) -> isinstance(cache, cachebox.BaseCacheImpl)

# If you pass `None` to `cached()`, change it to `dict`.
@cachetools.cached(None) -> @cachebox.cached({})

# If you use `cache.maxsize`, change it to `cache.getmaxsize()`
cache.maxsize -> cache.getmaxsize()

License

Copyright (c) 2024 aWolverP - MIT License

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

cachebox-1.0.23.tar.gz (23.9 kB view hashes)

Uploaded Source

Built Distributions

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

cachebox-1.0.23-cp312-none-win_amd64.whl (282.2 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

cachebox-1.0.23-cp312-none-win32.whl (262.3 kB view hashes)

Uploaded CPython 3.12 Windows x86

cachebox-1.0.23-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

cachebox-1.0.23-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

cachebox-1.0.23-cp312-cp312-macosx_11_0_arm64.whl (395.4 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

cachebox-1.0.23-cp312-cp312-macosx_10_12_x86_64.whl (406.6 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

cachebox-1.0.23-cp311-none-win_amd64.whl (287.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

cachebox-1.0.23-cp311-none-win32.whl (264.9 kB view hashes)

Uploaded CPython 3.11 Windows x86

cachebox-1.0.23-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

cachebox-1.0.23-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

cachebox-1.0.23-cp311-cp311-macosx_11_0_arm64.whl (397.2 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

cachebox-1.0.23-cp311-cp311-macosx_10_12_x86_64.whl (415.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

cachebox-1.0.23-cp310-none-win_amd64.whl (287.6 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cachebox-1.0.23-cp310-none-win32.whl (264.9 kB view hashes)

Uploaded CPython 3.10 Windows x86

cachebox-1.0.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

cachebox-1.0.23-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

cachebox-1.0.23-cp310-cp310-macosx_11_0_arm64.whl (397.2 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

cachebox-1.0.23-cp310-cp310-macosx_10_12_x86_64.whl (415.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

cachebox-1.0.23-cp39-none-win_amd64.whl (287.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cachebox-1.0.23-cp39-none-win32.whl (265.2 kB view hashes)

Uploaded CPython 3.9 Windows x86

cachebox-1.0.23-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

cachebox-1.0.23-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

cachebox-1.0.23-cp38-none-win_amd64.whl (287.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cachebox-1.0.23-cp38-none-win32.whl (265.3 kB view hashes)

Uploaded CPython 3.8 Windows x86

cachebox-1.0.23-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cachebox-1.0.23-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

cachebox-1.0.23-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

cachebox-1.0.23-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARMv7l

cachebox-1.0.23-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

cachebox-1.0.23-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

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