Commit 8b090b555ca722e3b01f3b0ad8abe5285016e0c1

Authored by Deepseath
1 parent a6bef0a5

变更缓存写入方式

src/ApiV2/Mapping.php
@@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
7 */ 7 */
8 namespace deepseath\hris\ApiV2; 8 namespace deepseath\hris\ApiV2;
9 9
  10 +use Symfony\Contracts\Cache\ItemInterface;
  11 +
10 class Mapping 12 class Mapping
11 { 13 {
12 /** 14 /**
@@ -38,29 +40,34 @@ class Mapping @@ -38,29 +40,34 @@ class Mapping
38 } 40 }
39 } 41 }
40 42
41 - $cache = $this->service->cache();  
42 - $cachePool = $cache->getItem('mappings');  
43 - if (!$cachePool->isHit()  
44 - || $force !== false  
45 - || !($mappings = $cachePool->get())  
46 - || !isset($mappings['expire_at'])  
47 - || time() - $mappings['expire_at'] > $this->service->config['mapping_expire']) {  
48 -  
49 - $result = $this->service->apiRequest('get', 'mappingVO', []);  
50 - $mappings = [];  
51 - foreach ($result['mappings'] ?? [] as $mapping) {  
52 - $mappings[$mapping['mappingType']][$mapping['mappingId']] = $mapping['mappingValue'];  
53 - }  
54 - $mappings = [  
55 - 'expire_at' => time(),  
56 - 'data' => $mappings  
57 - ];  
58 - $cachePool->set($mappings);  
59 - $cachePool->expiresAfter($this->service->config['mapping_expire']);  
60 - $cache->save($cachePool); 43 + if ($force !== false) {
  44 + $mappings = $this->_getWithoutCache();
  45 + } else {
  46 + $cache = $this->service->cache();
  47 + $mappings = $cache->get('hris_mappings', function (ItemInterface $item) {
  48 + $mappings = $this->_getWithoutCache();
  49 + $item->expiresAfter($this->service->config['mapping_expire']);
  50 + return $mappings;
  51 + });
  52 + unset($cache);
  53 + }
  54 +
  55 + return $field === null ? $mappings : $mappings[$type];
  56 + }
  57 +
  58 + /**
  59 + * 无缓存获取 mappings
  60 + * @return array
  61 + */
  62 + private function _getWithoutCache() : array
  63 + {
  64 + $result = $this->service->apiRequest('get', 'mappingVO', []);
  65 + $mappings = [];
  66 + foreach ($result['mappings'] ?? [] as $mapping) {
  67 + $mappings[$mapping['mappingType']][$mapping['mappingId']] = $mapping['mappingValue'];
61 } 68 }
62 -//print_r(array_keys($mappings['data']));  
63 - return $field === null ? $mappings['data'] : $mappings['data'][$type]; 69 + unset($result, $mapping);
  70 + return $mappings;
64 } 71 }
65 72
66 /** 73 /**
src/Hris.php
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 namespace deepseath\hris; 8 namespace deepseath\hris;
9 9
10 use Symfony\Component\Cache\Adapter\FilesystemAdapter; 10 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  11 +use Symfony\Contracts\Cache\ItemInterface;
11 12
12 class Hris 13 class Hris
13 { 14 {
@@ -125,11 +126,22 @@ class Hris @@ -125,11 +126,22 @@ class Hris
125 */ 126 */
126 public function getTokenInfo(bool $force = false) : array 127 public function getTokenInfo(bool $force = false) : array
127 { 128 {
128 - $token = $this->_cache->getItem('token');  
129 - if ($token->isHit() && $force === false) {  
130 - return $token->get(); 129 + if ($force !== false) {
  130 + return $this->_getTokenInfoWithoutCache();
  131 + } else {
  132 + return $this->_cache->get('hris_token', function (ItemInterface $item) {
  133 + $tokenInfo = $this->_getTokenInfoWithoutCache();
  134 + $item->expiresAfter($tokenInfo['expires_in']);
  135 + return $tokenInfo;
  136 + });
131 } 137 }
  138 + }
132 139
  140 + /**
  141 + * 获取 token 信息,无缓存
  142 + */
  143 + private function _getTokenInfoWithoutCache()
  144 + {
133 // 初始化 token 信息 145 // 初始化 token 信息
134 $newTokenInfo = []; 146 $newTokenInfo = [];
135 // 构造 token 获取接口地址 147 // 构造 token 获取接口地址
@@ -155,19 +167,9 @@ class Hris @@ -155,19 +167,9 @@ class Hris
155 if ($newTokenInfo['code'] != 200) { 167 if ($newTokenInfo['code'] != 200) {
156 throw new \Exception('HRIS TOKEN GET ERROR:' . $newTokenInfo['msg'] . ':' . $newTokenInfo['code']); 168 throw new \Exception('HRIS TOKEN GET ERROR:' . $newTokenInfo['msg'] . ':' . $newTokenInfo['code']);
157 } 169 }
158 -  
159 - $newTokenInfo['_datetime'] = date('Y-m-d H:i:s');  
160 - // 将本次获取的 token 值写入到缓存  
161 - $token->set($newTokenInfo);  
162 - if (isset($newTokenInfo['expires_in'])) {  
163 - $expire = $newTokenInfo['expires_in'];  
164 - } else {  
165 - $expire = $this->_tokenExpire; 170 + if (!isset($newTokenInfo['expires_in'])) {
  171 + $newTokenInfo['expires_in'] = $this->_tokenExpire;
166 } 172 }
167 - $token->expiresAfter($expire - 10);  
168 - $this->log($token->get(), 'Save Token');  
169 - $this->_cache->save($token);  
170 -  
171 return $newTokenInfo; 173 return $newTokenInfo;
172 } 174 }
173 175