# TTL Cache
A function-level memory cache that supports Time To Live (TTL).
- **Per-function argument caching**: Caching is possible based on the data passed to function arguments. The cache key is composed of the function name, argument names, and the values of the arguments.
- **Automatic expiration**: Cached data expires and is automatically deleted after the specified TTL (in seconds).
- **LRU policy**: When the cache exceeds its maximum size (`max_size`), the Least Recently Used (LRU) policy is applied to delete items.
- **Easy application**: Simply add the `@TtlCache(ttl=seconds)` decorator to the function you want to cache.
## Parameters:
- **ttl**: TTL for the cached data (in seconds).
- **max_size**: Maximum number of cache entries.
- **applying_params**: List of parameter names to use as the cache key. If `None`, all parameters are used. If `[]`, only the function name is used.
## Member Functions:
- **force_expire(key)**: Forces expiration of the cache entry for the specified key.
- **is_exist(key)**: Checks if a specific key exists in the cache.
- **get_item(key)**: Returns the cache item for the specified key.
- *Note*: The key can include partial elements of the cache key.
## Usage:
1. Add the `@TtlCache(ttl=seconds)` decorator to the function you want to cache.
2. Cache keys are generated in the format `"{class_name.}method_name(param1=value1, param2=value2, ...)"`.
3. To call the member functions of `TtlCache`, create an instance of `TtlCache` and use that instance as the decorator.
### Example:
```python
some_cache = TtlCache(ttl=5)
@some_cache
def some_function(x):
return x * 2
@TtlCache(ttl=5, max_size=10, applying_params=['key'])
def another_function(key, value):
return f'{key} = {value}'
# Usage
result = some_function(1)
some_cache.force_expire('some_function(x=1)')
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jogakdal/python_ttl_cache",
"name": "parametric-ttl-cache",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "cache, memory cache, ttl cache, function cache, cache decorator, parametric cache",
"author": "Yongho Hwang",
"author_email": "jogakdal@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/94/e4/7c8feb80d0310a0c033f17cf3b690fec658314cdd99df3c3e98bec60b635/parametric-ttl-cache-0.1.0.tar.gz",
"platform": null,
"description": "# TTL Cache\n\nA function-level memory cache that supports Time To Live (TTL).\n\n- **Per-function argument caching**: Caching is possible based on the data passed to function arguments. The cache key is composed of the function name, argument names, and the values of the arguments.\n- **Automatic expiration**: Cached data expires and is automatically deleted after the specified TTL (in seconds).\n- **LRU policy**: When the cache exceeds its maximum size (`max_size`), the Least Recently Used (LRU) policy is applied to delete items.\n- **Easy application**: Simply add the `@TtlCache(ttl=seconds)` decorator to the function you want to cache.\n\n## Parameters:\n\n- **ttl**: TTL for the cached data (in seconds).\n- **max_size**: Maximum number of cache entries.\n- **applying_params**: List of parameter names to use as the cache key. If `None`, all parameters are used. If `[]`, only the function name is used.\n\n## Member Functions:\n\n- **force_expire(key)**: Forces expiration of the cache entry for the specified key.\n- **is_exist(key)**: Checks if a specific key exists in the cache.\n- **get_item(key)**: Returns the cache item for the specified key.\n - *Note*: The key can include partial elements of the cache key.\n\n## Usage:\n\n1. Add the `@TtlCache(ttl=seconds)` decorator to the function you want to cache.\n2. Cache keys are generated in the format `\"{class_name.}method_name(param1=value1, param2=value2, ...)\"`.\n3. To call the member functions of `TtlCache`, create an instance of `TtlCache` and use that instance as the decorator.\n\n### Example:\n```python\nsome_cache = TtlCache(ttl=5)\n\n@some_cache\ndef some_function(x):\n return x * 2\n\n@TtlCache(ttl=5, max_size=10, applying_params=['key'])\ndef another_function(key, value):\n return f'{key} = {value}'\n\n# Usage\nresult = some_function(1)\nsome_cache.force_expire('some_function(x=1)')\n```\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A function-level memory cache that supports Time To Live (TTL)",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/jogakdal/python_ttl_cache"
},
"split_keywords": [
"cache",
" memory cache",
" ttl cache",
" function cache",
" cache decorator",
" parametric cache"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ac0d4ae9b2625a2c922b006966e0b6be6b5770d9fe02a435e5a14fd939098459",
"md5": "b2dc721b183333a65143f5c0c0f8ba80",
"sha256": "ad62abeed5aa0e0a2add5e6256f11dec459e1471ed0597d861a3fd9fb12ea38f"
},
"downloads": -1,
"filename": "parametric_ttl_cache-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b2dc721b183333a65143f5c0c0f8ba80",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6622,
"upload_time": "2024-06-19T00:54:31",
"upload_time_iso_8601": "2024-06-19T00:54:31.830997Z",
"url": "https://files.pythonhosted.org/packages/ac/0d/4ae9b2625a2c922b006966e0b6be6b5770d9fe02a435e5a14fd939098459/parametric_ttl_cache-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "94e47c8feb80d0310a0c033f17cf3b690fec658314cdd99df3c3e98bec60b635",
"md5": "75ff4015bb550577614ab3bd26bd2c1e",
"sha256": "c3e27228f6ddff473c65e6be8abfc616469d0d5fd9a901e814f87ad0574494b6"
},
"downloads": -1,
"filename": "parametric-ttl-cache-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "75ff4015bb550577614ab3bd26bd2c1e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3906,
"upload_time": "2024-06-19T00:54:34",
"upload_time_iso_8601": "2024-06-19T00:54:34.225257Z",
"url": "https://files.pythonhosted.org/packages/94/e4/7c8feb80d0310a0c033f17cf3b690fec658314cdd99df3c3e98bec60b635/parametric-ttl-cache-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-19 00:54:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jogakdal",
"github_project": "python_ttl_cache",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "parametric-ttl-cache"
}