Use this interface to define your own custom cache class.
Custom caches just have to implement the clearCache, getUrl and setUrl methods. How you define those methods internally is up to you.
clearCache
getUrl
setUrl
import { Soccer, type CustomCache } from '@reuters-graphics/sportlich';class MyCustomCache implements CustomCache { private cache: Record<string, { json: unknown, lastUpdated: string }> = {}; clearCache() { this.cache = {}; } getUrl(url: string, maxAge: number) { const cachedData = this.cache[url]; if (!cachedData) return null; const { json, lastUpdated } = cachedData; const secondsSinceUpdate = (Date.now() - new Date(lastUpdated).getTime()) / 1000; if (secondsSinceUpdate <= maxAge ) return json; return null; } setUrl(url: string, json: unknown) { this.cache[url] = { lastUpdated: new Date().toISOString(), json, }; }}// Pass your custom cache to new sportlich instanceconst soccer = new Soccer({ cache: new MyCustomCache() }); Copy
import { Soccer, type CustomCache } from '@reuters-graphics/sportlich';class MyCustomCache implements CustomCache { private cache: Record<string, { json: unknown, lastUpdated: string }> = {}; clearCache() { this.cache = {}; } getUrl(url: string, maxAge: number) { const cachedData = this.cache[url]; if (!cachedData) return null; const { json, lastUpdated } = cachedData; const secondsSinceUpdate = (Date.now() - new Date(lastUpdated).getTime()) / 1000; if (secondsSinceUpdate <= maxAge ) return json; return null; } setUrl(url: string, json: unknown) { this.cache[url] = { lastUpdated: new Date().toISOString(), json, }; }}// Pass your custom cache to new sportlich instanceconst soccer = new Soccer({ cache: new MyCustomCache() });
Clear the cache
Get cached data for URL
Request URL.href
Max age of the cache in seconds
Cached JSON data
Set cache for data
Request response data
Use this interface to define your own custom cache class.
Custom caches just have to implement the
clearCache
,getUrl
andsetUrl
methods. How you define those methods internally is up to you.Example