2019-08-03 14:48:56 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 09:45:36 -05: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class wraps cache driver so they can get profiled - in case the profiler is enabled
|
|
|
|
*
|
|
|
|
* It is using the decorator pattern (@see
|
|
|
|
*/
|
2019-08-04 04:26:53 -04:00
|
|
|
class ProfilerCache implements ICache, IMemoryCache
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
|
|
|
/**
|
2019-08-04 04:26:53 -04:00
|
|
|
* @var ICache The original cache driver
|
2019-08-03 14:48:56 -04:00
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Profiler The profiler of Friendica
|
|
|
|
*/
|
|
|
|
private $profiler;
|
|
|
|
|
2019-08-04 04:26:53 -04:00
|
|
|
public function __construct(ICache $cache, Profiler $profiler)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->profiler = $profiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getAllKeys($prefix = null)
|
|
|
|
{
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->getAllKeys($prefix);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->get($key);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->set($key, $value, $ttl);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->delete($key);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function clear($outdated = true)
|
|
|
|
{
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->clear($outdated);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
2019-08-04 04:26:53 -04:00
|
|
|
if ($this->cache instanceof IMemoryCache) {
|
2019-08-03 14:48:56 -04:00
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->add($key, $value, $ttl);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
2019-08-04 04:26:53 -04:00
|
|
|
if ($this->cache instanceof IMemoryCache) {
|
2019-08-03 14:48:56 -04:00
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function compareDelete($key, $value)
|
|
|
|
{
|
2019-08-04 04:26:53 -04:00
|
|
|
if ($this->cache instanceof IMemoryCache) {
|
2019-08-03 14:48:56 -04:00
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = $this->cache->compareDelete($key, $value);
|
|
|
|
|
2020-07-27 00:22:07 -04:00
|
|
|
$this->profiler->saveTimestamp($time, 'cache');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 09:42:39 -04:00
|
|
|
|
2019-08-04 10:13:53 -04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function GetName()
|
2019-08-04 09:42:39 -04:00
|
|
|
{
|
2019-08-04 10:13:53 -04:00
|
|
|
return $this->cache->getName() . ' (with profiler)';
|
2019-08-04 09:42:39 -04:00
|
|
|
}
|
2019-08-03 14:48:56 -04:00
|
|
|
}
|