@@ -3,6 +3,7 @@ import lodash from '@poppinss/utils/lodash'
3
3
import { Bus } from '../../bus/bus.js'
4
4
import { LocalCache } from '../facades/local_cache.js'
5
5
import { RemoteCache } from '../facades/remote_cache.js'
6
+ import { BaseDriver } from '../../drivers/base_driver.js'
6
7
import { JsonSerializer } from '../../serializers/json.js'
7
8
import type { BentoCacheOptions } from '../../bento_cache_options.js'
8
9
import { CacheEntryOptions } from '../cache_entry/cache_entry_options.js'
@@ -11,10 +12,11 @@ import type {
11
12
BusOptions ,
12
13
CacheEvent ,
13
14
CacheStackDrivers ,
15
+ CacheBusMessage ,
14
16
Logger ,
15
17
} from '../../types/main.js'
16
18
17
- export class CacheStack {
19
+ export class CacheStack extends BaseDriver {
18
20
#serializer = new JsonSerializer ( )
19
21
20
22
l1 ?: LocalCache
@@ -32,29 +34,31 @@ export class CacheStack {
32
34
drivers : CacheStackDrivers ,
33
35
bus ?: Bus ,
34
36
) {
37
+ super ( options )
35
38
this . logger = options . logger . child ( { cache : this . name } )
36
39
37
40
if ( drivers . l1Driver ) this . l1 = new LocalCache ( drivers . l1Driver , this . logger )
38
41
if ( drivers . l2Driver ) this . l2 = new RemoteCache ( drivers . l2Driver , this . logger )
39
42
40
- this . bus = this . #createBus( drivers . busDriver , bus , drivers . busOptions )
43
+ this . bus = bus ? bus : this . #createBus( drivers . busDriver , drivers . busOptions )
44
+ if ( this . l1 ) this . bus ?. manageCache ( this . prefix , this . l1 )
45
+
41
46
this . defaultOptions = new CacheEntryOptions ( options )
42
47
}
43
48
44
49
get emitter ( ) {
45
50
return this . options . emitter
46
51
}
47
52
48
- #createBus( busDriver ?: BusDriver , bus ?: Bus , busOptions ?: BusOptions ) {
49
- if ( bus ) return bus
50
- if ( ! busDriver || ! this . l1 ) return
53
+ #createBus( busDriver ?: BusDriver , busOptions ?: BusOptions ) {
54
+ if ( ! busDriver ) return
51
55
52
56
this . #busDriver = busDriver
53
57
this . #busOptions = lodash . merge (
54
58
{ retryQueue : { enabled : true , maxSize : undefined } } ,
55
59
busOptions ,
56
60
)
57
- const newBus = new Bus ( this . #busDriver , this . l1 , this . logger , this . emitter , this . #busOptions)
61
+ const newBus = new Bus ( this . name , this . #busDriver , this . logger , this . emitter , this . #busOptions)
58
62
59
63
return newBus
60
64
}
@@ -63,18 +67,31 @@ export class CacheStack {
63
67
if ( ! this . #namespaceCache. has ( namespace ) ) {
64
68
this . #namespaceCache. set (
65
69
namespace ,
66
- new CacheStack ( this . name , this . options , {
67
- l1Driver : this . l1 ?. namespace ( namespace ) ,
68
- l2Driver : this . l2 ?. namespace ( namespace ) ,
69
- busDriver : this . #busDriver,
70
- busOptions : { ...this . #busOptions, prefix : this . bus ?. namespace ( namespace ) } ,
71
- } ) ,
70
+ new CacheStack (
71
+ this . name ,
72
+ this . options . cloneWith ( { prefix : this . createNamespacePrefix ( namespace ) } ) ,
73
+ {
74
+ l1Driver : this . l1 ?. namespace ( namespace ) ,
75
+ l2Driver : this . l2 ?. namespace ( namespace ) ,
76
+ } ,
77
+ this . bus ,
78
+ ) ,
72
79
)
73
80
}
74
81
75
82
return < CacheStack > this . #namespaceCache. get ( namespace )
76
83
}
77
84
85
+ /**
86
+ * Publish a message to the bus channel
87
+ *
88
+ * @returns true if the message was published, false if not
89
+ * and undefined if a bus is not part of the stack
90
+ */
91
+ async publish ( message : CacheBusMessage ) : Promise < boolean | undefined > {
92
+ return this . bus ?. publish ( { ...message , namespace : this . prefix } )
93
+ }
94
+
78
95
emit ( event : CacheEvent ) {
79
96
return this . emitter . emit ( event . name , event . toJSON ( ) )
80
97
}
0 commit comments