-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9c03427
9068019
1cf9167
64c95e8
499d40b
1f9fb79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
|
@@ -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) | ||
: 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
this.cache = new NodeCache({ | ||
stdTTL: 3600, | ||
checkperiod: 120 | ||
}); | ||
} | ||
return this.cache; | ||
} | ||
|
||
private static getCacheKey(domain: string, placeType?: string): string { | ||
inromualdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets say there is a place scenario 1
but scenario 2
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); | ||
inromualdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.getCache().del(cacheKey); | ||
} | ||
|
||
return domainCache[domain][placeType]; | ||
} | ||
|
||
private static async fetchRemotePlaces(chtApi: ChtApi, contactType: ContactType, hierarchyLevel: HierarchyConstraint): Promise<RemotePlace[]> { | ||
|
There was a problem hiding this comment.
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
andpublic getRemotePlaces
in this class? are two interfaces like this required?