随笔 - 358, 文章 - 0, 评论 - 196, 引用 - 0


## 特点: 
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' # 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 # 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.register_saver('my_cache', my_saver)
#manager.register_loader('my_cache', my_loader) # Will print 'Save ... : { 'load': 'faked' }'
cache['new'] = 'real' # Add something to the cache # 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'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


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

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理