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

Conversation

inromualdo
Copy link
Contributor

@inromualdo inromualdo commented Jan 6, 2025

Replace PR#183

closes #169

Note: Failing unit test will be fix by PR#239

@inromualdo inromualdo requested a review from paulpascal January 7, 2025 13:24
Copy link
Member

@kennsippell kennsippell left a comment

Choose a reason for hiding this comment

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

Nice! I think this is really valuable work.

src/lib/remote-place-cache.ts Outdated Show resolved Hide resolved
src/lib/remote-place-cache.ts Show resolved Hide resolved
@inromualdo inromualdo requested a review from kennsippell January 8, 2025 12:20
Copy link
Member

@kennsippell kennsippell left a comment

Choose a reason for hiding this comment

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

nice!

@@ -31,8 +24,38 @@ export type RemotePlace = {
};

export default class RemotePlaceCache {
private static cache: RemotePlaceDatastore = {};
private static cache: NodeCache;
private static readonly CACHE_TTL = 3600;
Copy link
Member

Choose a reason for hiding this comment

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

is this one hour? i suspect we want it to be like 24 hrs? can you share your thinking? for nairobi i think this takes over a minute and 1 hour expiration would guarantee basically users do it once per session. is that what we want?

}

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

private static readonly CACHE_TTL = 3600;
private static readonly CACHE_CHECK_PERIOD = 120;

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?

// Clear all keys matching domain prefix
const keys = this.getCache().keys();
const domainPrefix = `${domain}:`;
keys.forEach(key => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
keys.forEach(key => {
keys.filter(key => key.startsWith(domainPrefix))
.forEach(this.cache.del)

Consider ?

// if there is no cache existing, discard the value
// it will be fetched if needed when the cache is built
if (places) {
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.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

why do you only add if places is empty? previously, we only added if not empty...

@@ -118,4 +152,11 @@ export default class RemotePlaceCache {

return [];
}

// For testing purposes
public static hasData(domain: string, placeType: string): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

you can just access private members in tests. shouldn't this be a function within the spec.ts file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache expiry for remote places
2 participants