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.

Example

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 instance
const soccer = new Soccer({ cache: new MyCustomCache() });
interface CustomCache {
    clearCache(): void;
    getUrl(url, maxAge): unknown;
    setUrl(url, json): void;
}

Methods

  • Clear the cache

    Returns void

  • Get cached data for URL

    Parameters

    • url: string

      Request URL.href

    • maxAge: number

      Max age of the cache in seconds

    Returns unknown

    Cached JSON data

  • Set cache for data

    Parameters

    • url: string

      Request URL.href

    • json: unknown

      Request response data

    Returns void