Download User/src/services/cacheService.ts from 0xarchit/CityTrack: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/0xarchit/CityTrack/resolve/main/User/src/services/cacheService.ts
- Command line
-
hf download hf://spaces/0xarchit/CityTrack/User/src/services/cacheService.ts
-
curl -L -o cacheService.ts https://huggingface.co/spaces/0xarchit/CityTrack/resolve/main/User/src/services/cacheService.ts
1.4 kB
| import AsyncStorage from '@react-native-async-storage/async-storage'; | |
| import { Issue } from '../types'; | |
| const CACHE_KEY = 'ISSUES_CACHE'; | |
| const CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes | |
| interface CacheData { | |
| issues: Issue[]; | |
| timestamp: number; | |
| } | |
| export const cacheService = { | |
| /** | |
| * Retrieves issues from cache. | |
| * @param ignoreExpiry If true, returns cached data even if expired (useful for initial render while fetching) | |
| */ | |
| async getIssuesCache(ignoreExpiry = false): Promise<Issue[] | null> { | |
| try { | |
| const cached = await AsyncStorage.getItem(CACHE_KEY); | |
| if (!cached) return null; | |
| const data: CacheData = JSON.parse(cached); | |
| const isExpired = Date.now() - data.timestamp > CACHE_EXPIRY_MS; | |
| if (isExpired && !ignoreExpiry) { | |
| // We don't clear here if we want to allow stale retrieval later | |
| return null; | |
| } | |
| return data.issues; | |
| } catch { | |
| return null; | |
| } | |
| }, | |
| async setIssuesCache(issues: Issue[]): Promise<void> { | |
| try { | |
| const data: CacheData = { | |
| issues, | |
| timestamp: Date.now(), | |
| }; | |
| await AsyncStorage.setItem(CACHE_KEY, JSON.stringify(data)); | |
| } catch { | |
| // Ignore write errors | |
| } | |
| }, | |
| async clearCache(): Promise<void> { | |
| try { | |
| await AsyncStorage.removeItem(CACHE_KEY); | |
| } catch { | |
| // Ignore errors | |
| } | |
| }, | |
| }; | |