File size: 1,313 Bytes
bd2b21f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
interface CacheEntry<T> {
  data: T;
  timestamp: number;
}

const cache = new Map<string, CacheEntry<unknown>>();
const DEFAULT_TTL = 3 * 60 * 1000; // 3 minutes

export function getCached<T>(key: string): T | null {
  const entry = cache.get(key) as CacheEntry<T> | undefined;
  if (!entry) return null;

  const age = Date.now() - entry.timestamp;
  if (age > DEFAULT_TTL) {
    cache.delete(key);
    return null;
  }

  return entry.data;
}

export function setCache<T>(key: string, data: T, ttl = DEFAULT_TTL): void {
  if (!data || (Array.isArray(data) && data.length === 0)) {
    return;
  }
  cache.set(key, { data, timestamp: Date.now() });
}

export function clearCache(): void {
  cache.clear();
}

export function getCacheKey(prefix: string, ...params: (string | number | boolean)[]): string {
  return `${prefix}:${params.join(':')}`;
}

export function generateRouteCacheKey(start: [number, number], end: [number, number], profile: string): string {
  return getCacheKey('route', start.join(','), end.join(','), profile);
}

export function generateGeocodeCacheKey(query: string): string {
  return getCacheKey('geocode', query.toLowerCase().trim());
}

export function generateWeatherCacheKey(lat: number, lon: number): string {
  return getCacheKey('weather', lat.toFixed(3), lon.toFixed(3));
}