Python 注解方式实现缓存数据详解
作者:liuxing93619 发布时间:2023-07-15 22:11:22
标签: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 wrapper
from .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 wraps
import hashlib
def 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 wrapperper
class 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 函数装饰器
Python函数属性和PyCodeObject
来源:https://blog.csdn.net/liuxing93619/article/details/120837018


猜你喜欢
- 前言:数据的排序是比较常用的操作,DataFrame 的排序分为两种,一种是对索引进行排序,另一种是对值进行排序,接下来就分别介绍一下。1.
- 很简单的一个东西,在'\n'、'\r\n'、'\r'3中换行符之间进行转换。用法usage:
- 1. 路由概念路由的本质就是一种对应关系,根据不同的URL请求,返回对应不同的资源。那么url地址和真实的资源之间就有一种对应的关系,就是路
- 这是份总结,有不恰达的地方欢迎一同讨论联系方式 : 龙藏 longzang@taobao.com点击这里全幅围观或者点下面大图去 slide
- 对于python2.7字符串在Python2.7内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,
- 本文实现12306抢火车票/京东抢手机示例,具体如下:#12306秒抢Python代码from splinter.browser impor
- 这篇文章主要介绍了Python定义函数时参数有默认值问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 我大概思考了下有如下几种方法可以解决: 1.在图片服务器上开通FTP,人为添加图片地址即可,但不方便,特别是在可视编辑器中看图还的多一部操作
- 如果你是一位前端开发工程师,对“跨平台”一词应该不会感到陌生。像常见的前端框架:比如React、Vue、Angular,它们可以做网页端,也
- 前言因为项目需要,存储字段存储成了JSON格式,在项目中是将查询出来的值通过jackson转成相应的bean进行处理的,觉得不够简单方便。M
- 利用Object.defineProperty进行数据劫持代码如下<!DOCTYPE html><html lang=&q
- 主要介绍常用的MySQL命令,包括连接数据库,修改密码,管理用户,操作数据库,操作数据表,数据库备份等,每个命令都配有实例说明,让大家更容易
- 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该显示在页面上的数据在数据库表中的起始位置。确定分页需求:1. 每
- 一、变量的定义 mysql中变量定义用declare来定义一局部变量,该变量的使用范围只能在begin...end 块中使用,变量必须定义在
- 说明相应的学习视频见链接,本文只对重点进行总结。多进程重点(只要看下面代码的main函数即可)1.创建2.如何开守护进程3.多进程,开销大,
- 用鼠标双击需要更改的变量,就会将其选中,选中的标志是相应变量名有了色块然后右键点击这个变量,找到Refactor,然后再选择Reanme然后
- 改变一个表的分区方案只需使用alter table 加 partition_options 子句就可以了。和创建分区表时的create ta
- Django中间件在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方
- 把今天的学习的opencv知识先记录一下!运行环境是:pycharm话不多说,献上代码再说:import cv2 # openc
- 有台服务器,访问量挺大,每天近250w动态pv,数据库查询平均每秒近600次 另一台服务器,跑的程序跟这台一样,不过只有每天约40w动态pv