81 lines
1.8 KiB
PHP
81 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Cms\cmsTextManager\inc;
|
|
|
|
use BRSystems\Core\Library\cApc\cApc;
|
|
|
|
/**
|
|
* Text manager APC cache helper.
|
|
*/
|
|
class cmsTextManagerCache
|
|
{
|
|
const DEFAULT_PREFIX = 'TM_';
|
|
const DEFAULT_TTL = 86400;
|
|
|
|
/** @var cApc */
|
|
private $apc;
|
|
|
|
/** @var string */
|
|
private $prefix;
|
|
|
|
/** @var int */
|
|
private $ttl;
|
|
|
|
public function __construct(cApc $apc, $prefix = self::DEFAULT_PREFIX, $ttl = self::DEFAULT_TTL)
|
|
{
|
|
$this->apc = $apc;
|
|
$this->prefix = (string)$prefix;
|
|
$this->ttl = (int)$ttl;
|
|
}
|
|
|
|
public function key($type, $langId)
|
|
{
|
|
return $this->prefix . $this->normalizeType($type) . '_LANG_' . (int)$langId;
|
|
}
|
|
|
|
public function get($type, $langId)
|
|
{
|
|
if (!$this->apc->enabled) {
|
|
return false;
|
|
}
|
|
|
|
$cached = $this->apc->check($this->key($type, $langId));
|
|
if (!is_array($cached) || !array_key_exists('payload', $cached)) {
|
|
return false;
|
|
}
|
|
|
|
return $cached['payload'];
|
|
}
|
|
|
|
public function set($type, $langId, $data, $ttl = null)
|
|
{
|
|
if (!$this->apc->enabled) {
|
|
return false;
|
|
}
|
|
|
|
$this->apc->store($this->key($type, $langId), ['payload' => $data], ($ttl !== null ? (int)$ttl : $this->ttl));
|
|
return true;
|
|
}
|
|
|
|
public function delete($type, $langId)
|
|
{
|
|
if (!$this->apc->enabled) {
|
|
return false;
|
|
}
|
|
|
|
$this->apc->setExpired($this->key($type, $langId));
|
|
return true;
|
|
}
|
|
|
|
private function normalizeType($type)
|
|
{
|
|
if (is_array($type)) {
|
|
$type = array_map('strval', $type);
|
|
sort($type, SORT_STRING);
|
|
return implode('_', $type);
|
|
}
|
|
|
|
return (string)$type;
|
|
}
|
|
}
|