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

Simplifies logic of LRUDiskCache #527

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- Simplifies logic of LRUDiskCache.

## [6.45.15] - 2023-02-01

### Changed

- Removed unnecessary worker signal logs

## [6.45.14] - 2023-02-01
Expand All @@ -23,7 +29,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Do not return empty content if no conflict is found.

## [6.45.13] - 2022-12-12

### Changed

- Deterministic cache for getWithBody
- Remove socket metrics per origin

Expand Down Expand Up @@ -1756,7 +1764,6 @@ instead

- `HttpClient` now adds `'Accept-Encoding': 'gzip'` header by default.


[Unreleased]: https://github.com/vtex/node-vtex-api/compare/v6.45.15...HEAD
[6.45.15]: https://github.com/vtex/node-vtex-api/compare/v6.45.14...v6.45.15
[6.45.14]: https://github.com/vtex/node-vtex-api/compare/v6.45.13...v6.45.14
Expand Down
22 changes: 3 additions & 19 deletions src/caches/LRUDiskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
private hits = 0
private total = 0
private lruStorage: LRU<string, number>
private keyToBeDeleted: string

constructor(private cachePath: string, options: LRUDiskCacheOptions, private readFile=readJSON, private writeFile=outputJSON) {
this.hits = 0
this.total = 0
this.disposed = 0
this.keyToBeDeleted = ''
this.lock = new ReadWriteLock()

const dispose = (key: string): void => {
this.keyToBeDeleted = key
this.disposed += 1
this.deleteFile(key)
}

const lruOptions = {
Expand Down Expand Up @@ -59,18 +57,10 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
public get = async (key: string): Promise<V | void> => {
const timeOfDeath = this.lruStorage.get(key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Confused when I read this. The items stored in the cache are timestamps?
🤔

Copy link
Author

@danzanzini danzanzini Jun 7, 2023

Choose a reason for hiding this comment

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

Yes! It took me a while to understand it, too. I'm still unsure if we should keep this behavior, but I don't want to change many things simultaneously.

this.total += 1
if (timeOfDeath === undefined) {

// if it is an outdated file when stale=false
if (this.keyToBeDeleted) {
await this.deleteFile(key)
}
return undefined
}

const pathKey = this.getPathKey(key)

const data = await new Promise<V>(resolve => {
const data = await new Promise<V | undefined>(resolve => {
this.lock.readLock(key, async (release: () => void) => {
try {
const fileData = await this.readFile(pathKey)
Expand All @@ -85,9 +75,8 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
})

// if it is an outdated file when stale=true
if (timeOfDeath < Date.now()) {
if (timeOfDeath && timeOfDeath < Date.now()) {
this.lruStorage.del(key)
await this.deleteFile(key)
}

return data
Expand All @@ -103,10 +92,6 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
this.lruStorage.set(key, NaN)
}

if (this.keyToBeDeleted && this.keyToBeDeleted !== key) {
await this.deleteFile(this.keyToBeDeleted)
}

const pathKey = this.getPathKey(key)
const failure = await new Promise<void | boolean>(resolve => {
this.lock.writeLock(key, async (release: () => void) => {
Expand All @@ -130,7 +115,6 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{

private deleteFile = async (key: string): Promise<boolean> => {
const pathKey = this.getPathKey(key)
this.keyToBeDeleted = ''
const failure = new Promise<void | boolean>(resolve => {
this.lock.writeLock(key, async (release: () => void) => {
try {
Expand Down