SysAreaChinaCache.Class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * 地区缓存
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/5
  7. * Time: 14:26
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Factory;
  13. class SysAreaChinaCache
  14. {
  15. private $cache;
  16. protected $allProvince = 'province';//所有省key
  17. protected $provinceCodeRelationCityKey = 'provinceCodeRelationCity';//省份下的市key
  18. protected $cityCodeRelationAreaKey = 'cityCodeRelationArea'; //市下的区县key
  19. protected $codeRelationNameKey = 'codeRelationName';//code=>name的key
  20. protected $allKey = 'AreaChinaCache';
  21. protected $notArea = 'notArea';//缓存所有不存在的地区
  22. public function __construct($cacheDb = 'mapping')
  23. {
  24. $this->cache = Factory::cache($cacheDb);
  25. }
  26. /**
  27. * 缓存所有省
  28. * @param $data
  29. * @return ResultWrapper
  30. */
  31. public function cacheAllProvince($data)
  32. {
  33. $result = $this->cache->set($this->allProvince, gzcompress(json_encode($data)));
  34. if ($result) {
  35. return ResultWrapper::success('success');
  36. } else {
  37. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  38. }
  39. }
  40. /**
  41. * 获取缓存中的所有省
  42. * @return array|mixed
  43. */
  44. public function getAllProvince()
  45. {
  46. $result = $this->cache->get($this->allProvince);
  47. if (!$result) {
  48. return [];
  49. }
  50. return json_decode(gzuncompress($result));
  51. }
  52. /**
  53. * 判断是否有省份的缓存Key
  54. * @return boolean
  55. */
  56. public function isHaveProvinceKey()
  57. {
  58. $result = $this->cache->has($this->allProvince);
  59. return $result;
  60. }
  61. /**
  62. * 缓存对应省份下城市
  63. * @param $data
  64. * @param $code
  65. * @return ResultWrapper
  66. */
  67. public function cacheCityByCode($data, $code)
  68. {
  69. $result = $this->cache->set($this->provinceCodeRelationCityKey . '::' . $code, gzcompress(json_encode($data)));
  70. if ($result) {
  71. return ResultWrapper::success('success');
  72. } else {
  73. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  74. }
  75. }
  76. /**
  77. * 通过省份code-->市
  78. * @param $code
  79. * @return array|mixed
  80. */
  81. public function getCityByProvinceCode($code)
  82. {
  83. $result = $this->cache->get($this->provinceCodeRelationCityKey . '::' . $code);
  84. if (!$result) {
  85. return [];
  86. }
  87. return json_decode(gzuncompress($result));
  88. }
  89. /**
  90. * 判断缓存中是否有此key(城市)
  91. * @param $code
  92. * @return mixed
  93. */
  94. public function isHaveCityKeyByProvinceCode($code)
  95. {
  96. $result = $this->cache->has($this->provinceCodeRelationCityKey . '::' . $code);
  97. return $result;
  98. }
  99. /**
  100. * 缓存市下的区
  101. * @param $data
  102. * @param $code
  103. * @return ResultWrapper
  104. */
  105. public function cacheAreaByCode($data, $code)
  106. {
  107. $result = $this->cache->set($this->cityCodeRelationAreaKey . '::' . $code, gzcompress(json_encode($data)));
  108. if ($result) {
  109. return ResultWrapper::success('success');
  110. } else {
  111. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  112. }
  113. }
  114. /**
  115. * 通过市Code->区
  116. * @param $code
  117. * @return array|mixed
  118. */
  119. public function getAreaByCityCode($code)
  120. {
  121. $result = $this->cache->get($this->cityCodeRelationAreaKey . '::' . $code);
  122. if (!$result) {
  123. return [];
  124. }
  125. return json_decode(gzuncompress($result));
  126. }
  127. /**
  128. * 判断缓存中是否有此key(区)
  129. * @param $code
  130. * @return mixed
  131. */
  132. public function isHaveAreaKeyByCityCode($code)
  133. {
  134. $result = $this->cache->has($this->cityCodeRelationAreaKey . '::' . $code);
  135. return $result;
  136. }
  137. /**
  138. * 缓存 name => code
  139. * @param $name
  140. * @param $code
  141. * @return ResultWrapper
  142. */
  143. public function cacheCodeRelationName($name, $code)
  144. {
  145. $result = $this->cache->zadd($this->codeRelationNameKey, $code, $name);
  146. if ($result) {
  147. return ResultWrapper::success('success');
  148. } else {
  149. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  150. }
  151. }
  152. /**
  153. * 通过code获取集合中的name
  154. * @param $code
  155. * @return string
  156. */
  157. public function getNameByCode($code)
  158. {
  159. $result = $this->cache->zrangebyscore($this->codeRelationNameKey, $code, $code);
  160. if (!$result) {
  161. return '';
  162. }
  163. return $result;
  164. }
  165. /**
  166. * 通过name获取code
  167. * @param $name
  168. * @return int
  169. */
  170. public function getCodeByName($name): int
  171. {
  172. $result = $this->cache->zscore($this->codeRelationNameKey, $name);
  173. if (!$result) return 0;
  174. return $result;
  175. }
  176. /********************************************通过地区名称缓存code******************************************/
  177. /**
  178. * 保存
  179. * @param $key
  180. * @param $value
  181. * @return int
  182. */
  183. public function setCache($key, $value)
  184. {
  185. return $this->cache->hset($this->allKey, $key, $value);
  186. }
  187. /**
  188. * Doc: (des="")
  189. * User: XMing
  190. * Date: 2021/1/20
  191. * Time: 5:25 下午
  192. */
  193. public function setNotArea($field,$value = 0)
  194. {
  195. return $this->cache->hset($this->notArea, $field, $value);
  196. }
  197. /**
  198. * 获取
  199. * @param $key
  200. * @return string
  201. */
  202. public function getCache($key)
  203. {
  204. $result = $this->cache->hget($this->allKey, $key);
  205. if(!$result){
  206. return '';
  207. }
  208. return $result;
  209. }
  210. /**
  211. * 删除
  212. * @param $key
  213. * @return int
  214. */
  215. public function delCache($key){
  216. return $this->cache->hdel($this->allKey, $key);
  217. }
  218. }