https://github.com/parisaalizadeh2003/python-caching-decorator
This project implements a Caching Decorator in Python that stores function results. When the function is called again with the same arguments, the cached result is returned instead of recalculating it, improving performance.
https://github.com/parisaalizadeh2003/python-caching-decorator
chaching memorization performance-optimization python pythondecorator pythontips
Last synced: 6 months ago
JSON representation
This project implements a Caching Decorator in Python that stores function results. When the function is called again with the same arguments, the cached result is returned instead of recalculating it, improving performance.
- Host: GitHub
- URL: https://github.com/parisaalizadeh2003/python-caching-decorator
- Owner: ParisaAlizadeh2003
- License: mit
- Created: 2025-03-17T17:15:01.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-03-17T17:21:25.000Z (7 months ago)
- Last Synced: 2025-04-12T00:50:04.640Z (6 months ago)
- Topics: chaching, memorization, performance-optimization, python, pythondecorator, pythontips
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Caching Decorator in Python
This project implements a **Caching Decorator** in Python. The decorator stores the results of a function's computation. When the function is called again with the same arguments, it returns the cached result instead of recalculating it, improving performance.
## How It Works
1. **Caching Decorator**: The `Caching` decorator wraps the original function and stores its results in a dictionary.
2. **Argument Handling**: The arguments passed to the function are converted into a tuple because lists are not hashable and can't be used as dictionary keys.
3. **Caching Logic**:
- When the function is called with certain arguments, the decorator first checks if the result for those arguments already exists in the cache (dictionary).
- If the result is found in the cache, it is returned immediately.
- If the result is not found, the function is executed, and the result is stored in the cache for future use.## Why Use a Dictionary for Caching?
This implementation uses a **dictionary (`dict`) for caching** because dictionaries in Python are highly optimized for **hash table lookups**. Since dictionary keys are hashed, retrieving a cached result is **O(1)** on average, making it extremely fast.
Using a dictionary for caching ensures:
- **Fast lookups** due to hash-based indexing.
- **Efficient memory usage** as only required results are stored.
- **Performance optimization** by avoiding redundant computations.Since lists are **mutable and unhashable**, they cannot be used as dictionary keys. Instead, function arguments are converted into **tuples**, which are immutable and hashable, allowing them to be stored efficiently as dictionary keys.
## Example Usage
```python
@Caching
def Sum(*args):
return sum(*args)print(Sum([1, 4])) # Computed and cached
print(Sum([2, 4])) # Computed and cached
print(Sum([1, 4])) # Cached result returned
print(Sum([3, 4])) # Computed and cached