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

Change LRUDiskCache to allow subclasses to override functionality #554

Open
wants to merge 5 commits into
base: feat/cacheOptions
Choose a base branch
from

Conversation

mairatma
Copy link
Contributor

@mairatma mairatma commented Nov 8, 2023

What is the purpose of this pull request?

This changes LRUDiskCache so that a subclass can properly override some of its functionality (such as the has/get/set methods, stats calculations, etc).

What problem is this solving?

We need to build a new disk cache implementation with extra features. To avoid reimplementing everything again, we can instead extend from LRUDiskCache and override some of its methods to achieve what we need. But the way LRUDiskCache was created didn't allow for these extensions, so this PR is changing it to allow better usage by subclasses.

How should this be manually tested?

  • Build the codebase and verify that there were no errors.
  • Create a new class extending from LRUDiskCache and make sure that there are no errors when overriding the methods.
  • Link this npm package into an app, within a linked workspace, and make sure that http client calls are working as before.

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Requires change to documentation, which has been updated accordingly.

return this.lruStorage
}

public has (key: string): boolean {
Copy link
Contributor Author

@mairatma mairatma Nov 8, 2023

Choose a reason for hiding this comment

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

I changed the has/get/set methods syntax because the previous syntax doesn't work properly when a subclass overrides, losing the access to super.

No changes were made in this one besides the syntax though.

@@ -85,23 +103,18 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
})

// if it is an outdated file when stale=true
if (timeOfDeath < Date.now()) {
if (lruData.timeOfDeath < Date.now()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The timeOfDeath is now part of the data retrieved from the memomery cache (lruStorage), instead of being the value itself.

Comment on lines +127 to +134
/**
* Builds the data object that will be stored at the LRU memory storage.
* Subclasses that need to store more than just the time of death should
* override this.
*/
protected buildLRUData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData {
return { timeOfDeath }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The data stored in the memory part of the cache was only the timeOfDeath. This new method will allow storing more data there, for any logic that needs to run before deciding to fetch the file from disk.

In our use case this extra data will be the list of fields that we have stored from the original object.

}
public async set (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise<boolean> {
const timeOfDeath = maxAge ? maxAge + Date.now() : NaN
const lruData = this.buildLRUData(timeOfDeath, localCacheOptions)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just changed the way we build the data to be stored in the memory cache to use the new buildLRUData function.

@mairatma mairatma marked this pull request as ready for review November 8, 2023 13:59
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.

1 participant