84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
from config.settings import ConfigHandler
|
||
import os
|
||
from typing import Any
|
||
from tools.exceptions import ValueNotFoundError
|
||
|
||
class Cache:
|
||
""" 设置、读取缓存 """
|
||
|
||
def __init__(self, filename: [str, bool]) -> None:
|
||
# 如果filename不为空,则操作指定文件内容
|
||
if filename:
|
||
self.path = os.path.join(ConfigHandler().cache_path,filename)
|
||
# 如果filename为None,则操作所有文件内容
|
||
else:
|
||
self.path = ConfigHandler().cache_path
|
||
|
||
def set_cache(self, key: str, value) -> None:
|
||
"""
|
||
设置缓存, 只支持设置单字典类型缓存数据, 缓存文件如以存在,则替换之前的缓存内容
|
||
:return:
|
||
"""
|
||
with open(self.path, 'w') as f:
|
||
f.write(str({key: value}))
|
||
|
||
|
||
def set_caches(self, value: any) -> None:
|
||
"""
|
||
设置多组缓存数据
|
||
:param value: 缓存内容
|
||
:return:
|
||
"""
|
||
with open(self.path, 'w') as f:
|
||
f.write(str(value))
|
||
|
||
def get_cache(self) -> Any:
|
||
"""
|
||
获取缓存数据
|
||
:return:
|
||
"""
|
||
with open(self.path, 'r') as f:
|
||
return f.read()
|
||
|
||
def clean_cache(self):
|
||
if not os.path.exists(self.path):
|
||
raise ValueError("您要删除的缓存文件不存在. {0}".format(self.path))
|
||
os.remove(self.path)
|
||
|
||
@classmethod
|
||
def clean_all_cache(cls) -> None:
|
||
"""
|
||
清除所有缓存文件
|
||
:return:
|
||
"""
|
||
cache_path = ConfigHandler().cache_path
|
||
# 列出目录下所有文件,生成一个list
|
||
list_dir = os.listdir(cache_path)
|
||
for i in list_dir:
|
||
# 循环删除文件夹下得所有内容
|
||
os.remove(cache_path + "/" + i)
|
||
|
||
|
||
_cache_config = {}
|
||
class CacheHandler:
|
||
@staticmethod
|
||
def get_cache(cache_data):
|
||
try:
|
||
return _cache_config[cache_data]
|
||
except KeyError:
|
||
raise ValueError(f"{cache_data}的缓存数据未找到,请检查是否将该数据存入缓存中")
|
||
|
||
@staticmethod
|
||
def update_cache(*, cache_name, value):
|
||
_cache_config[cache_name] = value
|
||
|
||
if __name__ == '__main__':
|
||
a = Cache('ecu_collection_select_id').get_cache()
|
||
|
||
print(a)
|
||
# Cache('2022-6-2').set_cache("2022/6/2",'guolinli')
|
||
Cache('2022-6-3').set_caches( 'guolinli')
|