{"id":27299957,"url":"https://github.com/parisaalizadeh2003/python-caching-decorator","last_synced_at":"2025-04-12T00:50:12.000Z","repository":{"id":282929628,"uuid":"950129384","full_name":"ParisaAlizadeh2003/Python-Caching-Decorator","owner":"ParisaAlizadeh2003","description":"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.","archived":false,"fork":false,"pushed_at":"2025-03-17T17:21:25.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T00:50:04.640Z","etag":null,"topics":["chaching","memorization","performance-optimization","python","pythondecorator","pythontips"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ParisaAlizadeh2003.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-17T17:15:01.000Z","updated_at":"2025-03-17T17:21:29.000Z","dependencies_parsed_at":"2025-03-17T18:39:58.325Z","dependency_job_id":null,"html_url":"https://github.com/ParisaAlizadeh2003/Python-Caching-Decorator","commit_stats":null,"previous_names":["parisaalizadeh2003/python-caching-decorator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FPython-Caching-Decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FPython-Caching-Decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FPython-Caching-Decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FPython-Caching-Decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ParisaAlizadeh2003","download_url":"https://codeload.github.com/ParisaAlizadeh2003/Python-Caching-Decorator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501902,"owners_count":21114681,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chaching","memorization","performance-optimization","python","pythondecorator","pythontips"],"created_at":"2025-04-12T00:50:11.423Z","updated_at":"2025-04-12T00:50:11.995Z","avatar_url":"https://github.com/ParisaAlizadeh2003.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Caching Decorator in Python\n\nThis 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.\n\n## How It Works\n\n1. **Caching Decorator**: The `Caching` decorator wraps the original function and stores its results in a dictionary.\n2. **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.\n3. **Caching Logic**:\n   - When the function is called with certain arguments, the decorator first checks if the result for those arguments already exists in the cache (dictionary).\n   - If the result is found in the cache, it is returned immediately.\n   - If the result is not found, the function is executed, and the result is stored in the cache for future use.\n\n## Why Use a Dictionary for Caching?\n\nThis 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. \n\nUsing a dictionary for caching ensures:\n- **Fast lookups** due to hash-based indexing.\n- **Efficient memory usage** as only required results are stored.\n- **Performance optimization** by avoiding redundant computations.\n\nSince 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.\n\n## Example Usage\n\n```python\n@Caching\ndef Sum(*args):\n    return sum(*args)\n\nprint(Sum([1, 4]))  # Computed and cached\nprint(Sum([2, 4]))  # Computed and cached\nprint(Sum([1, 4]))  # Cached result returned\nprint(Sum([3, 4]))  # Computed and cached\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisaalizadeh2003%2Fpython-caching-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparisaalizadeh2003%2Fpython-caching-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisaalizadeh2003%2Fpython-caching-decorator/lists"}