@@ -18,6 +18,11 @@ class APCng implements Adapter
1818
1919 private const MAX_LOOPS = 10 ;
2020
21+ /**
22+ * @var int ttl for all apcu entries reserved for prometheus
23+ */
24+ private $ ttl ;
25+
2126 /**
2227 * @var int
2328 */
@@ -44,10 +49,11 @@ class APCng implements Adapter
4449 * APCng constructor.
4550 *
4651 * @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
4753 *
4854 * @throws StorageException
4955 */
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 )
5157 {
5258 if (!extension_loaded ('apcu ' )) {
5359 throw new StorageException ('APCu extension is not loaded ' );
@@ -61,6 +67,8 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX,
6167 $ this ->metaInfoCounterKey = implode (': ' , [ $ this ->prometheusPrefix , 'metainfocounter ' ]);
6268 $ this ->metaInfoCountedMetricKeyPattern = implode (': ' , [ $ this ->prometheusPrefix , 'metainfocountedmetric_#COUNTER# ' ]);
6369
70+ $ this ->ttl = $ ttl ;
71+
6472 if ($ decimalPrecision < 0 || $ decimalPrecision > 6 ) {
6573 throw new UnexpectedValueException (
6674 sprintf ('Decimal precision %d is not from interval <0;6>. ' , $ decimalPrecision )
@@ -96,7 +104,7 @@ public function updateHistogram(array $data): void
96104
97105 if ($ old === false ) {
98106 // If sum does not exist, initialize it, store the metadata for the new histogram
99- apcu_add ($ sumKey , 0 , 0 );
107+ apcu_add ($ sumKey , 0 , $ this -> ttl );
100108 $ this ->storeMetadata ($ data );
101109 $ this ->storeLabelKeys ($ data );
102110 }
@@ -113,7 +121,7 @@ public function updateHistogram(array $data): void
113121 }
114122
115123 // Initialize and increment the bucket
116- apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 );
124+ apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 , $ this -> ttl );
117125 apcu_inc ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ));
118126 }
119127
@@ -134,7 +142,7 @@ public function updateSummary(array $data): void
134142 {
135143 // store value key; store metadata & labels if new
136144 $ valueKey = $ this ->valueKey ($ data );
137- $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), 0 );
145+ $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), $ this -> ttl );
138146 if ($ new ) {
139147 $ this ->storeMetadata ($ data , false );
140148 $ this ->storeLabelKeys ($ data );
@@ -167,7 +175,7 @@ public function updateGauge(array $data): void
167175 {
168176 $ valueKey = $ this ->valueKey ($ data );
169177 if ($ data ['command ' ] === Adapter::COMMAND_SET ) {
170- apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), 0 );
178+ apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), $ this -> ttl );
171179 $ this ->storeMetadata ($ data );
172180 $ this ->storeLabelKeys ($ data );
173181
@@ -177,7 +185,7 @@ public function updateGauge(array $data): void
177185 $ old = apcu_fetch ($ valueKey );
178186
179187 if ($ old === false ) {
180- apcu_add ($ valueKey , 0 , 0 );
188+ apcu_add ($ valueKey , 0 , $ this -> ttl );
181189 $ this ->storeMetadata ($ data );
182190 $ this ->storeLabelKeys ($ data );
183191 }
@@ -199,7 +207,7 @@ public function updateCounter(array $data): void
199207 $ old = apcu_fetch ($ valueKey );
200208
201209 if ($ old === false ) {
202- apcu_add ($ valueKey , 0 , 0 );
210+ apcu_add ($ valueKey , 0 , $ this -> ttl );
203211 $ this ->storeMetadata ($ data );
204212 $ this ->storeLabelKeys ($ data );
205213 }
@@ -253,7 +261,7 @@ private function addItemToKey(string $key, string $item): void
253261 $ _item = $ this ->encodeLabelKey ($ item );
254262 if (!array_key_exists ($ _item , $ arr )) {
255263 $ arr [$ _item ] = 1 ;
256- apcu_store ($ key , $ arr , 0 );
264+ apcu_store ($ key , $ arr , $ this -> ttl );
257265 }
258266 }
259267
@@ -335,7 +343,7 @@ private function scanAndBuildMetainfoCache(): array
335343 $ arr [$ type ][] = ['key ' => $ metaKey , 'value ' => $ metaInfo ];
336344 }
337345
338- apcu_store ($ this ->metainfoCacheKey , $ arr , 0 );
346+ apcu_store ($ this ->metainfoCacheKey , $ arr , $ this -> ttl );
339347
340348 return $ arr ;
341349 }
@@ -892,17 +900,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
892900 $ toStore = json_encode ($ metaData );
893901 }
894902
895- $ stored = apcu_add ($ metaKey , $ toStore , 0 );
903+ $ stored = apcu_add ($ metaKey , $ toStore , $ this -> ttl );
896904
897905 if (!$ stored ) {
898906 return ;
899907 }
900908
901- apcu_add ($ this ->metaInfoCounterKey , 0 , 0 );
909+ apcu_add ($ this ->metaInfoCounterKey , 0 , $ this -> ttl );
902910 $ counter = apcu_inc ($ this ->metaInfoCounterKey );
903911
904912 $ newCountedMetricKey = $ this ->metaCounterKey ($ counter );
905- apcu_store ($ newCountedMetricKey , $ metaKey , 0 );
913+ apcu_store ($ newCountedMetricKey , $ metaKey , $ this -> ttl );
906914 }
907915
908916 private function metaCounterKey (int $ counter ): string
0 commit comments