friendica/src/Core/Cache/Type/ProfilerCacheDecorator.php

184 lines
3.9 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Util\Profiler;
/**
2021-10-26 21:44:29 +02:00
* This class wraps cache driver, so they can get profiled - in case the profiler is enabled
*
2021-10-26 21:44:29 +02:00
* It is using the decorator pattern (@see https://en.wikipedia.org/wiki/Decorator_pattern )
*/
2021-10-26 21:44:29 +02:00
class ProfilerCacheDecorator implements ICanCache, ICanCacheInMemory
{
/**
2021-10-26 21:44:29 +02:00
* @var ICanCache The original cache driver
*/
private $cache;
/**
* @var Profiler The profiler of Friendica
*/
private $profiler;
2021-10-26 21:44:29 +02:00
public function __construct(ICanCache $cache, Profiler $profiler)
{
$this->cache = $cache;
$this->profiler = $profiler;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function getAllKeys(?string $prefix = null): array
{
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->getAllKeys($prefix);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function get(string $key)
{
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->get($key);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->set($key, $value, $ttl);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function delete(string $key): bool
{
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->delete($key);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function clear(bool $outdated = true): bool
{
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->clear($outdated);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
2021-10-26 21:44:29 +02:00
if ($this->cache instanceof ICanCacheInMemory) {
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->add($key, $value, $ttl);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
} else {
return false;
}
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
{
2021-10-26 21:44:29 +02:00
if ($this->cache instanceof ICanCacheInMemory) {
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
} else {
return false;
}
}
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function compareDelete(string $key, $value): bool
{
2021-10-26 21:44:29 +02:00
if ($this->cache instanceof ICanCacheInMemory) {
2021-07-27 06:57:29 +02:00
$this->profiler->startRecording('cache');
$return = $this->cache->compareDelete($key, $value);
2021-07-27 06:57:29 +02:00
$this->profiler->stopRecording();
return $return;
} else {
return false;
}
}
2019-08-04 16:13:53 +02:00
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function GetName(): string
{
2019-08-04 16:13:53 +02:00
return $this->cache->getName() . ' (with profiler)';
}
}