opticache
A Python caching library with pluggable eviction strategies.
Installation
pip install opticache
Usage
from opticache import Cache
from opticache.strategies import (
LRUStrategy,
LFUStrategy,
FIFOStrategy,
FIFOStrategyFastDelete,
RandomStrategy,
SIEVEStrategy,
)
cache_lru = Cache(LRUStrategy, capacity=500)
cache_lfu = Cache(LFUStrategy, capacity=500)
cache_fifo = Cache(FIFOStrategy, capacity=500)
cache_sieve = Cache(SIEVEStrategy, capacity=500)
cache_lru.set(key, value)
cache_lru[key] = value
cache_lru.get(key)
value = cache_lru[key]
cache_lru.delete(key)
cache_lru.clear()
for i in cache_lru:
print(i)
@cache_lru.memoize()
def foo(x):
return x * 2
foo(10) # Cache miss, computes result
foo(10) # Cache hit, returns cached result
Custom strategy
Implement the EvictionStrategy ABC to create your own:
from opticache.strategies.base import EvictionStrategy
class MyStrategy(EvictionStrategy):
def add(self, key):
"""Track a new key."""
...
def access(self, key):
"""Update tracking when a key is accessed."""
...
def evict(self):
"""Return the key to evict."""
...
def remove(self, key):
"""Remove a key from tracking (manual delete)."""
...
from opticache import Cache
cache = Cache(MyStrategy, capacity=1000)
Strategies
| Strategy | Evicts | Best for |
|---|---|---|
LRUStrategy |
Least recently used key | General purpose, rapidly changing access patterns |
LFUStrategy |
Least frequently used key | Stable popularity distributions (e.g. Zipf) |
FIFOStrategy |
Oldest key | Simple workloads, high throughput |
FIFOStrategyFastDelete |
Oldest key | FIFO with frequent cache.delete() calls |
RandomStrategy |
Random key | Unpredictable access patterns, low overhead |
SIEVEStrategy |
Unvisited key via moving hand | Web caches, CDNs, filtering one-hit wonders |
See the Github repository for more details and tests.
Metadata
Release files for opticache 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opticache-0.1.4.tar.gz | 9.7 kB | Without provenance | Details for opticache-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| opticache-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for opticache-0.1.4-py3-none-any.whl |
Total release size: 19.0 kB
Release files / opticache-0.1.4.tar.gz
| Download URL | opticache-0.1.4.tar.gz |
|---|---|
| Size | 9.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe709d0d1d3a2b978f3bb9164c3c897ebf856f61f97cbbc90855654083dca626
|
|
BLAKE2b-256 checksum How to use checksums |
08e6b2e498f7e9f3bb9521f5b54a08bb69ece463e6c61ce15214d276023d6bb5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / opticache-0.1.4-py3-none-any.whl
| Download URL | opticache-0.1.4-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2fa9991913b44738bb9c6c3778e9e72aae7de7a3ea269b338cc853bc5e10a3da
|
|
BLAKE2b-256 checksum How to use checksums |
05fec03de89c965be308adf588abf879a7be1b5c7572adfa6d5acda86aa0ad61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Security for opticache 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.