Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#169): implement cache expiry for remote places #237

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,6 +37,7 @@
"liquidjs": "^10.9.2",
"lodash": "^4.17.21",
"luxon": "^3.4.4",
"node-cache": "^5.1.2",
"pino-pretty": "^10.2.3",
"ts-node": "^10.9.2",
"typescript": "^5.2.2",
Expand Down
72 changes: 35 additions & 37 deletions src/lib/remote-place-cache.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import NodeCache from 'node-cache';

import Place from '../services/place';
import { ChtApi } from './cht-api';
import { IPropertyValue } from '../property-value';
import { ContactType, HierarchyConstraint } from '../config';
import { NamePropertyValue } from '../property-value/name-property-value';

type RemotePlacesByType = {
[key: string]: RemotePlace[];
};

type RemotePlaceDatastore = {
[key: string]: RemotePlacesByType;
};

export type RemotePlace = {
id: string;
name: IPropertyValue;
Expand All @@ -24,52 +18,56 @@ export type RemotePlace = {
};

export default class RemotePlaceCache {
private static cache: RemotePlaceDatastore = {};
private static cache: NodeCache;

public static async getPlacesWithType(chtApi: ChtApi, contactType: ContactType, hierarchyLevel: HierarchyConstraint)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what the difference between public getPlacesWithType and public getRemotePlaces in this class? are two interfaces like this required?

: Promise<RemotePlace[]> {
const domainStore = await RemotePlaceCache.getDomainStore(chtApi, contactType, hierarchyLevel);
return domainStore;
const { domain } = chtApi.chtSession.authInfo;
const placeType = hierarchyLevel.contact_type;
const cacheKey = this.getCacheKey(domain, placeType);

let places = this.getCache().get<RemotePlace[]>(cacheKey);
if (!places) {
places = await this.fetchRemotePlaces(chtApi, contactType, hierarchyLevel);
this.getCache().set(cacheKey, places);
}
return places;
}

private static getCache(): NodeCache {
if (!this.cache) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems easy to use RemotePlaceCache.cache instead of getCache? Is to make that harmless? Like calling initialize once RemotePlaceCache.initialize() once within the module scope and then you can use this.cache safely everywhere else

this.cache = new NodeCache({
stdTTL: 3600,
checkperiod: 120
});
}
return this.cache;
}

private static getCacheKey(domain: string, placeType?: string): string {
return placeType ? `${domain}:${placeType}` : domain;
}

public static add(place: Place, chtApi: ChtApi): void {
const { domain } = chtApi.chtSession.authInfo;
const placeType = place.type.name;

const places = RemotePlaceCache.cache[domain]?.[placeType];
// if there is no cache existing, discard the value
// it will be fetched if needed when the cache is built
const cacheKey = this.getCacheKey(domain, placeType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets say there is a place x of type type on the instance.

scenario 1

  1. the cache is empty
  2. run get('type')
    you'll get x

but scenario 2

  1. the cache is empty
  2. add(y) of type
  3. get('type')

now you won't see x. i think this is a bug, no?


const places = this.getCache().get<RemotePlace[]>(cacheKey);
if (places) {
places.push(place.asRemotePlace());
this.getCache().set(cacheKey, places);
}
}

public static clear(chtApi: ChtApi, contactTypeName?: string): void {
const domain = chtApi?.chtSession?.authInfo?.domain;
if (!domain) {
RemotePlaceCache.cache = {};
} else if (!contactTypeName) {
delete RemotePlaceCache.cache[domain];
} else if (RemotePlaceCache.cache[domain]) {
delete RemotePlaceCache.cache[domain][contactTypeName];
}
}

private static async getDomainStore(chtApi: ChtApi, contactType: ContactType, hierarchyLevel: HierarchyConstraint)
: Promise<RemotePlace[]> {
const { domain } = chtApi.chtSession.authInfo;
const placeType = hierarchyLevel.contact_type;
const { cache: domainCache } = RemotePlaceCache;
const places = domainCache[domain]?.[placeType];
if (!places) {
const fetchPlacesWithType = RemotePlaceCache.fetchRemotePlaces(chtApi, contactType, hierarchyLevel);
domainCache[domain] = {
...domainCache[domain],
[placeType]: await fetchPlacesWithType,
};
this.getCache().flushAll();
} else {
const cacheKey = this.getCacheKey(domain, contactTypeName);
this.getCache().del(cacheKey);
}

return domainCache[domain][placeType];
}

private static async fetchRemotePlaces(chtApi: ChtApi, contactType: ContactType, hierarchyLevel: HierarchyConstraint): Promise<RemotePlace[]> {
Expand Down
Loading