rosavox/lib/alveolata/cache/functions.php
2025-05-23 07:33:29 +00:00

103 lines
1.8 KiB
PHP

<?php
namespace alveolata\cache;
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/cache/abstract/interface.php');
require_once(DIR_ALVEOLATA . '/cache/implementation-none/wrapper-class.php');
require_once(DIR_ALVEOLATA . '/cache/implementation-memory/wrapper-class.php');
require_once(DIR_ALVEOLATA . '/cache/implementation-apc/wrapper-class.php');
/**
* @param string $id
* @param Closure $retrieve
* @return record<fetched:boolean,value:mixed>
* @author Christian Fraß <frass@greenscale.de>
*/
function get_with_info(
interface_cache $cache,
string $id,
\Closure $retrieve
)
{
if (! $cache->has($id)) {
$value = $retrieve();
$cache->set($id, $value);
return ['fetched' => false, 'value' => $value];
}
else {
$value = $cache->fetch($id);
return ['fetched' => true, 'value' => $value];
}
}
/**
* @param string $id
* @param Closure $retrieve
* @return mixed
* @author Christian Fraß <frass@greenscale.de>
*/
function get(
interface_cache $cache,
string $id,
\Closure $retrieve
)
{
return get_with_info($cache, $id, $retrieve)['value'];
}
/**
* @param string $id
* @param Closure $retrieve
* @return mixed
* @author Christian Fraß <frass@greenscale.de>
*/
function clear(
interface_cache $cache
)
{
return $cache->clear();
}
/**
* @author Christian Fraß <frass@greenscale.de>
*/
function make(
string $kind,
array $parameters = []
) : interface_cache
{
switch ($kind) {
default: {
throw (new \Exception(sprintf('invalid cache kind "%s"', $kind)));
break;
}
case 'none': {
return (
implementation_none::make(
)
);
break;
}
case 'memory': {
return (
implementation_memory::make(
)
);
break;
}
case 'apc': {
return (
implementation_apc::make(
$parameters['section'] ?? UNSET_STRING
)
);
break;
}
}
}