Skip to content

Instantly share code, notes, and snippets.

@azhai
Last active December 11, 2020 07:15
Show Gist options
  • Save azhai/94dc57a245976c9764e5d7d3f90f4aad to your computer and use it in GitHub Desktop.
Save azhai/94dc57a245976c9764e5d7d3f90f4aad to your computer and use it in GitHub Desktop.
获取全局配置,每分钟读取一次,并缓存到apcu
<?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