Skip to content

feat: cache query class#570

Merged
Yaros1ove merged 4 commits intomainfrom
DATAUI-3526_api-cache-class
Feb 4, 2026
Merged

feat: cache query class#570
Yaros1ove merged 4 commits intomainfrom
DATAUI-3526_api-cache-class

Conversation

@Yaros1ove
Copy link
Contributor

No description provided.

Comment on lines 378 to 382
const libData = await this.libsCache[id].getData();

return this.libsByIdCache[id].lib;
if (!libData) {
throw new Error(`Can't find lib with id – ${id}`);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess that if there will be no lib found by id we will get error "cannot read properties of undefined" while calling .getData() ?

Comment on lines +500 to +502
if (!content) {
throw new Error(`Can't find README for ${cacheKey}`);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as in comment above

Copy link
Collaborator

Choose a reason for hiding this comment

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

return this._data as Data;
}

await this.revalidate();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Am I correct in understanding that now, if the cache is outdated, we'll wait for it to update before serving the data?

Previously, we'd serve the data from the cache immediately and update the cache in parallel.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added immediateResponse parameter to getData function that is true on default

Comment on lines 49 to 83
changeAutoRevalidate(newAutoRevalidate: boolean) {
if (this._autoRevalidateInterval) {
clearInterval(this._autoRevalidateInterval);
}

this._autoRevalidate = newAutoRevalidate;

if (newAutoRevalidate) {
this._autoRevalidateInterval = setInterval(() => {
this.revalidate();
}, this._ttl);
} else {
this._autoRevalidateInterval = null;
}
}

clear() {
this._data = null;
this._cacheTimestamp = null;
this._state = 'stale';
}

reset() {
this._data = null;
this._cacheTimestamp = null;
this._state = 'initial';

this.changeAutoRevalidate(false);
}

changeTTL(newTTL: CacheTTL) {
this._ttl = CacheQuery.calculateTTLms(newTTL) || 0;

this.changeAutoRevalidate(this._autoRevalidate);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to remove methods that are not needed yet

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also have a question
Should I use FinalizationRegistry to clean up interval that auto revalidates cache in case of destroying query class instance?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We seem to have an instance of cache class that exists for the entire life of the application, so I don't think we should bother with it now
I'd even remove autoRevalidate for now, since we're not using it right now :)

(ttl?.seconds ?? 0) * 1000 +
(ttl?.minutes ?? 0) * 60_000 +
(ttl?.hours ?? 0) * 3_600_000 +
(ttl?.days ?? 0) * 86_400_000 || 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe its better to use || Infinity here?
So cache without ttl will be cache without updating
Now it will be cache that updates on every call

Comment on lines 105 to 109
if (this._cacheTimestamp === null || this._data === null) {
this._state = 'stale';

return this._state;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we need to remove this branch
_cacheTimestamp could not be null in state fresh

And if the data in the "fresh" state is null, doesn't that mean that's what the query returned null and we don't need to update data?

return this._data;
}

return this._currentQueryPromise;
Copy link
Collaborator

@stepanenkoxx stepanenkoxx Feb 4, 2026

Choose a reason for hiding this comment

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

_currentQueryPromise could be null, because of this code

finally {
            this._currentQueryPromise = null;
}

so i think it's better to make here something like

await this._currentQueryPromise;
return this._data;

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not entirely sure what the problem might be here, but I would fix it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed unused auto revalidation
And added await to currentQueryPromise

@Yaros1ove Yaros1ove merged commit 5d17157 into main Feb 4, 2026
2 checks passed
@Yaros1ove Yaros1ove deleted the DATAUI-3526_api-cache-class branch February 4, 2026 12:18
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.

2 participants