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

fix: 🐛 remove duplicate ttl conversion from s to ms #710

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/clean-impalas-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sebspark/promise-cache": patch
---

fix issue where ttl was being converted to ms twice
8 changes: 4 additions & 4 deletions packages/promise-cache/src/promiseCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('PromiseCache', () => {
// Cache should be set with the TTL from the response
expect(mockedPersistorSet).toHaveBeenCalledWith('testkey4', {
timestamp: expect.any(Number),
ttl: 112312000,
ttl: 112312,
value: {
value: 42,
ttl: '112312',
Expand All @@ -171,7 +171,7 @@ describe('PromiseCache', () => {

expect(mockedPersistorSet).toHaveBeenCalledWith('testkey5', {
timestamp: expect.any(Number),
ttl: 1000,
ttl: 1,
value: {
value: 42,
ttl: '112adsa3a12',
Expand All @@ -191,7 +191,7 @@ describe('PromiseCache', () => {

expect(mockedPersistorSet).toHaveBeenCalledWith('testkey6', {
timestamp: expect.any(Number),
ttl: 1000,
ttl: 1,
value: {
value: 42,
},
Expand All @@ -208,7 +208,7 @@ describe('PromiseCache', () => {

expect(mockedPersistorSet).toHaveBeenCalledWith('testkey7', {
timestamp: expect.any(Number),
ttl: 1000,
ttl: 1,
value: 42,
})
})
Expand Down
7 changes: 3 additions & 4 deletions packages/promise-cache/src/promiseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class PromiseCache<U> {
})
this.caseSensitive = caseSensitive
if (ttlInSeconds) {
this.ttl = ttlInSeconds * 1000 // Convert seconds to milliseconds.
this.ttl = ttlInSeconds // Conversion to milliseconds is done in the persistor.
}
}

Expand Down Expand Up @@ -140,8 +140,7 @@ export class PromiseCache<U> {
const effectiveKey = this.caseSensitive ? key : key.toLowerCase()

// Determine the TTL and unique cache key for this specific call.
let effectiveTTL =
ttlInSeconds !== undefined ? ttlInSeconds * 1000 : this.ttl
let effectiveTTL = ttlInSeconds ?? this.ttl

const cached = await this.persistor.get<U>(effectiveKey)

Expand All @@ -161,7 +160,7 @@ export class PromiseCache<U> {
// Get the TTL from the response if a TTL key is provided.
if (ttlKeyInSeconds) {
const responseDict = response as Record<string, unknown>
const responseTTL = Number(responseDict[ttlKeyInSeconds] as string) * 1000
const responseTTL = Number(responseDict[ttlKeyInSeconds] as string)
effectiveTTL = responseTTL || effectiveTTL // Fall back to the default TTL if the TTL key is not found.
}

Expand Down
Loading