Spaces:
Paused
Paused
Update trade_analysis/cache.py
Browse files- trade_analysis/cache.py +23 -23
trade_analysis/cache.py
CHANGED
|
@@ -1,24 +1,24 @@
|
|
| 1 |
-
import json, time, pathlib, hashlib
|
| 2 |
-
CACHE_DIR = pathlib.Path("
|
| 3 |
-
TTL = 60*60*24 # 24 h
|
| 4 |
-
def _key(name): return CACHE_DIR / f"{hashlib.md5(name.encode()).hexdigest()}.json"
|
| 5 |
-
|
| 6 |
-
def put(n, obj):
|
| 7 |
-
_key(n).write_text(
|
| 8 |
-
json.dumps(obj, ensure_ascii=False),
|
| 9 |
-
encoding='utf-8' # β Add this
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
def get(n):
|
| 13 |
-
fp = _key(n)
|
| 14 |
-
if not fp.exists():
|
| 15 |
-
return None
|
| 16 |
-
try:
|
| 17 |
-
data = fp.read_text(encoding='utf-8') # β Add this
|
| 18 |
-
return json.loads(data)
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"Cache error: {e}")
|
| 21 |
-
return None
|
| 22 |
-
|
| 23 |
-
|
| 24 |
def put(n,obj): _key(n).write_text(json.dumps(obj,ensure_ascii=False), encoding='utf-8')
|
|
|
|
| 1 |
+
import json, time, pathlib, hashlib
|
| 2 |
+
CACHE_DIR = pathlib.Path("/tmp/.cache"); CACHE_DIR.mkdir(exist_ok=True)
|
| 3 |
+
TTL = 60*60*24 # 24 h
|
| 4 |
+
def _key(name): return CACHE_DIR / f"{hashlib.md5(name.encode()).hexdigest()}.json"
|
| 5 |
+
|
| 6 |
+
def put(n, obj):
|
| 7 |
+
_key(n).write_text(
|
| 8 |
+
json.dumps(obj, ensure_ascii=False),
|
| 9 |
+
encoding='utf-8' # β Add this
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def get(n):
|
| 13 |
+
fp = _key(n)
|
| 14 |
+
if not fp.exists():
|
| 15 |
+
return None
|
| 16 |
+
try:
|
| 17 |
+
data = fp.read_text(encoding='utf-8') # β Add this
|
| 18 |
+
return json.loads(data)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Cache error: {e}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
def put(n,obj): _key(n).write_text(json.dumps(obj,ensure_ascii=False), encoding='utf-8')
|