1
1
import hashlib
2
- import os
3
2
from typing import Any , Optional
4
3
5
4
import ibis
5
+ import opendal
6
6
from loguru import logger
7
7
from opentelemetry import trace
8
8
9
9
tracer = trace .get_tracer (__name__ )
10
10
11
11
12
12
class QueryCacheManager :
13
- def __init__ (self ):
14
- pass
13
+ def __init__ (self , root : str = "/tmp/wren-engine/" ):
14
+ self . root = root
15
15
16
16
@tracer .start_as_current_span ("get_cache" , kind = trace .SpanKind .INTERNAL )
17
17
def get (self , data_source : str , sql : str , info ) -> Optional [Any ]:
18
18
cache_key = self ._generate_cache_key (data_source , sql , info )
19
- cache_path = self ._get_cache_path (cache_key )
19
+ cache_file_name = self ._get_cache_file_name (cache_key )
20
+ op = self ._get_dal_operator ()
21
+ full_path = self ._get_full_path (cache_file_name )
20
22
21
23
# Check if cache file exists
22
- if os . path . exists (cache_path ):
24
+ if op . exists (cache_file_name ):
23
25
try :
24
- cache = ibis .read_parquet (cache_path )
26
+ logger .info (f"\n Reading query cache { cache_file_name } \n " )
27
+ cache = ibis .read_parquet (full_path )
25
28
df = cache .execute ()
29
+ logger .info ("\n query cache to dataframe\n " )
26
30
return df
27
31
except Exception as e :
28
32
logger .debug (f"Failed to read query cache { e } " )
@@ -33,14 +37,17 @@ def get(self, data_source: str, sql: str, info) -> Optional[Any]:
33
37
@tracer .start_as_current_span ("set_cache" , kind = trace .SpanKind .INTERNAL )
34
38
def set (self , data_source : str , sql : str , result : Any , info ) -> None :
35
39
cache_key = self ._generate_cache_key (data_source , sql , info )
36
- cache_path = self ._get_cache_path (cache_key )
40
+ cache_file_name = self ._get_cache_file_name (cache_key )
41
+ op = self ._get_dal_operator ()
42
+ full_path = self ._get_full_path (cache_file_name )
37
43
38
44
try :
39
45
# Create cache directory if it doesn't exist
40
- os .makedirs (os .path .dirname (cache_path ), exist_ok = True )
41
- cache = ibis .memtable (result )
42
- logger .info (f"\n Writing query cache to { cache_path } \n " )
43
- cache .to_parquet (cache_path )
46
+ with op .open (cache_file_name , mode = "wb" ) as file :
47
+ cache = ibis .memtable (result )
48
+ logger .info (f"\n Writing query cache to { cache_file_name } \n " )
49
+ if file .writable ():
50
+ cache .to_parquet (full_path )
44
51
except Exception as e :
45
52
logger .debug (f"Failed to write query cache: { e } " )
46
53
return
@@ -57,5 +64,12 @@ def _generate_cache_key(self, data_source: str, sql: str, info) -> str:
57
64
58
65
return hashlib .sha256 (key_string .encode ()).hexdigest ()
59
66
60
- def _get_cache_path (self , cache_key : str ) -> str :
61
- return f"/tmp/wren-engine/{ cache_key } .cache"
67
+ def _get_cache_file_name (self , cache_key : str ) -> str :
68
+ return f"{ cache_key } .cache"
69
+
70
+ def _get_full_path (self , path : str ) -> str :
71
+ return self .root + path
72
+
73
+ def _get_dal_operator (self ) -> Any :
74
+ # Default implementation using local filesystem
75
+ return opendal .Operator ("fs" , root = self .root )
0 commit comments