Experimental method to clear APC cache from PHP-CLI #2554 #1943

This commit is contained in:
Kijin Sung 2025-08-24 22:16:39 +09:00
parent e0033ac2fc
commit 771dbfe114
3 changed files with 98 additions and 6 deletions

View file

@ -239,6 +239,10 @@ class Cache
{
if (self::$_driver !== null)
{
if (self::$_driver instanceof Drivers\Cache\APC && \PHP_SAPI === 'cli')
{
self::sendClearRequest([$key]);
}
return self::$_driver->delete(self::getRealKey($key)) ? true : false;
}
else
@ -323,6 +327,10 @@ class Cache
{
if (self::$_driver !== null)
{
if (self::$_driver instanceof Drivers\Cache\APC && \PHP_SAPI === 'cli')
{
self::sendClearRequest([$group_name . ':*']);
}
$success = self::$_driver->incr(self::$_prefix . $group_name . '#version', 1) ? true : false;
unset(self::$_group_versions[$group_name]);
return $success;
@ -344,6 +352,10 @@ class Cache
{
if (self::$_driver !== null)
{
if (self::$_driver instanceof Drivers\Cache\APC && \PHP_SAPI === 'cli')
{
self::sendClearRequest(['*']);
}
return self::$_driver->clear() ? true : false;
}
else
@ -396,4 +408,27 @@ class Cache
return self::$_prefix . $key;
}
/**
* Send a web request to clear the cache.
*
* @param array $keys
* @return void
*/
public static function sendClearRequest(array $keys): void
{
if (!$keys)
{
return;
}
$keystring = implode('|', $keys);
$signature = Security::createSignature($keystring);
HTTP::post(\Context::getRequestUri(), [
'module' => 'module',
'act' => 'procModuleClearCache',
'keys' => $keys,
'signature' => $signature,
]);
}
}

View file

@ -7,16 +7,17 @@
<action name="dispModuleFileBox" type="view" permission="root" meta-noindex="true" />
<action name="dispModuleFileBoxAdd" type="view" permission="root" meta-noindex="true" />
<action name="dispModuleChangeLang" type="view" />
<action name="getModuleSkinInfoList" type="model" permission="root" />
<action name="getFileBoxListHtml" type="model" permission="root" />
<action name="getModuleInfoByMenuItemSrl" type="model" permission="root" />
<action name="getLangListByLangcodeForAutoComplete" type="model" permission="manager" />
<action name="getLangByLangcode" type="model" />
<action name="procModuleFileBoxAdd" type="controller" permission="root" />
<action name="procModuleFileBoxDelete" type="controller" permission="root" />
<action name="procModuleClearCache" type="controller" check_csrf="false" />
<action name="dispModuleAdminContent" type="view" menu_name="installedModule" menu_index="true" admin_index="true" />
<action name="dispModuleAdminCategory" type="view" menu_name="installedModule" />
<action name="dispModuleAdminInfo" type="view" />
@ -26,7 +27,7 @@
<action name="dispModuleAdminCopyModule" type="view" />
<action name="dispModuleAdminFileBox" type="view" menu_name="filebox" menu_index="true" />
<action name="dispModuleAdminLangcode" type="view" menu_name="multilingual" menu_index="true" />
<action name="getModuleAdminModuleList" type="model" />
<action name="getModuleAdminModuleInfo" type="model" />
<action name="getModuleAdminGrant" type="model" />
@ -36,12 +37,12 @@
<action name="getModuleAdminModuleSearcherHtml" type="model" />
<action name="getModuleAdminMultilingualHtml" type="model" permission="manager" />
<action name="getModuleAdminLangListHtml" type="model" permission="manager" />
<action name="procModuleAdminInsertCategory" type="controller" ruleset="insertCategory" />
<action name="procModuleAdminUpdateCategory" type="controller" ruleset="updateCategory" />
<action name="procModuleAdminDeleteCategory" type="controller" ruleset="deleteCategory" />
<action name="procModuleAdminModuleSetup" type="controller" ruleset="insertModuleSetup" />
<action name="procModuleAdminModuleGrantSetup" type="controller" ruleset="insertModulesGrant" />
<action name="procModuleAdminModuleGrantSetup" type="controller" ruleset="insertModulesGrant" />
<action name="procModuleAdminCopyModule" type="controller" ruleset="copyModule" />
<action name="procModuleAdminInsertGrant" type="controller" permission="manager" check_var="module_srl" />
<action name="procModuleAdminUpdateSkinInfo" type="controller" permission="manager" check_var="module_srl" />

View file

@ -1266,6 +1266,62 @@ class ModuleController extends Module
return executeQuery('module.deleteModuleFileBox', $args);
}
/**
* API call to clear cache entries.
*
* This can be used to clear the APC cache from CLI scripts,
* such as async tasks run from crontab.
*/
public function procModuleClearCache()
{
// This is a JSON API.
Context::setResponseMethod('JSON');
if (PHP_SAPI === 'cli')
{
throw new Rhymix\Framework\Exceptions\InvalidRequest;
}
// Get cache keys to clear.
$keys = Context::get('keys');
if (!$keys)
{
throw new Rhymix\Framework\Exceptions\InvalidRequest;
}
if (!is_array($keys))
{
$keys = [$keys];
}
// Verify the API signature.
$keystring = implode('|', $keys);
$signature = Context::get('signature');
if (!$signature)
{
throw new Rhymix\Framework\Exceptions\NotPermitted;
}
if (!Rhymix\Framework\Security::verifySignature($keystring, $signature))
{
throw new Rhymix\Framework\Exceptions\NotPermitted;
}
// Clear the requested cache keys.
foreach ($keys as $key)
{
if ($key === '*')
{
Rhymix\Framework\Cache::clearAll();
}
elseif (preg_match('/^([^:]+):\*$/', $key, $matches))
{
Rhymix\Framework\Cache::clearGroup($matches[1]);
}
else
{
Rhymix\Framework\Cache::delete($key);
}
}
}
/**
* @brief function of locking (timeout is in seconds)
*/