diff --git a/src/ApiV2/Mapping.php b/src/ApiV2/Mapping.php index eb40601..d21cc14 100644 --- a/src/ApiV2/Mapping.php +++ b/src/ApiV2/Mapping.php @@ -7,6 +7,8 @@ */ namespace deepseath\hris\ApiV2; +use Symfony\Contracts\Cache\ItemInterface; + class Mapping { /** @@ -38,29 +40,34 @@ class Mapping } } - $cache = $this->service->cache(); - $cachePool = $cache->getItem('mappings'); - if (!$cachePool->isHit() - || $force !== false - || !($mappings = $cachePool->get()) - || !isset($mappings['expire_at']) - || time() - $mappings['expire_at'] > $this->service->config['mapping_expire']) { - - $result = $this->service->apiRequest('get', 'mappingVO', []); - $mappings = []; - foreach ($result['mappings'] ?? [] as $mapping) { - $mappings[$mapping['mappingType']][$mapping['mappingId']] = $mapping['mappingValue']; - } - $mappings = [ - 'expire_at' => time(), - 'data' => $mappings - ]; - $cachePool->set($mappings); - $cachePool->expiresAfter($this->service->config['mapping_expire']); - $cache->save($cachePool); + if ($force !== false) { + $mappings = $this->_getWithoutCache(); + } else { + $cache = $this->service->cache(); + $mappings = $cache->get('hris_mappings', function (ItemInterface $item) { + $mappings = $this->_getWithoutCache(); + $item->expiresAfter($this->service->config['mapping_expire']); + return $mappings; + }); + unset($cache); + } + + return $field === null ? $mappings : $mappings[$type]; + } + + /** + * 无缓存获取 mappings + * @return array + */ + private function _getWithoutCache() : array + { + $result = $this->service->apiRequest('get', 'mappingVO', []); + $mappings = []; + foreach ($result['mappings'] ?? [] as $mapping) { + $mappings[$mapping['mappingType']][$mapping['mappingId']] = $mapping['mappingValue']; } -//print_r(array_keys($mappings['data'])); - return $field === null ? $mappings['data'] : $mappings['data'][$type]; + unset($result, $mapping); + return $mappings; } /** diff --git a/src/Hris.php b/src/Hris.php index bf93366..427230f 100644 --- a/src/Hris.php +++ b/src/Hris.php @@ -8,6 +8,7 @@ namespace deepseath\hris; use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Contracts\Cache\ItemInterface; class Hris { @@ -125,11 +126,22 @@ class Hris */ public function getTokenInfo(bool $force = false) : array { - $token = $this->_cache->getItem('token'); - if ($token->isHit() && $force === false) { - return $token->get(); + if ($force !== false) { + return $this->_getTokenInfoWithoutCache(); + } else { + return $this->_cache->get('hris_token', function (ItemInterface $item) { + $tokenInfo = $this->_getTokenInfoWithoutCache(); + $item->expiresAfter($tokenInfo['expires_in']); + return $tokenInfo; + }); } + } + /** + * 获取 token 信息,无缓存 + */ + private function _getTokenInfoWithoutCache() + { // 初始化 token 信息 $newTokenInfo = []; // 构造 token 获取接口地址 @@ -155,19 +167,9 @@ class Hris if ($newTokenInfo['code'] != 200) { throw new \Exception('HRIS TOKEN GET ERROR:' . $newTokenInfo['msg'] . ':' . $newTokenInfo['code']); } - - $newTokenInfo['_datetime'] = date('Y-m-d H:i:s'); - // 将本次获取的 token 值写入到缓存 - $token->set($newTokenInfo); - if (isset($newTokenInfo['expires_in'])) { - $expire = $newTokenInfo['expires_in']; - } else { - $expire = $this->_tokenExpire; + if (!isset($newTokenInfo['expires_in'])) { + $newTokenInfo['expires_in'] = $this->_tokenExpire; } - $token->expiresAfter($expire - 10); - $this->log($token->get(), 'Save Token'); - $this->_cache->save($token); - return $newTokenInfo; }