@@ -7,7 +7,7 @@ import { FactoryRunner } from '../factory_runner.js'
7
7
import type { Factory } from '../../types/helpers.js'
8
8
import type { CacheEvent } from '../../types/events.js'
9
9
import { cacheEvents } from '../../events/cache_events.js'
10
- import type { CacheEntry } from '../cache_entry/cache_entry .js'
10
+ import type { GetCacheValueReturn } from '../../types/internals/index .js'
11
11
import type { CacheEntryOptions } from '../cache_entry/cache_entry_options.js'
12
12
13
13
export class SingleTierHandler {
@@ -35,11 +35,15 @@ export class SingleTierHandler {
35
35
/**
36
36
* Returns a value from the remote cache and emit a CacheHit event
37
37
*/
38
- async #returnRemoteCacheValue( key : string , item : CacheEntry , options : CacheEntryOptions ) {
39
- this . logger . trace ( { key, cache : this . stack . name , opId : options . id } , 'remote cache hit' )
38
+ async #returnRemoteCacheValue(
39
+ key : string ,
40
+ item : GetCacheValueReturn ,
41
+ options : CacheEntryOptions ,
42
+ ) {
43
+ this . logger . logL2Hit ( { cacheName : this . stack . name , key, options } )
40
44
41
- this . #emit( cacheEvents . hit ( key , item . getValue ( ) , this . stack . name ) )
42
- return item . getValue ( )
45
+ this . #emit( cacheEvents . hit ( key , item . entry . getValue ( ) , this . stack . name ) )
46
+ return item . entry . getValue ( )
43
47
}
44
48
45
49
/**
@@ -55,27 +59,21 @@ export class SingleTierHandler {
55
59
56
60
#returnGracedValueOrThrow(
57
61
key : string ,
58
- item : CacheEntry | undefined ,
62
+ item : GetCacheValueReturn | undefined ,
59
63
options : CacheEntryOptions ,
60
64
err : Error ,
61
65
) {
62
66
if ( options . isGraceEnabled ( ) && item ) {
63
- const isLogicallyExpired = item . isLogicallyExpired ( )
64
- this . #emit( cacheEvents . hit ( key , item . getValue ( ) , this . stack . name , isLogicallyExpired ) )
65
- this . logger . trace (
66
- { key, cache : this . stack . name , opId : options . id } ,
67
- 'remote cache hit (graced)' ,
68
- )
69
-
70
- return item . getValue ( )
67
+ this . #emit( cacheEvents . hit ( key , item . entry . getValue ( ) , this . stack . name , item . isGraced ) )
68
+ return item . entry . getValue ( )
71
69
}
72
70
73
71
throw err
74
72
}
75
73
76
74
async #applyFallbackAndReturnGracedValue(
77
75
key : string ,
78
- item : CacheEntry ,
76
+ item : GetCacheValueReturn ,
79
77
options : CacheEntryOptions ,
80
78
) {
81
79
if ( options . grace && options . graceBackoff ) {
@@ -84,29 +82,26 @@ export class SingleTierHandler {
84
82
'apply fallback duration' ,
85
83
)
86
84
87
- this . stack . l2 ?. set ( key , item . applyBackoff ( options . graceBackoff ) . serialize ( ) as any , options )
85
+ this . stack . l2 ?. set (
86
+ key ,
87
+ item . entry . applyBackoff ( options . graceBackoff ) . serialize ( ) as any ,
88
+ options ,
89
+ )
88
90
}
89
91
90
92
this . logger . trace ( { key, cache : this . stack . name , opId : options . id } , 'returns stale value' )
91
- this . #emit( cacheEvents . hit ( key , item . getValue ( ) , this . stack . name , true ) )
92
- return item . getValue ( )
93
- }
94
-
95
- /**
96
- * Check if a cache item is not undefined and not logically expired
97
- */
98
- #isItemValid( item : CacheEntry | undefined ) : item is CacheEntry {
99
- return ! ! item && ! item . isLogicallyExpired ( )
93
+ this . #emit( cacheEvents . hit ( key , item . entry . getValue ( ) , this . stack . name , true ) )
94
+ return item . entry . getValue ( )
100
95
}
101
96
102
97
async handle ( key : string , factory : Factory , options : CacheEntryOptions ) {
103
- let remoteItem : CacheEntry | undefined
98
+ let remoteItem : GetCacheValueReturn | undefined
104
99
105
100
/**
106
101
* Check in the remote cache first if we have something
107
102
*/
108
103
remoteItem = await this . stack . l2 ?. get ( key , options )
109
- if ( this . #isItemValid ( remoteItem ) ) {
104
+ if ( remoteItem ?. isGraced === false ) {
110
105
return this . #returnRemoteCacheValue( key , remoteItem , options )
111
106
}
112
107
@@ -126,7 +121,7 @@ export class SingleTierHandler {
126
121
* already set the value
127
122
*/
128
123
remoteItem = await this . stack . l2 ?. get ( key , options )
129
- if ( this . #isItemValid ( remoteItem ) ) {
124
+ if ( remoteItem ?. isGraced === false ) {
130
125
this . #locks. release ( key , releaser )
131
126
return this . #returnRemoteCacheValue( key , remoteItem , options )
132
127
}
0 commit comments