https://github.com/masyagin1998/py-lru-cache
Simple thread-safe caching decorator in Python
https://github.com/masyagin1998/py-lru-cache
Last synced: 1 day ago
JSON representation
Simple thread-safe caching decorator in Python
- Host: GitHub
- URL: https://github.com/masyagin1998/py-lru-cache
- Owner: masyagin1998
- License: mit
- Created: 2025-01-20T02:34:59.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-01-20T04:27:25.000Z (9 months ago)
- Last Synced: 2025-07-31T13:27:03.147Z (2 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# py-lru-cache
`py-lru-cache` is a lightweight Python library providing efficient caching mechanisms through an LRU (Least Recently Used) cache (in fact just a copy-n-paste of some CPython sources). The package includes:
- `lru_cache`: A decorator for caching function calls with LRU strategy.
- `cache`: A decorator over `lru_cache` one without any size limit.## Features
- Efficient caching mechanism using an LRU strategy.
- Easy-to-use decorator and class-based caching.
- Configurable and lightweight.
- Thread-safety and size-safety (LRU).## Installation
Install the package using `poetry` or `pip`:
### Using Poetry
```bash
poetry install
```## Usage
### Using the `lru_cache` Decorator
```python
from py_lru_cache import lru_cache@lru_cache(max_size=3)
def expensive_function(x):
print(f"Calculating {x}")
return x * xprint(expensive_function(2)) # Calculates and caches the result
print(expensive_function(2)) # Returns cached result
```### Using the `cache` Class
```python
from py_lru_cache import cache# Create an LRU cache instance with a maximum size of 2
my_cache = cache(max_size=2)my_cache["a"] = 1
my_cache["b"] = 2
print(my_cache["a"]) # Access the cached valuemy_cache["c"] = 3 # Evicts the least recently used item
print("a" in my_cache) # False, as "a" has been evicted
```## Running the Demo
The package includes a demo in the `lru_cache.py` file.
To run the demo, use:
```bash
python py_lru_cache/lru_cache.py
```## Running Tests
The package includes extensive test coverage in `tests/test_lru_cache.py`.
To run the tests, execute the following command:
```bash
pytest tests/
```## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
---
Start caching smarter with `py-lru-cache`!