1+ import getpass
12import logging
23import os
4+ import sys
35import time
46from dataclasses import asdict , dataclass , field
57from json import JSONDecodeError
1719 TypeVar ,
1820)
1921
20- from appdirs import user_data_dir
21-
2222from firebolt .utils .file_operations import (
2323 FernetEncrypter ,
2424 generate_encrypted_file_name ,
2929
3030# Cache expiry configuration
3131CACHE_EXPIRY_SECONDS = 3600 # 1 hour
32- APPNAME = "firebolt "
32+ APPNAME = "fireboltDriver "
3333
3434logger = logging .getLogger (__name__ )
3535
@@ -146,10 +146,11 @@ def _is_expired(self, value: T) -> bool:
146146 def set (self , key : ReprCacheable , value : T , preserve_expiry : bool = False ) -> None :
147147 if not self .disabled :
148148 # Set expiry_time for ConnectionInfo objects
149- if hasattr (value , "expiry_time" ):
150- if not preserve_expiry or value .expiry_time is None :
151- current_time = int (time .time ())
152- value .expiry_time = current_time + CACHE_EXPIRY_SECONDS
149+ if hasattr (value , "expiry_time" ) and (
150+ not preserve_expiry or value .expiry_time is None
151+ ):
152+ current_time = int (time .time ())
153+ value .expiry_time = current_time + CACHE_EXPIRY_SECONDS
153154
154155 s_key = self .create_key (key )
155156 self ._cache [s_key ] = value
@@ -193,6 +194,37 @@ def __hash__(self) -> int:
193194 return hash (self .key )
194195
195196
197+ def get_cache_data_dir (appname : str = APPNAME ) -> str :
198+ """
199+ Return the directory for storing cache files based on the OS.
200+ Mac: use $TMPDIR, fallback to /tmp/<appname>
201+ Windows: use the environment variable TEMP or if not defined C:\\ Temp
202+ Linux: use $XDG_RUNTIME_DIR, fallback to /tmp/<user_home>
203+ """
204+
205+ if sys .platform == "darwin" :
206+ tmpdir = os .environ .get ("TMPDIR" )
207+ if tmpdir :
208+ return os .path .join (tmpdir , appname )
209+ # fallback
210+ return os .path .join ("/tmp" , appname )
211+ elif sys .platform .startswith ("win" ):
212+ # Python doesn't expose java.io.tmpdir, but os.environ['TEMP'] is standard
213+ tmpdir = os .environ .get ("TEMP" )
214+ if tmpdir :
215+ return os .path .join (tmpdir , appname )
216+ # fallback
217+ return os .path .join ("C:\\ Temp" , appname )
218+ else :
219+ # Assume Linux/Unix
220+ xdg_dir = os .environ .get ("XDG_RUNTIME_DIR" )
221+ if xdg_dir :
222+ return os .path .join (xdg_dir , appname )
223+ # fallback: /tmp/<username>
224+ username = getpass .getuser ()
225+ return os .path .join ("/tmp" , username , appname )
226+
227+
196228class FileBasedCache :
197229 """
198230 File-based cache that persists to disk with encryption.
@@ -202,7 +234,7 @@ class FileBasedCache:
202234
203235 def __init__ (self , memory_cache : UtilCache [ConnectionInfo ], cache_name : str = "" ):
204236 self .memory_cache = memory_cache
205- self ._data_dir = user_data_dir ( appname = APPNAME ) # TODO: change to new dir
237+ self ._data_dir = get_cache_data_dir ( APPNAME )
206238 makedirs (self ._data_dir , exist_ok = True )
207239 # FileBasedCache has its own disabled state, independent of memory cache
208240 cache_env_var = f"FIREBOLT_SDK_DISABLE_CACHE_${ cache_name } "
0 commit comments