]> git.mxchange.org Git - friendica.git/commitdiff
Add Caching stats
authorPhilipp <admin@philipp.info>
Mon, 21 Apr 2025 18:12:54 +0000 (20:12 +0200)
committerPhilipp <admin@philipp.info>
Sun, 27 Apr 2025 19:26:43 +0000 (21:26 +0200)
src/Module/StatsCaching.php [new file with mode: 0644]
static/routes.config.php

diff --git a/src/Module/StatsCaching.php b/src/Module/StatsCaching.php
new file mode 100644 (file)
index 0000000..72ba61c
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+namespace Friendica\Module;
+
+use Friendica\App;
+use Friendica\BaseModule;
+use Friendica\Core\Cache\Capability\ICanCache;
+use Friendica\Core\Cache\Capability\ICanCacheInMemory;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\L10n;
+use Friendica\Core\Lock\Capability\ICanLock;
+use Friendica\Core\Lock\Type\CacheLock;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+use Friendica\Network\HTTPException;
+
+/**
+ * Returns statistics of Cache / Lock instances
+ *
+ * @todo Currently not possible to get distributed cache statistics in case the distributed cache (for sessions) is different to the normal cache (not possible to get the distributed cache instance yet)
+ */
+class StatsCaching extends BaseModule
+{
+       private IManageConfigValues $config;
+       private ICanCache $cache;
+       private ICanLock $lock;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, IManageConfigValues $config, ICanCache $cache, ICanLock $lock, array $parameters = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->config = $config;
+               $this->cache  = $cache;
+               $this->lock   = $lock;
+       }
+
+       private function isAllowed(array $request): bool
+       {
+               return empty(!$request['key']) && $request['key'] == $this->config->get('system', 'stats_key');
+       }
+
+       protected function content(array $request = []): string
+       {
+               if (!$this->isAllowed($request)) {
+                       throw new HTTPException\NotFoundException($this->l10n->t('Page not found.'));
+               }
+               return '';
+       }
+
+       protected function rawContent(array $request = [])
+       {
+               if (!$this->isAllowed($request)) {
+                       return;
+               }
+
+               $data = [];
+
+               // OPcache
+               if (function_exists('opcache_get_status')) {
+                       $status          = opcache_get_status(false);
+                       $data['opcache'] = [
+                               'enabled'            => $status['opcache_enabled'] ?? false,
+                               'hit_rate'           => $status['opcache_statistics']['opcache_hit_rate'] ?? null,
+                               'used_memory'        => $status['memory_usage']['used_memory'] ?? null,
+                               'free_memory'        => $status['memory_usage']['free_memory'] ?? null,
+                               'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'] ?? null,
+                       ];
+               } else {
+                       $data['opcache'] = [
+                               'enabled' => false,
+                       ];
+               }
+
+               if ($this->cache instanceof ICanCacheInMemory) {
+                       $data['cache'] = [
+                               'type' => $this->cache->getName(),
+                               'stats' => $this->cache->getStats(),
+                       ];
+               } else {
+                       $data['cache'] = [
+                               'type' => $this->cache->getName(),
+                       ];
+               }
+
+               if ($this->lock instanceof CacheLock) {
+                       $data['lock'] = [
+                               'type' => $this->lock->getName(),
+                               'stats' => $this->lock->getCacheStats(),
+                       ];
+               } else {
+                       $data['lock'] = [
+                               'type' => $this->lock->getName(),
+                       ];
+               }
+
+               $this->jsonExit($data);
+       }
+}
index 88e642c27a6538a892e19eb058ceb0a985a7c2b2..4c7b6949ecc33214488e4accbe8c61ba9297a84e 100644 (file)
@@ -642,7 +642,8 @@ return [
                ],
        ],
 
-       '/stats' => [Module\Stats::class, [R::GET]],
+       '/stats'         => [Module\Stats::class, [R::GET]],
+       '/stats/caching' => [Module\StatsCaching::class, [R::GET]],
 
        '/network' => [
                '[/{content}]'                => [Module\Conversation\Network::class, [R::GET]],