Cache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/4/11
  6. * Time: 下午1:10
  7. */
  8. namespace common\helpers;
  9. use Yii;
  10. use common\models\Config;
  11. use common\models\OcrApi;
  12. use common\models\DeclarationLevel;
  13. use common\models\EmployLevel;
  14. use common\models\Region;
  15. use common\models\User;
  16. use common\models\UserInfo;
  17. class Cache
  18. {
  19. const SYSTEM_CONFIG_KEY = 'sys:config';
  20. const DEC_LEVEL_CONFIG_KEY = 'sys:decLevel';
  21. const DEC_ROLE_CONFIG_KEY = 'sys:decRole';
  22. const EMP_LEVEL_CONFIG_KEY = 'sys:empLevel';
  23. const ADMIN_ROLE_KEY = 'sys:adminRole';
  24. const SMS_TEMPLATE_DEC_KEY = 'sys:smsTemplateDec';
  25. const SMS_TEMPLATE_EMP_KEY = 'sys:smsTemplateEmp';
  26. const WITHDRAW_LEVEL_CONFIG_KEY = 'sys:withdrawLevel';
  27. const OCR_API_CONFIG_KEY = 'sys:ocrApi';
  28. const SMS_API_CONFIG_KEY = 'sys:smsApi';
  29. const REGION_CONFIG_KEY = 'sys:region';
  30. const ASYNC_KEY_PREFIX = 'async_';
  31. const SOCKET_KEY_PREFIX = 'socket_';
  32. const SOCKET_ADMIN = 'admin';
  33. const SOCKET_USER_PC = 'userPc';
  34. const SOCKET_USER_APP = 'userAPP';
  35. const USER_CLOSE_KEY_PREFIX = 'user:close_';
  36. const USER_INFO_KEY = 'user:baseInfo';
  37. const USER_NETWORK_PARENTS = 'user:networkParents';
  38. const USER_RELATION_PARENTS = 'user:relationParents';
  39. const USER_CREATED_AT_LIST = 'user:createdAtList_';
  40. /**
  41. * WebSocket的fd加入缓存
  42. * @param $app
  43. * @param $userId
  44. * @param $fd
  45. * @return mixed
  46. */
  47. public static function setWebSocketFd($app, $userId, $fd){
  48. if(isset($userId) && $userId != ''){
  49. if($app == self::SOCKET_ADMIN){
  50. $model = Admin::class;
  51. $field = 'ID';
  52. } elseif($app == self::SOCKET_USER_PC) {
  53. $model = UserInfo::class;
  54. $field = 'USER_ID';
  55. $app = self::SOCKET_USER_PC;
  56. } else {
  57. $model = UserInfo::class;
  58. $field = 'USER_ID';
  59. $app = self::SOCKET_USER_APP;
  60. }
  61. if($model::find()->where($field.'=:'.$field, [':'.$field=>$userId])->exists()){
  62. Yii::$app->redis->hset(self::SOCKET_KEY_PREFIX.$app, $userId, $fd);
  63. }
  64. }
  65. return $fd;
  66. }
  67. /**
  68. * @param $app
  69. * @param $userId
  70. * @return mixed
  71. */
  72. public static function getWebSocketFd($app, $userId){
  73. return Yii::$app->redis->hget(self::SOCKET_KEY_PREFIX.$app, $userId);
  74. }
  75. /**
  76. * @return array|mixed|\yii\db\ActiveRecord[]
  77. */
  78. public static function getSystemConfig(){
  79. return Config::getFromCache();
  80. }
  81. /**
  82. * 更新系统配置
  83. * @return array|\yii\db\ActiveRecord[]
  84. */
  85. public static function updateSystemConfig(){
  86. return Config::updateToCache();
  87. }
  88. /**
  89. * @return array|mixed|\yii\db\ActiveRecord[]
  90. */
  91. public static function getDecLevelConfig(){
  92. return DeclarationLevel::getFromCache();
  93. }
  94. /**
  95. * @return array|\yii\db\ActiveRecord[]
  96. */
  97. public static function updateDecLevelConfig(){
  98. return DeclarationLevel::updateToCache();
  99. }
  100. /**
  101. * @return array|mixed|\yii\db\ActiveRecord[]
  102. */
  103. public static function getRegionConfig(){
  104. return Region::getFromCache();
  105. }
  106. /**
  107. * @return array|\yii\db\ActiveRecord[]
  108. */
  109. public static function updateRegionConfig(){
  110. return Region::updateToCache();
  111. }
  112. /**
  113. * @return array|mixed|\yii\db\ActiveRecord[]
  114. */
  115. public static function getEmpLevelConfig(){
  116. return EmployLevel::getFromCache();
  117. }
  118. /**
  119. * @return array|\yii\db\ActiveRecord[]
  120. */
  121. public static function updateEmpLevelConfig(){
  122. return EmployLevel::updateToCache();
  123. }
  124. /**
  125. * @param $userId
  126. * @return array|null|\yii\db\ActiveRecord
  127. */
  128. public static function getUserBaseInfo($userId){
  129. return User::getBaseInfoFromRedis($userId);
  130. }
  131. /**
  132. * @param $params
  133. * @return string
  134. * @throws \yii\base\Exception
  135. */
  136. public static function setAsyncParams($params){
  137. // 生成一个随机key
  138. $taskKey = self::createRandomKey(self::ASYNC_KEY_PREFIX);
  139. Yii::$app->cache->set($taskKey, $params);
  140. return $taskKey;
  141. }
  142. /**
  143. * @param $taskKey
  144. * @return mixed
  145. */
  146. public static function getAsyncParams($taskKey){
  147. $result = Yii::$app->cache->get($taskKey);
  148. Yii::$app->cache->delete($taskKey);
  149. return $result;
  150. }
  151. /**
  152. * 获取异步参数但不删除异步参数缓存
  153. * (用于临时获取一下,但还不影响正式获取后删除的逻辑)
  154. * @param $taskKey
  155. * @return mixed
  156. */
  157. public static function getAsyncParamsWithoutDel($taskKey){
  158. return Yii::$app->cache->get($taskKey);
  159. }
  160. /**
  161. * @param $key
  162. * @return bool
  163. */
  164. public static function deleteAsyncParams($key){
  165. return Yii::$app->cache->delete($key);
  166. }
  167. /**
  168. * 生成一个随机不重复的key
  169. * @param $prefix
  170. * @return string
  171. * @throws \yii\base\Exception
  172. */
  173. public static function createRandomKey($prefix){
  174. $key = Yii::$app->security->generateRandomString(20);
  175. if(Yii::$app->cache->exists($prefix.$key)){
  176. self::createRandomKey($prefix);
  177. }
  178. return $prefix.$key;
  179. }
  180. /**
  181. * @return array|mixed|\yii\db\ActiveRecord[]
  182. */
  183. public static function getOcrApiConfig(){
  184. return OcrApi::getFromCache();
  185. }
  186. /**
  187. * @return array|\yii\db\ActiveRecord[]
  188. */
  189. public static function updateOcrApiConfig(){
  190. return OcrApi::updateToCache();
  191. }
  192. }