Spaces:
Runtime error
Runtime error
Commit
·
7982b5b
1
Parent(s):
04b006b
Add basic caching to distance feature
Browse filesAlthough distance between two points don't depend on the which point was selected first, the probability of two points being select twice (upto 4-5 significant digits in lat/lon) is ~ 0. So, I am not bothering with treating dist(A,B) the same as dist(B,A).
frontend/src/components/Map.js
CHANGED
@@ -58,6 +58,8 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
58 |
const [geoSidebarOpen, setGeoSidebarOpen] = useState(false);
|
59 |
const [geoUnit, setGeoUnit] = useState('km');
|
60 |
|
|
|
|
|
61 |
const handleMouseDown = (e) => {
|
62 |
isDragging.current = true;
|
63 |
startX.current = e.clientX;
|
@@ -197,6 +199,7 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
197 |
const data = await res.json();
|
198 |
setGeoDistance(data.distance);
|
199 |
setGeoSidebarOpen(true);
|
|
|
200 |
} catch (err) {
|
201 |
console.error('Failed to fetch distance:', err);
|
202 |
setGeoDistance(null);
|
@@ -206,6 +209,13 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
206 |
|
207 |
useEffect(() => {
|
208 |
if (geoPoints.length === 2) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
const fetchDistance = async () => {
|
210 |
try{
|
211 |
const res = await fetch(`${BACKEND_URL}/geodistance`, {
|
@@ -221,6 +231,7 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
221 |
});
|
222 |
const data = await res.json();
|
223 |
setGeoDistance(data.distance);
|
|
|
224 |
}
|
225 |
catch (err) {
|
226 |
console.error('Failed to fetch distance:', err);
|
|
|
58 |
const [geoSidebarOpen, setGeoSidebarOpen] = useState(false);
|
59 |
const [geoUnit, setGeoUnit] = useState('km');
|
60 |
|
61 |
+
const distanceCache = useRef({});
|
62 |
+
|
63 |
const handleMouseDown = (e) => {
|
64 |
isDragging.current = true;
|
65 |
startX.current = e.clientX;
|
|
|
199 |
const data = await res.json();
|
200 |
setGeoDistance(data.distance);
|
201 |
setGeoSidebarOpen(true);
|
202 |
+
console.log("Distance fetched:", data.distance);
|
203 |
} catch (err) {
|
204 |
console.error('Failed to fetch distance:', err);
|
205 |
setGeoDistance(null);
|
|
|
209 |
|
210 |
useEffect(() => {
|
211 |
if (geoPoints.length === 2) {
|
212 |
+
const cacheKey = `${geoPoints[0].lat},${geoPoints[0].lon}-${geoPoints[1].lat},${geoPoints[1].lon}-${geoUnit}`;
|
213 |
+
if (distanceCache.current[cacheKey]) {
|
214 |
+
setGeoDistance(distanceCache.current[cacheKey]);
|
215 |
+
console.log("Using cached distance:", distanceCache.current[cacheKey]);
|
216 |
+
return;
|
217 |
+
}
|
218 |
+
|
219 |
const fetchDistance = async () => {
|
220 |
try{
|
221 |
const res = await fetch(`${BACKEND_URL}/geodistance`, {
|
|
|
231 |
});
|
232 |
const data = await res.json();
|
233 |
setGeoDistance(data.distance);
|
234 |
+
distanceCache.current[cacheKey] = data.distance; // Using cachig here, forgot it in first attempt.
|
235 |
}
|
236 |
catch (err) {
|
237 |
console.error('Failed to fetch distance:', err);
|