2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
class EventTracingCache implements CacheInterface
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
private CacheInterface $engine;
|
|
|
|
private \EventTracer $tracer;
|
|
|
|
private int $hits=0;
|
|
|
|
private int $misses=0;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function __construct(CacheInterface $engine, \EventTracer $tracer)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->engine = $engine;
|
|
|
|
$this->tracer = $tracer;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function get($key, $default=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-02 16:50:09 +00:00
|
|
|
if ($key === "__etc_cache_hits") {
|
|
|
|
return $this->hits;
|
|
|
|
}
|
|
|
|
if ($key === "__etc_cache_misses") {
|
|
|
|
return $this->misses;
|
|
|
|
}
|
2019-05-26 15:33:26 +00:00
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Query", ["key"=>$key]);
|
|
|
|
$val = $this->engine->get($key, $default);
|
|
|
|
if ($val != $default) {
|
2019-09-29 13:30:55 +00:00
|
|
|
$res = "hit";
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->hits++;
|
|
|
|
} else {
|
2019-09-29 13:30:55 +00:00
|
|
|
$res = "miss";
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->misses++;
|
|
|
|
}
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->end(null, ["result"=>$res]);
|
2019-09-29 13:30:55 +00:00
|
|
|
return $val;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function set($key, $value, $ttl = null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Set", ["key"=>$key, "ttl"=>$ttl]);
|
|
|
|
$val = $this->engine->set($key, $value, $ttl);
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function delete($key)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Delete", ["key"=>$key]);
|
|
|
|
$val = $this->engine->delete($key);
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function clear()
|
2023-02-02 16:04:35 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Clear");
|
|
|
|
$val = $this->engine->clear();
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:46:25 +00:00
|
|
|
public function getMultiple($keys, $default = null)
|
2023-02-02 16:04:35 +00:00
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Get Multiple");
|
|
|
|
$val = $this->engine->getMultiple($values, $default);
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:50:09 +00:00
|
|
|
public function setMultiple($values, $ttl = null)
|
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Set Multiple");
|
|
|
|
$val = $this->engine->setMultiple($values, $ttl);
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:50:09 +00:00
|
|
|
public function deleteMultiple($keys)
|
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Delete Multiple");
|
|
|
|
$val = $this->engine->deleteMultiple($keys);
|
|
|
|
$this->tracer->end();
|
|
|
|
return $val;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 16:50:09 +00:00
|
|
|
public function has($key)
|
|
|
|
{
|
2023-02-02 16:46:25 +00:00
|
|
|
$this->tracer->begin("Cache Has", ["key"=>$key]);
|
|
|
|
$val = $this->engine->has($key);
|
|
|
|
$this->tracer->end(null, ["exists"=>$val]);
|
|
|
|
return $val;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
2023-02-02 16:04:35 +00:00
|
|
|
|
2023-02-02 16:50:09 +00:00
|
|
|
function loadCache(?string $dsn): CacheInterface
|
|
|
|
{
|
2023-02-02 16:04:35 +00:00
|
|
|
$matches = [];
|
|
|
|
$c = null;
|
|
|
|
if ($dsn && preg_match("#(.*)://(.*)#", $dsn, $matches) && !isset($_GET['DISABLE_CACHE'])) {
|
|
|
|
if ($matches[1] == "memcached" || $matches[1] == "memcache") {
|
2023-02-02 16:46:25 +00:00
|
|
|
$hp = explode(":", $matches[2]);
|
|
|
|
$memcache = new \Memcached();
|
|
|
|
$memcache->addServer($hp[0], (int)$hp[1]);
|
|
|
|
$c = new \Sabre\Cache\Memcached($memcached);
|
2023-02-02 16:04:35 +00:00
|
|
|
} elseif ($matches[1] == "apc") {
|
2023-02-02 16:46:25 +00:00
|
|
|
$c = new \Sabre\Cache\Apcu();
|
2023-02-02 16:04:35 +00:00
|
|
|
} elseif ($matches[1] == "redis") {
|
2023-02-02 16:46:25 +00:00
|
|
|
$hp = explode(":", $matches[2]);
|
|
|
|
$redis = new \Predis\Client([
|
|
|
|
'scheme' => 'tcp',
|
|
|
|
'host' => $hp[0],
|
|
|
|
'port' => (int)$hp[1]
|
|
|
|
], ['prefix' => 'shm:']);
|
|
|
|
$c = new \Naroga\RedisCache\Redis($redis);
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-02 16:46:25 +00:00
|
|
|
$c = new \Sabre\Cache\Memory();
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|
2023-02-02 16:46:25 +00:00
|
|
|
global $_tracer;
|
|
|
|
return new EventTracingCache($c, $_tracer);
|
2023-02-02 16:04:35 +00:00
|
|
|
}
|