Skip to content

Commit

Permalink
feat(#169): cache expiry for remote places
Browse files Browse the repository at this point in the history
  • Loading branch information
inromualdo committed Jun 20, 2024
1 parent 4d93578 commit 9ccb972
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"liquidjs": "^10.9.2",
"lodash": "^4.17.21",
"luxon": "^3.4.4",
"node-cache": "^5.1.2",
"pino-pretty": "^10.2.3",
"typescript": "^5.2.2",
"uuid": "^9.0.1"
Expand Down
30 changes: 22 additions & 8 deletions src/lib/remote-place-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import NodeCache from 'node-cache';
import Place from '../services/place';
import { ChtApi, RemotePlace } from './cht-api';
import {ChtApi, RemotePlace} from './cht-api';

const CACHE_KEY = 'CACHE_REMOTE_PLACE';

type RemotePlacesByType = {
[key: string]: RemotePlace[];
Expand All @@ -10,12 +13,14 @@ type RemotePlaceDatastore = {
};

export default class RemotePlaceCache {
private static cache: RemotePlaceDatastore = {};
// 60 min cache
private static cache = new NodeCache({
stdTTL: 60 * 60
});

public static async getPlacesWithType(chtApi: ChtApi, placeType: string)
: Promise<RemotePlace[]> {
const domainStore = await RemotePlaceCache.getDomainStore(chtApi, placeType);
return domainStore;
return await RemotePlaceCache.getDomainStore(chtApi, placeType);
}

public static async add(place: Place, chtApi: ChtApi): Promise<void> {
Expand All @@ -25,19 +30,27 @@ export default class RemotePlaceCache {

public static clear(chtApi: ChtApi, contactTypeName?: string): void {
const domain = chtApi?.chtSession?.authInfo?.domain;
let placeCache = RemotePlaceCache.cache.get<RemotePlaceDatastore>(CACHE_KEY);
if (!placeCache) {
return;
}
if (!domain) {
RemotePlaceCache.cache = {};
placeCache = {};
} else if (!contactTypeName) {
delete RemotePlaceCache.cache[domain];
delete placeCache[domain];
} else {
delete RemotePlaceCache.cache[domain][contactTypeName];
delete placeCache[domain][contactTypeName];
}
RemotePlaceCache.cache.set<RemotePlaceDatastore>(CACHE_KEY, placeCache);
}

private static async getDomainStore(chtApi: ChtApi, placeType: string)
: Promise<RemotePlace[]> {
const { domain } = chtApi.chtSession.authInfo;
const { cache: domainCache } = RemotePlaceCache;
let domainCache = RemotePlaceCache.cache.get<RemotePlaceDatastore>(CACHE_KEY);
if (!domainCache) {
domainCache = {};
}

const places = domainCache[domain]?.[placeType];
if (!places) {
Expand All @@ -46,6 +59,7 @@ export default class RemotePlaceCache {
...domainCache[domain],
[placeType]: await fetchPlacesWithType,
};
RemotePlaceCache.cache.set<RemotePlaceDatastore>(CACHE_KEY, domainCache);
}

return domainCache[domain][placeType];
Expand Down

0 comments on commit 9ccb972

Please sign in to comment.