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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
opticache-0.1.4.tar.gz
(9.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opticache-0.1.4.tar.gz.
File metadata
- Download URL: opticache-0.1.4.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe709d0d1d3a2b978f3bb9164c3c897ebf856f61f97cbbc90855654083dca626
|
|
| MD5 |
2097c08d25554b29e5a885bf3d02d551
|
|
| BLAKE2b-256 |
08e6b2e498f7e9f3bb9521f5b54a08bb69ece463e6c61ce15214d276023d6bb5
|
File details
Details for the file opticache-0.1.4-py3-none-any.whl.
File metadata
- Download URL: opticache-0.1.4-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fa9991913b44738bb9c6c3778e9e72aae7de7a3ea269b338cc853bc5e10a3da
|
|
| MD5 |
bd5110e21496f6da3a47504ee563acc5
|
|
| BLAKE2b-256 |
05fec03de89c965be308adf588abf879a7be1b5c7572adfa6d5acda86aa0ad61
|