baseService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. class baseService{
  6. /**
  7. * 通过主键Id从数据库取数据并生成缓存 (非强制更新缓存时($forceUpdate=false),此缓存键必须不存在,否则报错--键已存在!)
  8. * @param $id 表主键ID
  9. * @param $model 表对应的模型
  10. * @param string $cacheKey 缓存键
  11. * @param int $expire 过期时间(默认无过期)
  12. * @param bool $forceUpdate 是否强制从数据库里取,并更新缓存数据(false:默认在键不存时设置缓存; true:强制从数据库取数据更新缓)
  13. * @return mixed
  14. */
  15. public static function setCacheById($id, $model, $cacheKey = '', $expire = 0, $forceUpdate = false){
  16. $fresh = false;//是否更新缓存
  17. if(empty($cacheKey)){
  18. $preKey = dict::getCacheKey($model);
  19. if(empty($preKey)){
  20. throw new \Exception('get' . $model . 'model cache key fail from dict::getCacheKey');
  21. }
  22. $cacheKey = $preKey.$id;
  23. }
  24. $class = "\common\models\\$model";
  25. //是否存在此键值
  26. $exist = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]);
  27. if(!$exist){//不存在 此键值
  28. $data = $class::find()->where(['id' => $id])->asArray()->one();
  29. $fresh = empty($data) ? false : true;
  30. }else{//存在 此键值(是否强制从数据库里取,并更新缓存数据)
  31. if($forceUpdate){
  32. $data = $class::find()->where(['id' => $id])->asArray()->one();
  33. $fresh = empty($data) ? false : true;
  34. }else{
  35. throw new \Exception('Cache Key: '. $cacheKey . ' is existed');
  36. }
  37. }
  38. if($fresh){
  39. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//先删除
  40. $params = [$cacheKey];
  41. if(!empty($data)){
  42. foreach($data as $key => $val){
  43. $params[] = $key;
  44. $params[] = $val;
  45. }
  46. }
  47. Yii::$app->redis->executeCommand('HMSET', $params);
  48. if($expire > 0){
  49. Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]);
  50. }
  51. }
  52. return $data;
  53. }
  54. /**
  55. * 传数组数据更新缓存
  56. * @param $data 数据
  57. * @param string $cacheKey 缓存键
  58. * @param int $expire 过期时间(默认无过期)
  59. * @return bool
  60. * @throws \Exception
  61. */
  62. public static function setCacheByData(array $data, $model, $cacheKey = '', $expire = 0){
  63. $fresh = false;//是否更新缓存
  64. if(empty($cacheKey)){
  65. $preKey = dict::getCacheKey($model);
  66. if(empty($preKey)){
  67. throw new \Exception('get ' . $model . ' model cache key fail from dict::getCacheKey');
  68. }
  69. if(!isset($data['id'])){
  70. throw new \Exception('parameter data array not exist key id');
  71. }
  72. $cacheKey = $preKey.$data['id'];
  73. }
  74. //是否存在 此键值
  75. $exist = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]);
  76. if(!$exist){//不存在 此键值
  77. }
  78. $fresh = (!empty($data)) ? true : false;//是数组且不为空
  79. if($fresh){
  80. $params = [$cacheKey];
  81. foreach($data as $key => $val){
  82. $params[] = $key;
  83. $params[] = $val;
  84. }
  85. Yii::$app->redis->executeCommand('HMSET', $params);
  86. if($expire > 0){
  87. Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]);
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * 传数组数据更新缓存(此缓存键必须存在,否则报错--键不存在! ///- 安全起见,只能函数setCacheById、setCacheByData生成新键 -///)
  95. * @param $data 数据
  96. * @param string $cacheKey 缓存键
  97. * @param int $expire 过期时间(默认无过期)
  98. * @return bool
  99. * @throws \Exception
  100. */
  101. public static function updateCacheByData(array $data, $model, $cacheKey = '', $expire = 0){
  102. $fresh = false;//是否更新缓存
  103. if(empty($cacheKey)){
  104. $preKey = dict::getCacheKey($model);
  105. if(empty($preKey)){
  106. throw new \Exception('get ' . $model . ' model cache key fail from dict::getCacheKey');
  107. }
  108. if(!isset($data['id'])){
  109. throw new \Exception('parameter data array not exist key id');
  110. }
  111. $cacheKey = $preKey.$data['id'];
  112. }
  113. //是否存在 此键值
  114. $exist = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]);
  115. if(!$exist){//不存在 此键值
  116. throw new \Exception('Cache Key: '. $cacheKey . ' is not existed');
  117. }
  118. $fresh = (!empty($data)) ? true : false;//是数组且不为空
  119. if($fresh){
  120. $params = [$cacheKey];
  121. foreach($data as $key => $val){
  122. $params[] = $key;
  123. $params[] = $val;
  124. }
  125. Yii::$app->redis->executeCommand('HMSET', $params);
  126. if($expire > 0){
  127. Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, $expire]);
  128. }
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * 获取Hgetall(哈希表)缓存
  135. * @param $cacheKey
  136. * @return array
  137. */
  138. public static function getCache($cacheKey){
  139. $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  140. //$cacheData = Yii::$app->redis->hGetAll($key);
  141. if(!empty($cacheData)){
  142. $arr = [];
  143. $i = 0;
  144. $x = 0;
  145. foreach($cacheData as $key => $val){
  146. $i++;
  147. if($i%2 == 0){
  148. $arr[$x]['value'] = $val;
  149. $x++;
  150. }else{
  151. $arr[$x]['key'] = $val;
  152. }
  153. }
  154. $cacheDataArr = [];
  155. foreach($arr as $key => $val){
  156. $cacheDataArr[$val['key']] = $val['value'];
  157. }
  158. return $cacheDataArr;
  159. }
  160. return false;
  161. }
  162. }