@@ -17,6 +17,7 @@ import type {
17
17
DeleteManyOptions ,
18
18
GetOrSetForeverOptions ,
19
19
ExpireOptions ,
20
+ DeleteByTagOptions ,
20
21
} from '../types/main.js'
21
22
22
23
export class Cache implements CacheProvider {
@@ -35,6 +36,7 @@ export class Cache implements CacheProvider {
35
36
this . #stack = stack
36
37
this . #options = stack . options
37
38
this . #getSetHandler = new GetSetHandler ( this . #stack)
39
+ this . #stack. setTagSystemGetSetHandler ( this . #getSetHandler)
38
40
}
39
41
40
42
#resolveDefaultValue( defaultValue ?: Factory ) {
@@ -57,19 +59,21 @@ export class Cache implements CacheProvider {
57
59
this . #options. logger . logMethod ( { method : 'get' , key, options, cacheName : this . name } )
58
60
59
61
const localItem = this . #stack. l1 ?. get ( key , options )
60
- if ( localItem ?. isGraced === false ) {
61
- this . #stack. emit ( cacheEvents . hit ( key , localItem . entry . getValue ( ) , this . name ) )
62
+ const isLocalItemValid = await this . #stack. isEntryValid ( localItem )
63
+ if ( isLocalItemValid ) {
64
+ this . #stack. emit ( cacheEvents . hit ( key , localItem ! . entry . getValue ( ) , this . name ) )
62
65
this . #options. logger . logL1Hit ( { cacheName : this . name , key, options } )
63
- return localItem . entry . getValue ( )
66
+ return localItem ! . entry . getValue ( )
64
67
}
65
68
66
69
const remoteItem = await this . #stack. l2 ?. get ( key , options )
70
+ const isRemoteItemValid = await this . #stack. isEntryValid ( remoteItem )
67
71
68
- if ( remoteItem ?. isGraced === false ) {
69
- this . #stack. l1 ?. set ( key , remoteItem . entry . serialize ( ) , options )
70
- this . #stack. emit ( cacheEvents . hit ( key , remoteItem . entry . getValue ( ) , this . name ) )
72
+ if ( isRemoteItemValid ) {
73
+ this . #stack. l1 ?. set ( key , remoteItem ! . entry . serialize ( ) , options )
74
+ this . #stack. emit ( cacheEvents . hit ( key , remoteItem ! . entry . getValue ( ) , this . name ) )
71
75
this . #options. logger . logL2Hit ( { cacheName : this . name , key, options } )
72
- return remoteItem . entry . getValue ( )
76
+ return remoteItem ! . entry . getValue ( )
73
77
}
74
78
75
79
if ( remoteItem && options . isGraceEnabled ( ) ) {
@@ -193,6 +197,18 @@ export class Cache implements CacheProvider {
193
197
return true
194
198
}
195
199
200
+ /**
201
+ * Invalidate all keys with the given tags
202
+ */
203
+ async deleteByTag ( rawOptions : DeleteByTagOptions ) : Promise < boolean > {
204
+ const tags = rawOptions . tags
205
+ const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
206
+
207
+ this . #options. logger . logMethod ( { method : 'deleteByTag' , cacheName : this . name , tags, options } )
208
+
209
+ return await this . #stack. createTagInvalidations ( tags )
210
+ }
211
+
196
212
/**
197
213
* Delete multiple keys from local and remote cache
198
214
* Then emit cache:deleted events for each key
@@ -222,17 +238,12 @@ export class Cache implements CacheProvider {
222
238
* Entry will not be fully deleted but expired and
223
239
* retained for the grace period if enabled.
224
240
*/
225
- async expire ( rawOptions : ExpireOptions ) {
241
+ expire ( rawOptions : ExpireOptions ) {
226
242
const key = rawOptions . key
227
243
const options = this . #stack. defaultOptions . cloneWith ( rawOptions )
228
244
this . #options. logger . logMethod ( { method : 'expire' , cacheName : this . name , key, options } )
229
245
230
- this . #stack. l1 ?. logicallyExpire ( key , options )
231
- await this . #stack. l2 ?. logicallyExpire ( key , options )
232
- await this . #stack. publish ( { type : CacheBusMessageType . Expire , keys : [ key ] } )
233
-
234
- this . #stack. emit ( cacheEvents . expire ( key , this . name ) )
235
- return true
246
+ return this . #stack. expire ( key , options )
236
247
}
237
248
238
249
/**
0 commit comments