@@ -5,6 +5,7 @@ import { CacheBusMessageType } from '../types/main.js'
5
5
import { cacheEvents } from '../events/cache_events.js'
6
6
import type { CacheProvider } from '../types/provider.js'
7
7
import { GetSetHandler } from './get_set/get_set_handler.js'
8
+ import type { BentoCacheOptions } from '../bento_cache_options.js'
8
9
import type {
9
10
Factory ,
10
11
ClearOptions ,
@@ -25,11 +26,13 @@ export class Cache implements CacheProvider {
25
26
26
27
#getSetHandler: GetSetHandler
27
28
#stack: CacheStack
29
+ #options: BentoCacheOptions
28
30
29
31
constructor ( name : string , stack : CacheStack ) {
30
32
this . name = name
31
33
32
34
this . #stack = stack
35
+ this . #options = stack . options
33
36
this . #getSetHandler = new GetSetHandler ( this . #stack)
34
37
}
35
38
@@ -45,54 +48,61 @@ export class Cache implements CacheProvider {
45
48
}
46
49
47
50
get < T = any > ( options : GetOptions < T > ) : Promise < T >
48
- async get < T = any > ( keyOrOptions : GetOptions < T > ) : Promise < T | undefined | null > {
49
- const key = keyOrOptions . key
50
- const providedOptions = keyOrOptions
51
- const defaultValueFn = this . #resolveDefaultValue( keyOrOptions . defaultValue )
51
+ async get < T = any > ( rawOptions : GetOptions < T > ) : Promise < T | undefined | null > {
52
+ const key = rawOptions . key
53
+ const defaultValueFn = this . #resolveDefaultValue( rawOptions . defaultValue )
52
54
53
- const options = this . #stack. defaultOptions . cloneWith ( providedOptions )
54
- const localItem = this . #stack . l1 ?. get ( key , options )
55
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
56
+ this . #options . logger . logMethod ( { method : 'get' , key, options, cacheName : this . name } )
55
57
56
- if ( localItem !== undefined && ! localItem . isLogicallyExpired ( ) ) {
57
- this . #stack. emit ( cacheEvents . hit ( key , localItem . getValue ( ) , this . name ) )
58
- return localItem . getValue ( )
58
+ const localItem = this . #stack. l1 ?. get ( key , options )
59
+ if ( localItem ?. isGraced === false ) {
60
+ this . #stack. emit ( cacheEvents . hit ( key , localItem . entry . getValue ( ) , this . name ) )
61
+ this . #options. logger . logL1Hit ( { cacheName : this . name , key, options } )
62
+ return localItem . entry . getValue ( )
59
63
}
60
64
61
65
const remoteItem = await this . #stack. l2 ?. get ( key , options )
62
66
63
- if ( remoteItem !== undefined && ! remoteItem . isLogicallyExpired ( ) ) {
64
- this . #stack. l1 ?. set ( key , remoteItem . serialize ( ) , options )
65
- this . #stack. emit ( cacheEvents . hit ( key , remoteItem . getValue ( ) , this . name ) )
66
- return remoteItem . getValue ( )
67
- }
68
-
69
- if ( ! options . isGraceEnabled ( ) ) {
70
- this . #stack. emit ( cacheEvents . miss ( key , this . name ) )
71
- return this . #resolveDefaultValue( defaultValueFn )
67
+ if ( remoteItem ?. isGraced === false ) {
68
+ this . #stack. l1 ?. set ( key , remoteItem . entry . serialize ( ) , options )
69
+ this . #stack. emit ( cacheEvents . hit ( key , remoteItem . entry . getValue ( ) , this . name ) )
70
+ this . #options. logger . logL2Hit ( { cacheName : this . name , key, options } )
71
+ return remoteItem . entry . getValue ( )
72
72
}
73
73
74
- if ( remoteItem ) {
75
- this . #stack. l1 ?. set ( key , remoteItem . serialize ( ) , options )
76
- this . #stack. emit ( cacheEvents . hit ( key , remoteItem . serialize ( ) , this . name , true ) )
77
- return remoteItem . getValue ( )
74
+ if ( remoteItem && options . isGraceEnabled ( ) ) {
75
+ this . #stack. l1 ?. set ( key , remoteItem . entry . serialize ( ) , options )
76
+ this . #stack. emit ( cacheEvents . hit ( key , remoteItem . entry . serialize ( ) , this . name , true ) )
77
+ this . #options. logger . logL2Hit ( { cacheName : this . name , key, options, graced : true } )
78
+ return remoteItem . entry . getValue ( )
78
79
}
79
80
80
- if ( localItem ) {
81
- this . #stack. emit ( cacheEvents . hit ( key , localItem . serialize ( ) , this . name , true ) )
82
- return localItem . getValue ( )
81
+ if ( localItem && options . isGraceEnabled ( ) ) {
82
+ this . #stack. emit ( cacheEvents . hit ( key , localItem . entry . serialize ( ) , this . name , true ) )
83
+ this . #options. logger . logL1Hit ( { cacheName : this . name , key, options, graced : true } )
84
+ return localItem . entry . getValue ( )
83
85
}
84
86
85
87
this . #stack. emit ( cacheEvents . miss ( key , this . name ) )
88
+ this . #options. logger . debug ( { key, cacheName : this . name } , 'cache miss. using default value' )
86
89
return this . #resolveDefaultValue( defaultValueFn )
87
90
}
88
91
89
92
/**
90
93
* Set a value in the cache
91
94
* Returns true if the value was set, false otherwise
92
95
*/
93
- set ( options : SetOptions ) {
94
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
95
- return this . #stack. set ( options . key , options . value , cacheOptions )
96
+ set ( rawOptions : SetOptions ) {
97
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
98
+ this . #options. logger . logMethod ( {
99
+ method : 'set' ,
100
+ options,
101
+ key : rawOptions . key ,
102
+ cacheName : this . name ,
103
+ } )
104
+
105
+ return this . #stack. set ( rawOptions . key , rawOptions . value , options )
96
106
}
97
107
98
108
/**
@@ -107,28 +117,41 @@ export class Cache implements CacheProvider {
107
117
* Retrieve an item from the cache if it exists, otherwise store the value
108
118
* provided by the factory and return it
109
119
*/
110
- getOrSet < T > ( options : GetOrSetOptions < T > ) : Promise < T > {
111
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
112
- return this . #getSetHandler. handle ( options . key , options . factory , cacheOptions )
120
+ getOrSet < T > ( rawOptions : GetOrSetOptions < T > ) : Promise < T > {
121
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
122
+ this . #options. logger . logMethod ( {
123
+ method : 'getOrSet' ,
124
+ key : rawOptions . key ,
125
+ cacheName : this . name ,
126
+ options,
127
+ } )
128
+
129
+ return this . #getSetHandler. handle ( rawOptions . key , rawOptions . factory , options )
113
130
}
114
131
115
132
/**
116
133
* Retrieve an item from the cache if it exists, otherwise store the value
117
134
* provided by the factory forever and return it
118
135
*/
119
- getOrSetForever < T > ( options : GetOrSetForeverOptions < T > ) : Promise < T > {
120
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( { ttl : null , ...options } )
121
- return this . #getSetHandler. handle ( options . key , options . factory , cacheOptions )
136
+ getOrSetForever < T > ( rawOptions : GetOrSetForeverOptions < T > ) : Promise < T > {
137
+ const options = this . #stack. defaultOptions . cloneWith ( { ttl : null , ...rawOptions } )
138
+ return this . #getSetHandler. handle ( rawOptions . key , rawOptions . factory , options )
122
139
}
123
140
124
141
/**
125
142
* Check if a key exists in the cache
126
143
*/
127
144
async has ( options : HasOptions ) {
128
145
const key = options . key
129
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
130
-
131
- const inRemote = await this . #stack. l2 ?. has ( key , cacheOptions )
146
+ const entryOptions = this . #stack. defaultOptions . cloneWith ( options )
147
+ this . #options. logger . logMethod ( {
148
+ method : 'has' ,
149
+ key,
150
+ cacheName : this . name ,
151
+ options : entryOptions ,
152
+ } )
153
+
154
+ const inRemote = await this . #stack. l2 ?. has ( key , entryOptions )
132
155
const inLocal = this . #stack. l1 ?. has ( key )
133
156
134
157
return ! ! ( inRemote || inLocal )
@@ -155,12 +178,13 @@ export class Cache implements CacheProvider {
155
178
* Delete a key from the cache, emit cache:deleted event and
156
179
* publish invalidation through the bus
157
180
*/
158
- async delete ( options : DeleteOptions ) : Promise < boolean > {
159
- const key = options . key
160
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
181
+ async delete ( rawOptions : DeleteOptions ) : Promise < boolean > {
182
+ const key = rawOptions . key
183
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
184
+ this . #options. logger . logMethod ( { method : 'delete' , key, cacheName : this . name , options } )
161
185
162
- this . #stack. l1 ?. delete ( key , cacheOptions )
163
- await this . #stack. l2 ?. delete ( key , cacheOptions )
186
+ this . #stack. l1 ?. delete ( key , options )
187
+ await this . #stack. l2 ?. delete ( key , options )
164
188
165
189
this . #stack. emit ( cacheEvents . deleted ( key , this . name ) )
166
190
await this . #stack. publish ( { type : CacheBusMessageType . Delete , keys : [ key ] } )
@@ -173,12 +197,18 @@ export class Cache implements CacheProvider {
173
197
* Then emit cache:deleted events for each key
174
198
* And finally publish invalidation through the bus
175
199
*/
176
- async deleteMany ( options : DeleteManyOptions ) : Promise < boolean > {
177
- const keys = options . keys
178
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
179
-
180
- this . #stack. l1 ?. deleteMany ( keys , cacheOptions )
181
- await this . #stack. l2 ?. deleteMany ( keys , cacheOptions )
200
+ async deleteMany ( rawOptions : DeleteManyOptions ) : Promise < boolean > {
201
+ const keys = rawOptions . keys
202
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
203
+ this . #options. logger . logMethod ( {
204
+ method : 'deleteMany' ,
205
+ key : keys ,
206
+ cacheName : this . name ,
207
+ options,
208
+ } )
209
+
210
+ this . #stack. l1 ?. deleteMany ( keys , options )
211
+ await this . #stack. l2 ?. deleteMany ( keys , options )
182
212
183
213
keys . forEach ( ( key ) => this . #stack. emit ( cacheEvents . deleted ( key , this . name ) ) )
184
214
await this . #stack. publish ( { type : CacheBusMessageType . Delete , keys } )
@@ -189,12 +219,13 @@ export class Cache implements CacheProvider {
189
219
/**
190
220
* Remove all items from the cache
191
221
*/
192
- async clear ( options ?: ClearOptions ) {
193
- const cacheOptions = this . #stack. defaultOptions . cloneWith ( options )
222
+ async clear ( rawOptions ?: ClearOptions ) {
223
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
224
+ this . #options. logger . logMethod ( { method : 'clear' , cacheName : this . name , options } )
194
225
195
226
await Promise . all ( [
196
227
this . #stack. l1 ?. clear ( ) ,
197
- this . #stack. l2 ?. clear ( cacheOptions ) ,
228
+ this . #stack. l2 ?. clear ( options ) ,
198
229
this . #stack. publish ( { type : CacheBusMessageType . Clear , keys : [ ] } ) ,
199
230
] )
200
231
0 commit comments