feat(backend): add TfL disruptions API with Gemini AI shortening
- Add TfL Unified API integration for real-time transport disruptions - Implement batch AI shortening using Gemini 2.5 Flash-Lite - Add in-memory caching with 1-hour TTL - Support Tube, Overground, DLR, Elizabeth Line, and Tram - Sort disruptions by severity with regex-based line name cleanup Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
57
apps/backend/src/tfl/cache.ts
Normal file
57
apps/backend/src/tfl/cache.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Simple in-memory cache for shortened disruption descriptions
|
||||
*/
|
||||
|
||||
interface CacheEntry {
|
||||
shortened: string
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
const cache = new Map<string, CacheEntry>()
|
||||
|
||||
// Cache for 1 hour
|
||||
const CACHE_DURATION = 60 * 60 * 1000
|
||||
|
||||
/**
|
||||
* Get cached shortened description
|
||||
*/
|
||||
export function getCachedShortened(originalReason: string): string | null {
|
||||
const entry = cache.get(originalReason)
|
||||
|
||||
if (!entry) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Check if expired
|
||||
if (Date.now() - entry.timestamp > CACHE_DURATION) {
|
||||
cache.delete(originalReason)
|
||||
return null
|
||||
}
|
||||
|
||||
return entry.shortened
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache a shortened description
|
||||
*/
|
||||
export function setCachedShortened(originalReason: string, shortened: string): void {
|
||||
cache.set(originalReason, {
|
||||
shortened,
|
||||
timestamp: Date.now(),
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear expired cache entries
|
||||
*/
|
||||
export function clearExpiredCache(): void {
|
||||
const now = Date.now()
|
||||
for (const [key, entry] of cache.entries()) {
|
||||
if (now - entry.timestamp > CACHE_DURATION) {
|
||||
cache.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear expired entries every 10 minutes
|
||||
setInterval(clearExpiredCache, 10 * 60 * 1000)
|
||||
Reference in New Issue
Block a user