Cache data which access frequently.
Project description
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
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 (张炎泼) drdr.xp@gmail.com
Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
k3cacheable-0.1.0.tar.gz
(4.8 kB
view details)
Built Distribution
File details
Details for the file k3cacheable-0.1.0.tar.gz
.
File metadata
- Download URL: k3cacheable-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23a62ec10a30fa93225de8f3ed25418151f954a14ab22f04da87ced38fe3eef6 |
|
MD5 | 94ab8fe3d28d36b9e92a8c01b7efb5bf |
|
BLAKE2b-256 | 60fb6540da2f7718bede8102fbd8523c8028ba2a5675f433c9dfa87ce0666703 |
File details
Details for the file k3cacheable-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: k3cacheable-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 920411fdafc58788c95bf03b746027e6915e75a989253180d6368d650a9cce79 |
|
MD5 | 635ab39b2978d99033c635118762d6ba |
|
BLAKE2b-256 | c10aeaa10ffa61a7cd94517fa0d77cda473837469467b817ec6f47bd486f818b |