@@ -18,6 +18,11 @@ class APCng implements Adapter
18
18
19
19
private const MAX_LOOPS = 10 ;
20
20
21
+ /**
22
+ * @var int ttl for all apcu entries reserved for prometheus
23
+ */
24
+ private $ ttl ;
25
+
21
26
/**
22
27
* @var int
23
28
*/
@@ -44,10 +49,11 @@ class APCng implements Adapter
44
49
* APCng constructor.
45
50
*
46
51
* @param string $prometheusPrefix Prefix for APCu keys (defaults to {@see PROMETHEUS_PREFIX}).
52
+ * @param int $ttl ttl for all apcu entries reserved for prometheus, default is 120 days
47
53
*
48
54
* @throws StorageException
49
55
*/
50
- public function __construct (string $ prometheusPrefix = self ::PROMETHEUS_PREFIX , int $ decimalPrecision = 3 )
56
+ public function __construct (string $ prometheusPrefix = self ::PROMETHEUS_PREFIX , int $ ttl = 10368000 , int $ decimalPrecision = 3 )
51
57
{
52
58
if (!extension_loaded ('apcu ' )) {
53
59
throw new StorageException ('APCu extension is not loaded ' );
@@ -96,7 +102,7 @@ public function updateHistogram(array $data): void
96
102
97
103
if ($ old === false ) {
98
104
// If sum does not exist, initialize it, store the metadata for the new histogram
99
- apcu_add ($ sumKey , 0 , 0 );
105
+ apcu_add ($ sumKey , 0 , $ this -> ttl );
100
106
$ this ->storeMetadata ($ data );
101
107
$ this ->storeLabelKeys ($ data );
102
108
}
@@ -113,7 +119,7 @@ public function updateHistogram(array $data): void
113
119
}
114
120
115
121
// Initialize and increment the bucket
116
- apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 );
122
+ apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 , $ this -> ttl );
117
123
apcu_inc ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ));
118
124
}
119
125
@@ -134,7 +140,7 @@ public function updateSummary(array $data): void
134
140
{
135
141
// store value key; store metadata & labels if new
136
142
$ valueKey = $ this ->valueKey ($ data );
137
- $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), 0 );
143
+ $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), $ this -> ttl );
138
144
if ($ new ) {
139
145
$ this ->storeMetadata ($ data , false );
140
146
$ this ->storeLabelKeys ($ data );
@@ -167,7 +173,7 @@ public function updateGauge(array $data): void
167
173
{
168
174
$ valueKey = $ this ->valueKey ($ data );
169
175
if ($ data ['command ' ] === Adapter::COMMAND_SET ) {
170
- apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), 0 );
176
+ apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), $ this -> ttl );
171
177
$ this ->storeMetadata ($ data );
172
178
$ this ->storeLabelKeys ($ data );
173
179
@@ -177,7 +183,7 @@ public function updateGauge(array $data): void
177
183
$ old = apcu_fetch ($ valueKey );
178
184
179
185
if ($ old === false ) {
180
- apcu_add ($ valueKey , 0 , 0 );
186
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
181
187
$ this ->storeMetadata ($ data );
182
188
$ this ->storeLabelKeys ($ data );
183
189
}
@@ -199,7 +205,7 @@ public function updateCounter(array $data): void
199
205
$ old = apcu_fetch ($ valueKey );
200
206
201
207
if ($ old === false ) {
202
- apcu_add ($ valueKey , 0 , 0 );
208
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
203
209
$ this ->storeMetadata ($ data );
204
210
$ this ->storeLabelKeys ($ data );
205
211
}
@@ -253,7 +259,7 @@ private function addItemToKey(string $key, string $item): void
253
259
$ _item = $ this ->encodeLabelKey ($ item );
254
260
if (!array_key_exists ($ _item , $ arr )) {
255
261
$ arr [$ _item ] = 1 ;
256
- apcu_store ($ key , $ arr , 0 );
262
+ apcu_store ($ key , $ arr , $ this -> ttl );
257
263
}
258
264
}
259
265
@@ -335,7 +341,7 @@ private function scanAndBuildMetainfoCache(): array
335
341
$ arr [$ type ][] = ['key ' => $ metaKey , 'value ' => $ metaInfo ];
336
342
}
337
343
338
- apcu_store ($ this ->metainfoCacheKey , $ arr , 0 );
344
+ apcu_store ($ this ->metainfoCacheKey , $ arr , $ this -> ttl );
339
345
340
346
return $ arr ;
341
347
}
@@ -892,17 +898,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
892
898
$ toStore = json_encode ($ metaData );
893
899
}
894
900
895
- $ stored = apcu_add ($ metaKey , $ toStore , 0 );
901
+ $ stored = apcu_add ($ metaKey , $ toStore , $ this -> ttl );
896
902
897
903
if (!$ stored ) {
898
904
return ;
899
905
}
900
906
901
- apcu_add ($ this ->metaInfoCounterKey , 0 , 0 );
907
+ apcu_add ($ this ->metaInfoCounterKey , 0 , $ this -> ttl );
902
908
$ counter = apcu_inc ($ this ->metaInfoCounterKey );
903
909
904
910
$ newCountedMetricKey = $ this ->metaCounterKey ($ counter );
905
- apcu_store ($ newCountedMetricKey , $ metaKey , 0 );
911
+ apcu_store ($ newCountedMetricKey , $ metaKey , $ this -> ttl );
906
912
}
907
913
908
914
private function metaCounterKey (int $ counter ): string
0 commit comments