@@ -74,9 +74,8 @@ def __init__(self):
74
74
75
75
def get_data (self , key : str ) -> pd .DataFrame | None :
76
76
"""Return data from cache if exists"""
77
- file_path = os .path .join (
78
- self .directory , f"{ self .generate_unique_consistent_filename (key )} .orc"
79
- )
77
+
78
+ file_path = self .make_file_path_from_key (key )
80
79
81
80
if os .path .exists (file_path ):
82
81
if self .is_file_cache_expired (file_path ):
@@ -89,21 +88,22 @@ def get_data(self, key: str) -> pd.DataFrame | None:
89
88
90
89
def set_data (self , key : str , data : pd .DataFrame ) -> None :
91
90
"""Save data to cache. The data will be stored as an ORC file."""
92
- file_path = os .path .join (
93
- self .directory , f"{ self .generate_unique_consistent_filename (key )} .orc"
94
- )
91
+ file_path = self .make_file_path_from_key (key )
95
92
96
93
data .to_orc (file_path )
97
94
98
95
def invalidate (self , key : str ) -> None :
99
96
"""Remove data from cache"""
100
- file_path = os .path .join (
101
- self .directory , f"{ self .generate_unique_consistent_filename (key )} .orc"
102
- )
97
+ file_path = self .make_file_path_from_key (key )
103
98
104
99
if os .path .exists (file_path ):
105
100
os .remove (file_path )
106
101
102
+ def make_file_path_from_key (self , key : str ) -> str :
103
+ return os .path .join (
104
+ self .directory , f"{ self .generate_unique_consistent_filename (key )} .orc"
105
+ )
106
+
107
107
def generate_unique_consistent_filename (self , key : str ) -> str :
108
108
"""Generate unique and consistent filename based on the key"""
109
109
hash_object = hashlib .sha256 ()
@@ -112,8 +112,13 @@ def generate_unique_consistent_filename(self, key: str) -> str:
112
112
113
113
@staticmethod
114
114
def is_file_cache_expired (file_path : str ) -> bool :
115
- """Check if file cache is expired"""
116
- return os .path .getmtime (file_path ) + config .get_file_cache_ttl () < time .time ()
115
+ """Check if file cache is expired. If TTL is 0 then cache never expires."""
116
+ file_ttl = config .get_file_cache_ttl ()
117
+
118
+ if not file_ttl :
119
+ return False
120
+
121
+ return time .time () - os .path .getmtime (file_path ) > file_ttl
117
122
118
123
119
124
def get_cache_manager (cache_strategy : str | None ) -> CacheStrategy :
0 commit comments