Skip to content

Commit 01b8f5b

Browse files
committed
Revert "fix: force factory re-execution when tags change"
This reverts commit c6e4758.
1 parent c6e4758 commit 01b8f5b

6 files changed

Lines changed: 13 additions & 139 deletions

File tree

.changeset/rotten-facts-love.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/bentocache/src/cache/cache_stack.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,39 +173,23 @@ export class CacheStack extends BaseDriver {
173173
return true
174174
}
175175

176-
/**
177-
* Compare two arrays of tags to check if they are identical
178-
*
179-
* See https://github.com/Julien-R44/bentocache/issues/83 for more details
180-
* about why we need to compare tags.
181-
*/
182-
#areTagsMatching(cachedTags: string[], newTags: string[] = []): boolean {
183-
if (cachedTags.length !== newTags?.length) return false
184-
185-
return cachedTags.every((tag) => newTags.includes(tag))
186-
}
187-
188176
/**
189177
* Check if an item is valid.
190178
* Valid means :
191179
* - Logically not expired ( not graced )
192180
* - Not invalidated by a tag
193-
* - Tags match with current options (if provided)
194181
*/
195-
isEntryValid(
196-
item: GetCacheValueReturn | undefined,
197-
options?: { tags?: string[] },
198-
): Promise<boolean> | boolean {
199-
if (!item || item.isGraced) return false
182+
isEntryValid(item: GetCacheValueReturn | undefined): Promise<boolean> | boolean {
183+
if (!item) return false
200184

201-
const cachedTags = item.entry.getTags()
185+
const isGraced = item?.isGraced === true
186+
if (isGraced) return false
202187

203-
if (!this.#areTagsMatching(cachedTags, options?.tags)) return false
204-
if (!cachedTags.length) return true
188+
if (item.entry.getTags().length === 0) return true
205189

206-
return this.#tagSystem
207-
.isTagInvalidated(item.entry)
208-
.then((isTagInvalidated) => !isTagInvalidated)
190+
return this.#tagSystem.isTagInvalidated(item.entry).then((isTagInvalidated) => {
191+
return !isTagInvalidated
192+
})
209193
}
210194

211195
/**

packages/bentocache/src/cache/get_set/single_tier_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class SingleTierHandler {
103103

104104
if (!options.forceFresh) {
105105
remoteItem = await this.stack.l2?.get(key, options)
106-
isRemoteItemValid = await this.stack.isEntryValid(remoteItem, options)
106+
isRemoteItemValid = await this.stack.isEntryValid(remoteItem)
107107
if (isRemoteItemValid) {
108108
return this.#returnRemoteCacheValue(key, remoteItem!, options)
109109
}
@@ -126,7 +126,7 @@ export class SingleTierHandler {
126126
*/
127127
if (!options.forceFresh) {
128128
remoteItem = await this.stack.l2?.get(key, options)
129-
isRemoteItemValid = await this.stack.isEntryValid(remoteItem, options)
129+
isRemoteItemValid = await this.stack.isEntryValid(remoteItem)
130130
if (isRemoteItemValid) {
131131
this.#locks.release(key, releaser)
132132
return this.#returnRemoteCacheValue(key, remoteItem!, options)

packages/bentocache/src/cache/get_set/two_tier_handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class TwoTierHandler {
125125
* while we were waiting for the lock.
126126
*/
127127
localItem = this.stack.l1?.get(key, options)
128-
const isLocalItemValid = await this.stack.isEntryValid(localItem, options)
128+
const isLocalItemValid = await this.stack.isEntryValid(localItem)
129129
if (isLocalItemValid) {
130130
this.#locks.release(key, releaser)
131131
return this.#returnL1Value(key, localItem!)
@@ -135,7 +135,7 @@ export class TwoTierHandler {
135135
* Check remote cache in case something was written there
136136
*/
137137
remoteItem = await this.stack.l2?.get(key, options)
138-
const isRemoteItemValid = await this.stack.isEntryValid(remoteItem, options)
138+
const isRemoteItemValid = await this.stack.isEntryValid(remoteItem)
139139
if (isRemoteItemValid) {
140140
this.#locks.release(key, releaser)
141141
return this.#returnRemoteCacheValue(key, remoteItem!, options)
@@ -186,7 +186,7 @@ export class TwoTierHandler {
186186
* returns it without acquiring a lock.
187187
*/
188188
const localItem = this.stack.l1?.get(key, options)
189-
const isLocalItemValid = this.stack.isEntryValid(localItem, options)
189+
const isLocalItemValid = this.stack.isEntryValid(localItem)
190190

191191
// A bit nasty, but to keep maximum performance, we avoid async/await here.
192192
// Let's check for a better way to handle this later.

packages/bentocache/tests/cache/one_tier_local.spec.ts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -526,74 +526,4 @@ test.group('One tier tests', () => {
526526
// Original value should still be intact
527527
assert.equal(await cache.get({ key: 'key1' }), 'initial')
528528
})
529-
530-
test('getOrSet() should recalculate value when tags change', async ({ assert }) => {
531-
const { cache } = new CacheFactory().withMemoryL1().create()
532-
533-
let factoryCallCount = 0
534-
const factory = () => {
535-
factoryCallCount++
536-
return `value-${factoryCallCount}`
537-
}
538-
539-
// First call with specific tags
540-
const result1 = await cache.getOrSet({
541-
key: 'test-key',
542-
factory,
543-
tags: ['labels', 'settings'],
544-
ttl: '5m',
545-
})
546-
547-
assert.equal(result1, 'value-1')
548-
assert.equal(factoryCallCount, 1)
549-
550-
// Second call with same key but different tags
551-
// This should trigger the factory again because tags have changed
552-
const result2 = await cache.getOrSet({
553-
key: 'test-key',
554-
factory,
555-
tags: ['labels', 'datasets', 'settings'],
556-
ttl: '5m',
557-
})
558-
559-
assert.equal(result2, 'value-2')
560-
assert.equal(factoryCallCount, 2)
561-
})
562-
563-
test('should be able to deleteByTag if tags changed', async ({ assert }) => {
564-
const { cache } = new CacheFactory().withMemoryL1().create()
565-
566-
let factoryCallCount = 0
567-
const factory = () => {
568-
factoryCallCount++
569-
return `value-${factoryCallCount}`
570-
}
571-
572-
const result1 = await cache.getOrSet({
573-
key: 'test-key',
574-
factory,
575-
tags: ['labels', 'settings'],
576-
ttl: '5m',
577-
})
578-
579-
const result2 = await cache.getOrSet({
580-
key: 'test-key',
581-
factory,
582-
tags: ['labels', 'datasets', 'settings'],
583-
ttl: '5m',
584-
})
585-
586-
await cache.deleteByTag({ tags: ['datasets'] })
587-
588-
const result3 = await cache.getOrSet({
589-
key: 'test-key',
590-
factory,
591-
tags: ['labels', 'datasets', 'settings'],
592-
ttl: '5m',
593-
})
594-
595-
assert.equal(result1, 'value-1')
596-
assert.equal(result2, 'value-2')
597-
assert.equal(result3, 'value-3')
598-
})
599529
})

packages/bentocache/tests/cache/two_tier.spec.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -904,39 +904,4 @@ test.group('Cache', () => {
904904
assert.equal(l1Value?.entry.getValue(), 'initial')
905905
assert.equal(l2Value?.entry.getValue(), 'initial')
906906
})
907-
908-
test('getOrSet() should recalculate value when tags change in L1+L2 setup', async ({
909-
assert,
910-
}) => {
911-
const { cache } = new CacheFactory().withL1L2Config().create()
912-
913-
let factoryCallCount = 0
914-
const factory = () => {
915-
factoryCallCount++
916-
return `value-${factoryCallCount}`
917-
}
918-
919-
// First call with specific tags
920-
const result1 = await cache.getOrSet({
921-
key: 'test-key',
922-
factory,
923-
tags: ['tag1', 'tag2'],
924-
ttl: '5m',
925-
})
926-
927-
assert.equal(result1, 'value-1')
928-
assert.equal(factoryCallCount, 1)
929-
930-
// Second call with same key but different tags
931-
// This should trigger the factory again because tags have changed
932-
const result2 = await cache.getOrSet({
933-
key: 'test-key',
934-
factory,
935-
tags: ['tag1', 'tag3'],
936-
ttl: '5m',
937-
})
938-
939-
assert.equal(result2, 'value-2')
940-
assert.equal(factoryCallCount, 2)
941-
})
942907
})

0 commit comments

Comments
 (0)