Skip to content

Commit

Permalink
Clarify LRUCache usage (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Bond authored May 5, 2023
1 parent e647b21 commit 68d576c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Clarification about `LRUCache` usage

## [0.2.21] - 2023-02-24

### Fixed
Expand Down
10 changes: 10 additions & 0 deletions node/clients/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ export default class Status extends ExternalClient {
metric: 'status-get-raw',
})
}

public async getStatusAndForceMaxAge(
status: number
): Promise<IOResponse<string>> {
return this.http.get(status.toString(), {
// when using an LRUCache, this will force the response to be cached
forceMaxAge: 5000,
metric: 'status-get-forceMaxAge',
})
}
}
4 changes: 4 additions & 0 deletions node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { validate } from './middlewares/validate'
const TIMEOUT_MS = 800

// Create a LRU memory cache for the Status client.
// The 'max' parameter sets the size of the cache.
// The @vtex/api HttpClient respects Cache-Control headers and uses the provided cache.
// Note that the response from the API being called must include an 'etag' header
// or a 'cache-control' header with a 'max-age' value. If neither exist, the response will not be cached.
// To force responses to be cached, consider adding the `forceMaxAge` option to your client methods.
const memoryCache = new LRUCache<string, any>({ max: 5000 })

metrics.trackCache('status', memoryCache)
Expand Down

0 comments on commit 68d576c

Please sign in to comment.