CacheModel.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Models;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Support\Facades\Cache;
  14. /**
  15. * 缓存模型
  16. * @author zongjl
  17. * @date 2019/5/23
  18. * Class CacheModel
  19. * @package App\Models
  20. */
  21. class CacheModel extends Model
  22. {
  23. // 是否启用缓存
  24. protected $is_cache = true;
  25. /**
  26. * 重置缓存
  27. * @param int $id 记录ID
  28. * @param array $data 数据源
  29. * @param bool $is_edit 是否编辑
  30. * @return bool 返回结果
  31. * @author zongjl
  32. * @date 2019/5/23
  33. */
  34. public function cacheReset($id, $data = [], $is_edit = false)
  35. {
  36. if (!$data) {
  37. $this->resetCacheFunc('info', $id);
  38. }
  39. $info = [];
  40. if ($is_edit) {
  41. $info = $this->getCacheFunc("info", $id);
  42. }
  43. if (is_array($data)) {
  44. $info = array_merge($info, $data);
  45. } else {
  46. $info = $data;
  47. }
  48. $cache_key = $this->getCacheKey('info', $id);
  49. $result = $this->setCache($cache_key, $info);
  50. return $result;
  51. }
  52. /**
  53. * 删除缓存
  54. * @param int $id 删除缓存ID
  55. * @return bool 返回true或false
  56. * @author zongjl
  57. * @date 2019/5/23
  58. */
  59. public function cacheDelete($id)
  60. {
  61. $cache_key = $this->getCacheKey("info", $id);
  62. $result = $this->deleteCache($cache_key);
  63. return $result;
  64. }
  65. /**
  66. * 设置单条数据缓存
  67. * @param int $id 记录ID
  68. * @return array 返回结果
  69. * @author zongjl
  70. * @date 2019/5/23
  71. */
  72. public function cacheInfo($id)
  73. {
  74. if (!$id) {
  75. return false;
  76. }
  77. $data = $this->find((int)$id);
  78. return $data ? $data->toArray() : [];
  79. }
  80. /**
  81. * 获取缓存KEY
  82. * @return string 缓存KEY
  83. * @author zongjl
  84. * @date 2019/5/23
  85. */
  86. public function getCacheKey()
  87. {
  88. $arg_list = func_get_args();
  89. if ($this->table) {
  90. array_unshift($arg_list, $this->table);
  91. }
  92. foreach ($arg_list as $key => $val) {
  93. if (is_array($val)) {
  94. unset($arg_list[$key]);
  95. }
  96. }
  97. $cache_key = implode("_", $arg_list);
  98. return $cache_key;
  99. }
  100. /**
  101. * 设置缓存
  102. * @param string $cache_key 缓存KEY
  103. * @param array $data 缓存数据
  104. * @param int $ttl 过期时间
  105. * @return bool 返回结果
  106. * @author zongjl
  107. * @date 2019/5/23
  108. */
  109. public function setCache($cache_key, $data, $ttl = 0)
  110. {
  111. if (isset($GLOBALS['trans']) && $GLOBALS['trans'] === true) {
  112. $GLOBALS['trans_keys'][] = $cache_key;
  113. }
  114. // 不设置缓存,直接返回
  115. if (!$this->is_cache) {
  116. return true;
  117. }
  118. if (!$data) {
  119. return Cache::put($cache_key, null, $ttl);
  120. }
  121. $isGzcompress = gzcompress(json_encode($data));
  122. if ($isGzcompress) {
  123. $result = Cache::put($cache_key, $isGzcompress);
  124. }
  125. return $result;
  126. }
  127. /**
  128. * 获取缓存
  129. * @param string $cache_key 缓存KEY
  130. * @return mixed 返回缓存数据
  131. * @author zongjl
  132. * @date 2019/5/23
  133. */
  134. public function getCache($cache_key)
  135. {
  136. $data = Cache::get($cache_key);
  137. if ($data) {
  138. $data = json_decode(gzuncompress($data), true);
  139. }
  140. return $data;
  141. }
  142. /**
  143. * 删除缓存
  144. * @param string $cache_key 缓存KEY
  145. * @return bool 返回结果true或false
  146. * @author zongjl
  147. * @date 2019/5/23
  148. */
  149. public function deleteCache($cache_key)
  150. {
  151. // 判断缓存键值是否存在,存在则删除
  152. if (Cache::has($cache_key)) {
  153. return Cache::forget($cache_key);
  154. }
  155. return false;
  156. }
  157. /**
  158. * 设置缓存函数
  159. * @param string $funcName 方法名
  160. * @param string $id 缓存数据ID
  161. * @return bool 返回结果true或false
  162. * @author zongjl
  163. * @date 2019/5/23
  164. */
  165. public function resetCacheFunc($funcName, $id = '')
  166. {
  167. // 获取缓存KEY
  168. $cache_key = $this->getCacheKey($funcName, $id);
  169. // 删除缓存
  170. $result = $this->deleteCache($cache_key);
  171. // 设置缓存
  172. $arg_list = func_get_args();
  173. if ($this->table) {
  174. array_shift($arg_list);
  175. }
  176. $act = "cache" . ucfirst($funcName);
  177. $data = call_user_func_array(array($this, $act), $arg_list);
  178. return $this->setCache($cache_key, $data);
  179. }
  180. /**
  181. * 获取缓存函数
  182. * @param string $funcName 方法名
  183. * @param string $id 缓存数据ID
  184. * @return mixed 返回结果
  185. * @author zongjl
  186. * @date 2019/5/23
  187. */
  188. public function getCacheFunc($funcName, $id = '')
  189. {
  190. $cache_key = $this->getCacheKey($funcName, $id);
  191. $data = $this->getCache($cache_key);
  192. if (!$data) {
  193. $arg_list = func_get_args();
  194. if ($this->table) {
  195. array_shift($arg_list);
  196. }
  197. $act = "cache" . ucfirst($funcName);
  198. $data = call_user_func_array(array($this, $act), $arg_list);
  199. $this->setCache($cache_key, $data, rand(3, 5));
  200. }
  201. return $data;
  202. }
  203. /**
  204. * 获取整表缓存
  205. * @param array $map 查询条件
  206. * @param bool $is_pri 是否只缓存主键true或false
  207. * @param bool $pri_key 是否以主键作为键值true或false
  208. * @return mixed 返回结果
  209. * @author zongjl
  210. * @date 2019/6/5
  211. */
  212. public function getAll($map = [], $is_pri = false, $pri_key = false)
  213. {
  214. $list = $this->getCacheFunc('all', $map, $is_pri, $pri_key);
  215. return $list;
  216. }
  217. /**
  218. * 设置整表缓存
  219. * @param array $map 查询条件
  220. * @param bool $is_pri 是否只缓存主键true或false
  221. * @param bool $pri_key 是否以主键作为键值true或false
  222. * @return mixed 返回结果
  223. * @author zongjl
  224. * @date 2019/6/5
  225. */
  226. public function cacheAll($map = [], $is_pri = false, $pri_key = false)
  227. {
  228. // 必备查询条件
  229. $map[] = ['mark', '=', 1];
  230. // 格式化查询条件
  231. if (method_exists($this, 'formatQuery')) {
  232. $query = $this->formatQuery($this, $map);
  233. }
  234. // 是否缓存主键
  235. if ($is_pri) {
  236. if (is_array($is_pri)) {
  237. // 字段数组
  238. $query->select($is_pri);
  239. } elseif (is_string($is_pri)) {
  240. // 字段字符串
  241. $fields = explode(',', $is_pri);
  242. $query->select($fields);
  243. } else {
  244. // 默认主键ID
  245. $query->select('id');
  246. }
  247. }
  248. // 查询数据并转数组
  249. $list = $query->get()->toArray();
  250. // 设置主键ID为数组键值
  251. if ($pri_key) {
  252. $list = array_column($list, null, 'id');
  253. }
  254. return $list;
  255. }
  256. /**
  257. * 重置全表缓存
  258. * @param array $map 查询条件
  259. * @param bool $is_pri 是否只缓存主键true或false
  260. * @param bool $pri_key 是否以主键作为键值true或false
  261. * @return bool 返回结果true(重置成功)或false(重置失败)
  262. * @author zongjl
  263. * @date 2019/6/5
  264. */
  265. public function cacheResetAll($map = [], $is_pri = false, $pri_key = false)
  266. {
  267. return $this->resetCacheFunc('all', $map, $is_pri, $pri_key);
  268. }
  269. }