怎么用Python注解方式实现缓存数据详解
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,这篇文章主要介绍"怎么用Python注解方式实现缓存数据详解",在日常操作中,相信很多人在怎么用Python注解方式实现缓存数据详解问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对
千家信息网最后更新 2025年11月09日怎么用Python注解方式实现缓存数据详解
这篇文章主要介绍"怎么用Python注解方式实现缓存数据详解",在日常操作中,相信很多人在怎么用Python注解方式实现缓存数据详解问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么用Python注解方式实现缓存数据详解"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
背景
每次加载数据都要重新Load,想通过加入的注解方式开发缓存机制,每次缓存不用写代码了
缺点:目前仅支持一个返回值,虽然能弄成字典,但是已经满足个人需求,没动力改(狗头)。
拿来即用
新建文件 Cache.py
class Cache: def __init__(self, cache_path='.', nocache=False): self.cache_path = cache_path self.cache = not nocache def __call__(self, func): @wraps(func) def wrapper(*args, **kwargs): s = f'{func.__code__.co_filename}.{func.__name__}' s += ','.join(list(args[1:]) + [f'{k}={v}' for k, v in kwargs.items()]) md5 = hashlib.md5() md5.update(s.encode('utf-8')) cache_file = f'{self.cache_path}/{md5.hexdigest()}' if self.cache and os.path.exists(cache_file): print('Loading from cache') return pickle.load(open(cache_file, 'rb')) else: if not os.path.exists(self.cache_path): os.makedirs(self.cache_path) data = func(*args, **kwargs) pickle.dump(data, file=open(cache_file, 'wb')) print(f'Dump finished {cache_file}') return data return wrapperfrom .Cache import Cache@Cache(root_path, nocache=True)def load_data(self, inpath): return 'Wula~a~a~!'
实践过程
第一次,来个简单的继承父类
class Cache(object): def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path else '.' self.cache_path = f'{self.cache_path}/cache' self.data = self.load_cache() def load_cache(self): if os.path.exists(self.cache_path): print('Loading from cache') return pickle.load(open(self.cache_path, 'rb')) else: return None def save_cache(self): pickle.dump(self.data, file=open(self.cache_path, 'wb')) print(f'Dump finished {self.cache_path}')class Filter4Analyzer(Cache): def __init__(self, rootpath, datapath): super().__init__(rootpath) self.root_path = rootpath if self.data is None: self.data = self.load_data(datapath) self.save_cache()只要继承Cache类就可以啦,但是有很多局限,例如只能指定某个参数被cache,例如还得在Filter4Analyzer里面写保存的代码。
下一步,python嵌套装饰器来改善这个问题
from functools import wrapsimport hashlibdef cached(cache_path): def wrapperper(func): @wraps(func) def wrapper(*args, **kwargs): s = f'{func.__code__.co_filename}.{func.__name__}' + ','.join(args[1:]) s += ','.join(list(args[1:]) + [f'{k}={v}' for k, v in kwargs.items()]) md5 = hashlib.md5() md5.update(s.encode('utf-8')) cache_file = f'{cache_path}/{md5.hexdigest()}' if cache_path else './cache' if os.path.exists(cache_file): print('Loading from cache') return pickle.load(open(cache_file, 'rb')) else: if not os.path.exists(cache_path): os.makedirs(cache_path) data = func(*args, **kwargs) pickle.dump(data, file=open(cache_file, 'wb')) print(f'Dump finished {cache_file}') return data return wrapper return wrapperperclass Tester: @cached(cache_path='./workpath_test') def test(self, data_path): return ['hiahia']通过装饰器类简化代码
class Cache: def __init__(self, cache_path='.', nocache=False): self.cache_path = cache_path self.cache = not nocache def __call__(self, func): @wraps(func) def wrapper(*args, **kwargs): s = f'{func.__code__.co_filename}.{func.__name__}' s += ','.join(list(args[1:]) + [f'{k}={v}' for k, v in kwargs.items()]) md5 = hashlib.md5() md5.update(s.encode('utf-8')) cache_file = f'{self.cache_path}/{md5.hexdigest()}' if self.cache and os.path.exists(cache_file): print('Loading from cache') return pickle.load(open(cache_file, 'rb')) else: if not os.path.exists(self.cache_path): os.makedirs(self.cache_path) data = func(*args, **kwargs) pickle.dump(data, file=open(cache_file, 'wb')) print(f'Dump finished {cache_file}') return data return wrapper到此,关于"怎么用Python注解方式实现缓存数据详解"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
缓存
数据
方式
注解
学习
代码
更多
问题
实践
帮助
实用
接下来
不用
个人
动力
参数
字典
文件
文章
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
世界网络安全科技公司简介
数据库之间怎么建立联系
如何在vs里面看见数据库的列
东南大学网络安全怎样
几岁可以学软件开发
l3426上什么服务器主板
网络安全法讲稿
mysql数据库驱动名称
大芒果 已从服务器断开
数据库机房安装工人
深圳市计算机网络安全员
做网络安全的公司
科达telnet服务器
信息系统与数据库技术学习通
巴比特区块链软件开发师
网络安全可信第三方
盐城市于钱网络技术有限公司
图书馆如何使用数据库
java数据库通讯录
网络安全风险排查报告
数据库中文件怎么查看
网络安全公司的职位介绍
手机邮件无法安全地连接到服务器
精工数媒播控服务器
信息系统网络安全分析
服务器怎么区分经过代理的链接
材料性能数据库网站
软件开发和后端开发的区别
信息网络安全责任制
c 数据库多数据修改器