https://github.com/neeru1207/selective_lru_cache_decorator
A Python Decorator similar to but simpler than functools.lru_cache with the extra ability to select parameters
https://github.com/neeru1207/selective_lru_cache_decorator
functools hacktoberfest lru-cache memoize-decorator python-decorator
Last synced: 10 months ago
JSON representation
A Python Decorator similar to but simpler than functools.lru_cache with the extra ability to select parameters
- Host: GitHub
- URL: https://github.com/neeru1207/selective_lru_cache_decorator
- Owner: neeru1207
- Created: 2020-08-09T09:37:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-11T05:31:37.000Z (over 5 years ago)
- Last Synced: 2025-02-09T22:16:27.765Z (over 1 year ago)
- Topics: functools, hacktoberfest, lru-cache, memoize-decorator, python-decorator
- Language: Python
- Homepage:
- Size: 147 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Selective_LRU_Cache_Decorator
A Python Decorator similar to but much simpler than functools.lru_cache with the extra ability to select parameters. It can be used to memoize recursive functions.
## Usage
* Clone this repository and place the lru_cache module in the working directory.
* Import the LRU Cache in your Python code by typing the below command.
```python
from lru_cache.lru_cache import SelectiveLRUCache
```
* Decorate a function with the cache by placing the below command just above the function definition. The decorator takes two parameters - maxsize (the maximum size of the LRU Cache) and parameters. The "parameters" parameter is a lambda function which decides which parameters to select. In the below example, the "parameters" lambda selects only the first parameter.
```python
@SelectiveLRUCache(parameters=lambda x:(x[0],), maxsize=None)
def fibonacci(n, cntr):
```
* The "parameters" lambda function takes the list of parameters as input and outputs a tuple of the selected list of parameters. Here the lambda selects the first and third parameters from the list of parameters.
```python
@SelectiveLRUCache(parameters=lambda x:(x[0], x[2]))
```
## Performance
* When tested on the recursive fibonacci function, a **46000 times** faster execution and a **29000 times** reduction in the number of function calls was achieved.
* Fibonacci(40) without the cache took *331160289* function calls and *92.686* seconds as shown below:

* Fibonacci(40) with the cache took *1140* function calls and only *0.002* seconds to execute as shown below:

## Contributing
* Feel free to open an issue if any bug is found.
* Pull requests are welcome. Just make sure to contribute readable, well commented, and tested code.