Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 330, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

py_cache_manager使用简介

## 特点: 
一、持久化缓存
from cacheman import cacher
manager = cacher.get_cache_manager() # Optional manager name argument can be used here
cache = manager.register_cache('my_simple_cache') # You now have a cache!
print cache.get('my_key') # `None` first run, 'my_value' if this code was executed earlier
cache['my_key'] = 'my_value'
cache.save() # Changes are now persisted to disk
manager.save_cache_contents('my_simple_cache') # Alternative way to save a cache
二、非持久化缓存
from cacheman import cacher
manager = cacher.get_cache_manager()
cache = manager.register_custom_cache('my_simple_cache', persistent=False) # You cache won't save to disk
cache.save() # This is a no-op
三、注册钩子
from cacheman import cacher
from cacheman import cachewrap
def my_saver(cache_name, contents):
    print("Save requested on {} cache content: {}".format(cache_name, contents))
def my_loader(cache_name):
    return { 'load': 'faked' }
manager = cacher.get_cache_manager()
cache = cachewrap.PersistentCache('my_cache', saver=my_saver, loader=my_loader)
# Can also use manager to set savers/loaders
#manager.retrieve_cache('my_cache')
#manager.register_saver('my_cache', my_saver)
#manager.register_loader('my_cache', my_loader)
cache.save() # Will print 'Save ... : { 'load': 'faked' }'
cache['new'] = 'real' # Add something to the cache
cache.save() # Will print 'Save ... : { 'load': 'faked', 'new': 'real' }'
四、直接设置
from cacheman import cacher
# Default cache directory is '/tmp/general_cacher' or 'user\appadata\local\temp\general_cache'
# All pickle caches now save to namespaced directories within the base_cache_directory directory
manager = cacher.get_cache_manager(base_cache_directory='secret/cache/location')
cache = manager.register_cache('my_cache')
cache['new'] = 'real' # Add something to the cache
cache.save('my_cache') # Will save contents to 'secret/cache/location/general_cache/my_cache.pkl'
五、依赖缓存
from cacheman import cacher
manager = cacher.get_cache_manager()
edge_cache = manager.retrieve_cache('edge_cache')
root_cache = manager.register_cache('root_cache')
manager.register_dependent_cache('root_cache', 'edge_cache')
def set_processed_value():
    # Computes and caches 'processed' from root's 'raw' value
    processed = edge_cache.get('processed')
    if processed is None:
        processed = (root_cache.get('raw') or 0) * 5
        edge_cache['processed'] = processed
    return processed
# A common problem with caching computed or dependent values:
print set_processed_value() # 0 without raw value
root_cache['raw'] = 1
print set_processed_value() # still 0 because it's cache in edge
# Now we use cache invalidation to tell downstream caches they're no longer valid
root_cache.invalidate() # Invalidates dependent caches
print edge_cache # Prints {} even though we only invalidated the root_cache
root_cache['raw'] = 1
print set_processed_value() # Now 5 because the edge was cleared before the request
print edge_cache # Can see {'processed': 5} propogated

附注:安装使用压缩包里setup.py
py_cache_manager.rar

posted on 2021-09-11 12:10 Benjamin 阅读(21) 评论(0)  编辑 收藏 引用 所属分类: python


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理