LRU Cache Implementation
Project description
python-lru
Least Recently Used (LRU) Cache implementation
Usage
Instantiate a cache collection object specifying storage parameters. The cache object itself is thread safe. However, depending on the storage backend, it may not be safe to open a cache store multiple times.
from lru import LRUCache, ItemNotCached
# Open cache store (all arguments are optional)
cache = LRUCache(
storage = MemoryStorage() or ShelvedStorage(path=''),
max_size = 1000000,
sizeof = lambda o: len(str(o)),
max_age = timedelta(minutes=15))
# Add items with keys
cache['name'] = "Bob"
cache['age'] = 12
# Check for items in cache
try:
age = cache['age']
except ItemNotCached:
print("No age")
Cache Objects
Cached data can be any variable, and must be cached using a string key. It's up to you to ensure that you don't mutate objects returned from the cache, as storage won't automatically update from changes.
Cache Parameters
The LRUCache container parameters are:
- storage: Where to store cached data. See Storages.
- sizeof: Callable to estimate the size of an object being cached.
- max_size: Maximum size before starting to forget cached items.
- max_age: All cached items will expire after this amount of time.
- storage: Object to use to store cached data
Storages
There are a few storage classes provided. All are inherited from CacheStorage
- MemoryStorage: Caches data in memory
- ShelvedStorage: Caches data in shelve. Really only useful if you're caching large objects.
- Sqlite3Storage: Slowest storage engine, but possibly accessible from multiple processes?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
python-lru-0.2.1.tar.gz
(10.7 kB
view hashes)
Built Distributions
python_lru-0.2.1-py3.8.egg
(35.2 kB
view hashes)
python_lru-0.2.1-py3-none-any.whl
(13.9 kB
view hashes)
Close
Hashes for python_lru-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1a3ab3943644547ffe9e674732ebaf0675e10fdd4459324a9d3e97528ea2964 |
|
MD5 | 88e5df93cf0dde553311a79e9f51bf23 |
|
BLAKE2b-256 | de2d292be3212b1b02cf6402f863e080d6c529cd31c0b62f272fea087586a9ea |