Spaces:
Sleeping
Sleeping
update cache.py, google_maps_tool.py and route_planner_tool.py
Browse files- tools/google_maps_tool.py +1 -1
- tools/route_planner_tool.py +1 -1
- utils/cache.py +6 -4
tools/google_maps_tool.py
CHANGED
|
@@ -4,7 +4,7 @@ from typing import List, Dict, Optional
|
|
| 4 |
from crewai.tools import BaseTool
|
| 5 |
from utils.cache import SQLiteCache
|
| 6 |
|
| 7 |
-
cache = SQLiteCache("cache.sqlite")
|
| 8 |
|
| 9 |
class GoogleMapsTool(BaseTool):
|
| 10 |
"""
|
|
|
|
| 4 |
from crewai.tools import BaseTool
|
| 5 |
from utils.cache import SQLiteCache
|
| 6 |
|
| 7 |
+
cache = SQLiteCache("tmp/cache.sqlite")
|
| 8 |
|
| 9 |
class GoogleMapsTool(BaseTool):
|
| 10 |
"""
|
tools/route_planner_tool.py
CHANGED
|
@@ -6,7 +6,7 @@ import time
|
|
| 6 |
import random
|
| 7 |
from utils.cache import SQLiteCache
|
| 8 |
|
| 9 |
-
cache = SQLiteCache("cache.sqlite")
|
| 10 |
|
| 11 |
class RoutePlannerTool(BaseTool):
|
| 12 |
"""
|
|
|
|
| 6 |
import random
|
| 7 |
from utils.cache import SQLiteCache
|
| 8 |
|
| 9 |
+
cache = SQLiteCache("tmp/cache.sqlite")
|
| 10 |
|
| 11 |
class RoutePlannerTool(BaseTool):
|
| 12 |
"""
|
utils/cache.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
# utils/cache.py
|
| 2 |
import json, sqlite3, time
|
| 3 |
import threading
|
|
|
|
| 4 |
from typing import Any, Optional
|
| 5 |
|
| 6 |
class SQLiteCache:
|
| 7 |
def __init__(
|
| 8 |
self,
|
| 9 |
-
path: str = "cache.sqlite",
|
| 10 |
default_ttl_seconds: int = 3600,
|
| 11 |
sqlite_timeout_seconds: int = 10,
|
| 12 |
busy_timeout_ms: int = 5000,
|
|
@@ -26,6 +27,7 @@ class SQLiteCache:
|
|
| 26 |
con.execute("PRAGMA journal_mode=WAL;")
|
| 27 |
con.execute("PRAGMA synchronous=NORMAL;")
|
| 28 |
con.execute(f"PRAGMA busy_timeout={self.busy_timeout_ms};")
|
|
|
|
| 29 |
|
| 30 |
def _init_db(self):
|
| 31 |
with self._connect() as con:
|
|
@@ -40,7 +42,7 @@ class SQLiteCache:
|
|
| 40 |
|
| 41 |
def get(self, key: str) -> Optional[Any]:
|
| 42 |
now = int(time.time())
|
| 43 |
-
with self.
|
| 44 |
row = con.execute("SELECT v, expires_at FROM cache WHERE k=?", (key,)).fetchone()
|
| 45 |
if not row:
|
| 46 |
return None
|
|
@@ -62,7 +64,7 @@ class SQLiteCache:
|
|
| 62 |
payload = json.dumps(value)
|
| 63 |
|
| 64 |
with self._lock:
|
| 65 |
-
with self.
|
| 66 |
con.execute(
|
| 67 |
"INSERT OR REPLACE INTO cache (k, v, expires_at) VALUES (?, ?, ?)",
|
| 68 |
(key, payload, expires_at)
|
|
@@ -71,7 +73,7 @@ class SQLiteCache:
|
|
| 71 |
|
| 72 |
def delete(self, key: str) -> None:
|
| 73 |
with self._lock:
|
| 74 |
-
with self.
|
| 75 |
con.execute("DELETE FROM cache WHERE k=?", (key,))
|
| 76 |
con.commit()
|
| 77 |
|
|
|
|
| 1 |
# utils/cache.py
|
| 2 |
import json, sqlite3, time
|
| 3 |
import threading
|
| 4 |
+
from contextlib import contextmanager
|
| 5 |
from typing import Any, Optional
|
| 6 |
|
| 7 |
class SQLiteCache:
|
| 8 |
def __init__(
|
| 9 |
self,
|
| 10 |
+
path: str = "/tmp/cache.sqlite",
|
| 11 |
default_ttl_seconds: int = 3600,
|
| 12 |
sqlite_timeout_seconds: int = 10,
|
| 13 |
busy_timeout_ms: int = 5000,
|
|
|
|
| 27 |
con.execute("PRAGMA journal_mode=WAL;")
|
| 28 |
con.execute("PRAGMA synchronous=NORMAL;")
|
| 29 |
con.execute(f"PRAGMA busy_timeout={self.busy_timeout_ms};")
|
| 30 |
+
return con
|
| 31 |
|
| 32 |
def _init_db(self):
|
| 33 |
with self._connect() as con:
|
|
|
|
| 42 |
|
| 43 |
def get(self, key: str) -> Optional[Any]:
|
| 44 |
now = int(time.time())
|
| 45 |
+
with self._connect() as con:
|
| 46 |
row = con.execute("SELECT v, expires_at FROM cache WHERE k=?", (key,)).fetchone()
|
| 47 |
if not row:
|
| 48 |
return None
|
|
|
|
| 64 |
payload = json.dumps(value)
|
| 65 |
|
| 66 |
with self._lock:
|
| 67 |
+
with self._connect() as con:
|
| 68 |
con.execute(
|
| 69 |
"INSERT OR REPLACE INTO cache (k, v, expires_at) VALUES (?, ?, ?)",
|
| 70 |
(key, payload, expires_at)
|
|
|
|
| 73 |
|
| 74 |
def delete(self, key: str) -> None:
|
| 75 |
with self._lock:
|
| 76 |
+
with self._connect() as con:
|
| 77 |
con.execute("DELETE FROM cache WHERE k=?", (key,))
|
| 78 |
con.commit()
|
| 79 |
|