ArticleService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\SystemArticleModel;
  4. use utils\RedisCache;
  5. /**
  6. * 文章服务 by wes
  7. * Class ArticleService
  8. * @package app\common\service
  9. */
  10. class ArticleService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new SystemArticleModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 按类型获取列表
  31. * @param $groupKey 分组
  32. * @return array|mixed
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getListByType($type)
  38. {
  39. $cacheKey = "caches:articles:list_{$type}";
  40. $data = RedisCache::get($cacheKey);
  41. if($data){
  42. return $data;
  43. }
  44. $where = ['type'=> $type];
  45. $data = $this->model->where($where)
  46. ->where('id', 'not in', '6,7')
  47. ->order('create_time desc')
  48. ->select();
  49. $data = $data? $data->toArray():[];
  50. if($data){
  51. RedisCache::set($cacheKey, $data, rand(10,20));
  52. }
  53. return $data;
  54. }
  55. }