CacheModel.php 8.0 KB

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