Metadata-Version: 2.4
Name: k3cacheable
Version: 0.1.3
Summary: Cache data which access frequently
Author-email: Zhang Yanpo <drdr.xp@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/pykit3/k3cacheable
Project-URL: Documentation, https://k3cacheable.readthedocs.io
Keywords: cache,cacheable,memoize,pykit3
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: msgpack
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Provides-Extra: publish
Requires-Dist: build; extra == "publish"
Requires-Dist: twine; extra == "publish"
Requires-Dist: pk3; extra == "publish"
Provides-Extra: docs
Requires-Dist: mkdocs>=1.5; extra == "docs"
Requires-Dist: mkdocs-material>=9.0; extra == "docs"
Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
Dynamic: license-file

# k3cacheable

[![Action-CI](https://github.com/pykit3/k3cacheable/actions/workflows/python-package.yml/badge.svg)](https://github.com/pykit3/k3cacheable/actions/workflows/python-package.yml)
[![Documentation Status](https://readthedocs.org/projects/k3cacheable/badge/?version=stable)](https://k3cacheable.readthedocs.io/en/stable/?badge=stable)
[![Package](https://img.shields.io/pypi/pyversions/k3cacheable)](https://pypi.org/project/k3cacheable)

Cache data which access frequently.

k3cacheable is a component of [pykit3] project: a python3 toolkit set.


Cache data which access frequently.




# Install

```
pip install k3cacheable
```

# Synopsis

```python
import time

import k3cacheable

# create a `LRU`, capacity:10 timeout:60
c = k3cacheable.LRU(10, 60)

# set value like the `dict`
c["key"] = "val"

# get value like the `dict`
# if item timeout, delete it and raise `KeyError`
# if item not exist, raise `KeyError`
try:
    val = c["key"]
except KeyError:
    print("key error")

cache_data = {
    "key1": "val_1",
    "key2": "val_2",
}


# define the function with a decorator
@k3cacheable.cache("cache_name", capacity=100, timeout=60, is_deepcopy=False, mutex_update=False)
def get_data(param):
    return cache_data.get(param, "")


# call `get_data`, if item has not been cached, cache the return value
data = get_data("key1")

# call `get_data` use the same param, data will be got from cache
time.sleep(1)
data = get_data("key1")

# if item timeout, when call `get_data`, cache again
time.sleep(1)
data = get_data("key1")


# define a method with a decorator
class MethodCache:
    @k3cacheable.cache("method_cache_name", capacity=100, timeout=60, is_deepcopy=False, mutex_update=False)
    def get_data(self, param):
        return cache_data.get(param, "")


mm = MethodCache()
data = mm.get_data("key2")
```

#   Author

Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>

#   Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>


[pykit3]: https://github.com/pykit3
