rosavox/lib/alveolata/cache/implementation-apc/functions.php

164 lines
2.6 KiB
PHP
Raw Normal View History

2025-05-23 07:33:29 +00:00
<?php
namespace alveolata\cache;
// require_once(DIR_ALVEOLATA . '/definitions.php');
/**
*/
class _state_apc {
/**
* @var array {list<string>}
*/
public static $sections = [];
}
/**
* @author Christian Fraß <frass@greenscale.de>
*/
class struct_subject_apc {
/**
* @var string
* @author Christian Fraß <frass@greenscale.de>
*/
public $section;
/**
* @var list<string>
* @author Christian Fraß <frass@greenscale.de>
*/
public $ids;
/**
* @param string $section
* @author Christian Fraß <frass@greenscale.de>
*/
public function __construct(
string $section
)
{
$this->section = $section;
$this->ids = [];
}
}
/**
* @param struct_subject_apc $subject
* @param string id
* @return string
* @author Christian Fraß <frass@greenscale.de>
*/
function _apc_id(
struct_subject_apc $subject,
string $id
) : string
{
return (
($subject->section === UNSET_STRING)
? $id
: sprintf('%s_%s', $subject->section, $id)
);
}
/**
* @param string $section
* @return struct_subject_apc
* @author Christian Fraß <frass@greenscale.de>
*/
function apc_make(
string $section = UNSET_STRING
) : struct_subject_apc
{
if ($section === UNSET_STRING) {
$section = sprintf('alveolata_%d', count(_state_apc::$sections));
}
if (in_array($section, _state_apc::$sections)) {
throw (new \Exception(sprintf('APC section "%s" already in use', $section)));
}
else {
array_push(_state_apc::$sections, $section);
$subject = (new struct_subject_apc($section));
return $subject;
}
}
/**
* @param struct_subject_apc $subject
* @param string $id
* @param mixed $value
* @author Christian Fraß <frass@greenscale.de>
*/
function apc_set(
struct_subject_apc $subject,
string $id,
$value
) : void
{
$id_ = _apc_id($subject, $id);
array_push($subject->ids, $id_);
\apc_store($id_, $value);
}
/**
* @param struct_subject_apc $subject
* @param string $id
* @return bool
* @author Christian Fraß <frass@greenscale.de>
*/
function apc_has(
struct_subject_apc $subject,
string $id
) : bool
{
return \apc_exists(_apc_id($subject, $id));
}
/**
* @param struct_subject_apc $subject
* @param string $id
* @return mixed
* @author Christian Fraß <frass@greenscale.de>
*/
function apc_fetch(
struct_subject_apc $subject,
string $id
)
{
return \apc_fetch(_apc_id($subject, $id));
}
/**
* @param struct_subject_apc $subject
* @author Christian Fraß <frass@greenscale.de>
*/
function apc_clear(
struct_subject_apc $subject
) : void
{
if ($subject->section === UNSET_STRING) {
\apc_clear_cache();
}
else {
foreach ($subject->ids as $id_) {
\apc_delete($id_);
}
$subject->ids = [];
}
}
?>