@@ -114,6 +114,23 @@ private function mainDashboard(array $info): string {
114114 ]);
115115 }
116116
117+ /**
118+ * Get key and convert any value to a string.
119+ *
120+ * @param string $key
121+ *
122+ * @return string
123+ */
124+ private function getKey (string $ key ): string {
125+ $ data = apcu_fetch ($ key );
126+
127+ if (is_array ($ data ) || is_object ($ data )) {
128+ $ data = serialize ($ data );
129+ }
130+
131+ return (string ) $ data ;
132+ }
133+
117134 /**
118135 * View key value.
119136 *
@@ -129,7 +146,7 @@ private function viewKey(): string {
129146 if (isset ($ _GET ['export ' ])) {
130147 header ('Content-disposition: attachment; filename= ' .$ key .'.txt ' );
131148 header ('Content-Type: text/plain ' );
132- echo apcu_fetch ($ key );
149+ echo $ this -> getKey ($ key );
133150 exit ;
134151 }
135152
@@ -138,7 +155,7 @@ private function viewKey(): string {
138155 Http::redirect (['db ' ]);
139156 }
140157
141- $ value = apcu_fetch ($ key );
158+ $ value = $ this -> getKey ($ key );
142159
143160 [$ value , $ encode_fn , $ is_formatted ] = Value::format ($ value );
144161
@@ -149,7 +166,7 @@ private function viewKey(): string {
149166 return $ this ->template ->render ('partials/view_key ' , [
150167 'key ' => $ key ,
151168 'value ' => $ value ,
152- 'type ' => 'string ' ,
169+ 'type ' => 'string ' , // Checking the original data type with gettype() can affect performance.
153170 'ttl ' => Format::seconds ($ ttl ),
154171 'size ' => Format::bytes (strlen ($ value )),
155172 'encode_fn ' => $ encode_fn ,
@@ -192,7 +209,7 @@ private function form(): string {
192209 $ value = Http::post ('value ' );
193210
194211 if (isset ($ _GET ['key ' ]) && apcu_exists ($ key )) {
195- $ value = apcu_fetch ($ key );
212+ $ value = $ this -> getKey ($ key );
196213 $ info = apcu_key_info ($ key );
197214
198215 $ expire = $ info ['ttl ' ];
0 commit comments