-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_manager.py
More file actions
94 lines (78 loc) · 3.04 KB
/
Copy pathcache_manager.py
File metadata and controls
94 lines (78 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import json
import joblib
import hashlib
from pathlib import Path
from datetime import datetime
class CacheManager:
"""Manages caching for model training and feature engineering results"""
def __init__(self, cache_dir='model_cache'):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self.cache_info_file = self.cache_dir / 'cache_info.json'
def _calculate_model_hash(self):
"""Calculate hash based on model parameters for cache validation"""
model_params = {
'version': '2.0',
'timestamp': datetime.now().strftime('%Y%m%d')
}
param_string = json.dumps(model_params, sort_keys=True)
return hashlib.md5(param_string.encode()).hexdigest()
def is_cache_valid(self, data_hash):
"""Check if cache is valid based on data and model hashes"""
if not self.cache_info_file.exists():
return False
try:
with open(self.cache_info_file, 'r') as f:
saved_cache_info = json.load(f)
except:
return False
return (saved_cache_info.get('data_hash') == data_hash and
saved_cache_info.get('model_hash') == self._calculate_model_hash())
def clear_cache(self):
"""Clear all cache files"""
print("Cache is invalid, clearing cache directory")
for file_path in self.cache_dir.glob('*.joblib'):
try:
file_path.unlink()
except:
pass
if self.cache_info_file.exists():
try:
self.cache_info_file.unlink()
except:
pass
print("Cache cleared successfully")
def save_cache_info(self, data_hash):
"""Save cache information"""
cache_info = {
'data_hash': data_hash,
'model_hash': self._calculate_model_hash(),
'timestamp': datetime.now().isoformat(),
'version': '2.0'
}
with open(self.cache_info_file, 'w') as f:
json.dump(cache_info, f, indent=2)
def get_cache_key(self, operation, **kwargs):
"""Generate cache key for specific operation"""
key_data = f"{operation}_{json.dumps(kwargs, sort_keys=True)}"
return hashlib.md5(key_data.encode()).hexdigest()
def save_to_cache(self, key, data):
"""Save data to cache"""
cache_file = self.cache_dir / f"{key}.joblib"
try:
joblib.dump(data, cache_file)
return True
except Exception as e:
print(f"Failed to save to cache: {e}")
return False
def load_from_cache(self, key):
"""Load data from cache"""
cache_file = self.cache_dir / f"{key}.joblib"
if not cache_file.exists():
return None
try:
return joblib.load(cache_file)
except Exception as e:
print(f"Failed to load from cache: {e}")
return None