https://github.com/pykit3/k3cacheable
Cache data which access frequently.
https://github.com/pykit3/k3cacheable
cache python
Last synced: 4 months ago
JSON representation
Cache data which access frequently.
- Host: GitHub
- URL: https://github.com/pykit3/k3cacheable
- Owner: pykit3
- License: mit
- Created: 2021-09-02T06:13:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-08-28T12:35:54.000Z (9 months ago)
- Last Synced: 2025-09-22T16:10:51.462Z (8 months ago)
- Topics: cache, python
- Language: Python
- Homepage: https://blog.openacid.com
- Size: 34.2 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k3cacheable
[](https://github.com/pykit3/k3cacheable/actions/workflows/python-package.yml)
[](https://k3cacheable.readthedocs.io/en/stable/?badge=stable)
[](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 k3cacheable
import time
# 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(object):
@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 (张炎泼)
# Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼)
[pykit3]: https://github.com/pykit3