Last active
December 11, 2020 07:15
-
-
Save azhai/94dc57a245976c9764e5d7d3f90f4aad to your computer and use it in GitHub Desktop.
获取全局配置,每分钟读取一次,并缓存到apcu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 获取全局配置,每分钟读取一次 | |
*/ | |
function getGlobalConfig($key) { | |
if (empty($key) || !extension_loaded('apcu') || !apcu_enabled()) { | |
return null; | |
} | |
$cache_key = 'globals'; | |
$success = false; | |
$result = apcu_fetch($cache_key, $success); | |
if ($success && $result) { | |
return isset($result[$key]) ? $result[$key] : null; | |
} | |
$fname = APPPATH . 'config/globals.json'; | |
if (!($content = @file_get_contents($fname))) { | |
return null; | |
} | |
if (!($result = @json_decode($content, true))) { | |
return null; | |
} | |
apcu_store($cache_key, $result, 60); | |
return isset($result[$key]) ? $result[$key] : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment