Python implementation of a thread-safe LRU cache.
Project description
pyfastlru v. 1.0
Python implementation of a thread-safe LRU cache.
Features: thread safety, statistics, familiar interface.
The MIT License (MIT). Copyright © 2025 Anatoly Petrov petrov.projects@gmail.com
Description
An LRU (Least Recently Used) cache is a data structure that provides quick access to items by key and automatically removes the least recently used items in case of size exhaustion.
These characteristics make the LRU cache ideal for memoizing arbitrary data without the risk of excessive memory usage.
The proposed implementation (LruCache class) utilizes a doubly linked list
to track item usage and a dictionary for the lookup table.
Overview
The LruCache provides a MutableMapping interface similar to the dict.
However, unlike a regular dict, which has no size limits, the cache size
is restricted by the maxsize parameter specified during the initialization
of the cache (defaulting to 128). When the number of items exceeds this limit,
the LruCache automatically removes the least recently used item to make space
for new entries.
Also, unlike a standard dict, which maintains a FIFO (first-in, first-out) order,
the cache employs an MRU (most recently used) order. As a result, all iterators
provided by the LruCache — including those from the __iter__, keys, values,
and items methods — iterate from the MRU (most recently used) item to the LRU
(least recently used) item.
Cache synchronization is handled internally using a reentrant lock,
so no action is needed from the end user. Manual synchronization with the LruCache
context manager or through the acquire and release methods is only necessary
for implementing atomic operations.
The cache gathers usage statistics, including hits, misses, maxsize, and currsize,
which can be accessed using the cache_info() method.
Testing
pyfastlru is tested with pytest framework.
License
pyfastlru is licensed under the MIT License, see LICENSE for more information.
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
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 pyfastlru-1.0.3.tar.gz.
File metadata
- Download URL: pyfastlru-1.0.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79d171fbdedad46948bc50e210a1b8cfcfb40d4339cc85c3474c9ff90fbe92fb
|
|
| MD5 |
e70d1e353c873ce89dadd7d563c5bf79
|
|
| BLAKE2b-256 |
de7c451df4c90d6c6bbd5eadd37d8f8e9c3b4f52578bdf8abfc7f8dd82631950
|
File details
Details for the file pyfastlru-1.0.3-py3-none-any.whl.
File metadata
- Download URL: pyfastlru-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed3f5ab031bfdeaba89b1fe71e3025f11931fafe956187babca546dd9aa40600
|
|
| MD5 |
73ff44b04477f125b7397721933c175f
|
|
| BLAKE2b-256 |
f6eb2591c94c9b8be530e813ed37b5ffb7ab6872ce8b7847563b784e94d9a16c
|