BookList.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class BookList extends Model
  6. {
  7. // 表名
  8. protected $table = 'book_list';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'status_text',
  17. ];
  18. public function getStatusList()
  19. {
  20. return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
  21. }
  22. public function getStatusTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : $data['status'];
  25. $list = $this->getStatusList();
  26. return isset($list[$value]) ? $list[$value] : '';
  27. }
  28. /**
  29. * 返回详情
  30. * @param $id
  31. * @return array
  32. */
  33. public function getInfo($id)
  34. {
  35. $result = [];
  36. $redis = Redis::instance();
  37. $key = 'BLP:'. $id; //书单信息缓存
  38. if ($redis->exists($key)) {
  39. $result = $redis->hGetAll($key);
  40. } else {
  41. $result = $this->where('id', 'EQ', $id)->find();
  42. if (!empty($result)) {
  43. $redis->hMSet($key, $result->toArray());
  44. $redis->expire($key, 600);
  45. }
  46. }
  47. return $result;
  48. }
  49. public function rmCache($id)
  50. {
  51. if (!empty($id)) {
  52. $redis = Redis::instance();
  53. $key = 'BLP:'. $id; //书单信息缓存
  54. if ($redis->exists($key)) {
  55. $redis->del($key);
  56. }
  57. }
  58. return true;
  59. }
  60. }