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(cache-browser-local-storage): Implemented TTL support to cached items #1457

Merged

Conversation

SanderFlooris
Copy link

@SanderFlooris SanderFlooris commented Apr 21, 2023

Feature summary

This introduces the ability to set a TTL for cached items, in order to reduce the size of this cache and prevents the cache from permanently holding onto the items.

This feature is optional, and if the timeToLive option is not configured, it will behave like how it currently does.

Why?

We ran into storage quotas, and saw that this cache can grow to a couple MB after a while. We made our own patched version, but it would be nice if this feature was implemented upstream.

Providing this fix in the form of a pull request was discussed via the support.algolia.com/hc/requests/551781 support ticket.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 21, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 51f2510:

Sandbox Source
javascript-client-app Configuration

Comment on lines 38 to 40
if (!timeToLive) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

could return right after line 27 to save some more impact

Copy link
Author

Choose a reason for hiding this comment

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

@Haroenv It has been updated 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I meant right after creating the timeToLive variable, so after line 27, we wouldn't need to do the Object.entries etc.

Copy link
Author

Choose a reason for hiding this comment

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

Ah that's what you mean. This was done in order to deal with existing cache items that were made before this PR is merged.
Line 30 - 34 searches for old cache items and removes them, as they are no longer in a format that is supported.

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

good idea, thanks for the contribution!!

/**
* The time to live for each cached item in seconds.
*/
readonly ttl?: number;
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can go full length here and use timeToLive to make it easier for everyone, wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

Took a look at some of the other Options.ts files, and I do agree with this suggestion.
Not only does it make it a bit easier to read, it also makes it more consistent with other configurations.

Copy link
Author

Choose a reason for hiding this comment

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

It has been updated 👍

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Would you mind adding a basic test just to assert that it gets deleted?

@SanderFlooris
Copy link
Author

I've added 2 new tests for this feature, reads unexpired timeToLive keys and deletes expired keys.
I also updated creates a namespace within local storage and can be cleared, as they broke with this change.

Since this question might come up, I'll answer it in advance.
We need to compare if it's an empty object, this is because the cache.get that is above it will also run the removeOutdatedCacheItems function.
This will automatically create a new namespace, so for this reason I've moved that specific check to be above the cache.get, and added another one at the bottom to see if the namespace is empty.
Scherm­afbeelding 2023-04-28 om 10 16 16

@SanderFlooris
Copy link
Author

@shortcuts Are there any updates yet regarding this PR? Or is it still being processed internally?

@shortcuts
Copy link
Member

shortcuts commented May 5, 2023

@shortcuts Are there any updates yet regarding this PR? Or is it still being processed internally?

oh sorry I forgot, checking!

shortcuts
shortcuts previously approved these changes May 5, 2023
Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

looks good! thanks for the contribution :D I'll port it to the next major as stated here algolia/api-clients-automation#1489

@shortcuts shortcuts enabled auto-merge (squash) May 22, 2023 07:50
@SanderFlooris
Copy link
Author

@shortcuts Just making sure, I just have to update the bundlesize inside package.json right?

@shortcuts
Copy link
Member

@shortcuts Just making sure, I just have to update the bundlesize inside package.json right?

exactly!

auto-merge was automatically disabled June 12, 2023 07:30

Head branch was pushed to by a user without write access

@SanderFlooris
Copy link
Author

@shortcuts It has been updated 👍

@SanderFlooris
Copy link
Author

Hmm, seems like the bundle size is a bit bigger then when I locally ran the test.
Currently recompiling everything to see if it will fail the bundle test afterwards.

@SanderFlooris
Copy link
Author

After a fresh compile the size was indeed bigger than before, this has been fixed now

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@shortcuts shortcuts merged commit 9092414 into algolia:master Jun 12, 2023
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.

3 participants