@@ -32,13 +32,15 @@ class Connection
32
32
'group ' => 'symfony ' ,
33
33
'consumer ' => 'consumer ' ,
34
34
'auto_setup ' => true ,
35
+ 'stream_max_entries ' => 0 , // any value higher than 0 defines an approximate maximum number of stream entries
35
36
];
36
37
37
38
private $ connection ;
38
39
private $ stream ;
39
40
private $ group ;
40
41
private $ consumer ;
41
42
private $ autoSetup ;
43
+ private $ maxEntries ;
42
44
private $ couldHavePendingMessages = true ;
43
45
44
46
public function __construct (array $ configuration , array $ connectionCredentials = [], array $ redisOptions = [], \Redis $ redis = null )
@@ -50,6 +52,7 @@ public function __construct(array $configuration, array $connectionCredentials =
50
52
$ this ->group = $ configuration ['group ' ] ?? self ::DEFAULT_OPTIONS ['group ' ];
51
53
$ this ->consumer = $ configuration ['consumer ' ] ?? self ::DEFAULT_OPTIONS ['consumer ' ];
52
54
$ this ->autoSetup = $ configuration ['auto_setup ' ] ?? self ::DEFAULT_OPTIONS ['auto_setup ' ];
55
+ $ this ->maxEntries = $ configuration ['stream_max_entries ' ] ?? self ::DEFAULT_OPTIONS ['stream_max_entries ' ];
53
56
}
54
57
55
58
public static function fromDsn (string $ dsn , array $ redisOptions = [], \Redis $ redis = null ): self
@@ -79,7 +82,19 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
79
82
unset($ redisOptions ['auto_setup ' ]);
80
83
}
81
84
82
- return new self (['stream ' => $ stream , 'group ' => $ group , 'consumer ' => $ consumer , 'auto_setup ' => $ autoSetup ], $ connectionCredentials , $ redisOptions , $ redis );
85
+ $ maxEntries = null ;
86
+ if (\array_key_exists ('stream_max_entries ' , $ redisOptions )) {
87
+ $ maxEntries = filter_var ($ redisOptions ['stream_max_entries ' ], FILTER_VALIDATE_INT );
88
+ unset($ redisOptions ['stream_max_entries ' ]);
89
+ }
90
+
91
+ return new self ([
92
+ 'stream ' => $ stream ,
93
+ 'group ' => $ group ,
94
+ 'consumer ' => $ consumer ,
95
+ 'auto_setup ' => $ autoSetup ,
96
+ 'stream_max_entries ' => $ maxEntries ,
97
+ ], $ connectionCredentials , $ redisOptions , $ redis );
83
98
}
84
99
85
100
public function get (): ?array
@@ -166,9 +181,15 @@ public function add(string $body, array $headers): void
166
181
167
182
$ e = null ;
168
183
try {
169
- $ added = $ this ->connection ->xadd ($ this ->stream , '* ' , ['message ' => json_encode (
170
- ['body ' => $ body , 'headers ' => $ headers ]
171
- )]);
184
+ if ($ this ->maxEntries ) {
185
+ $ added = $ this ->connection ->xadd ($ this ->stream , '* ' , ['message ' => json_encode (
186
+ ['body ' => $ body , 'headers ' => $ headers ]
187
+ )], $ this ->maxEntries , true );
188
+ } else {
189
+ $ added = $ this ->connection ->xadd ($ this ->stream , '* ' , ['message ' => json_encode (
190
+ ['body ' => $ body , 'headers ' => $ headers ]
191
+ )]);
192
+ }
172
193
} catch (\RedisException $ e ) {
173
194
}
174
195
0 commit comments