CacheDao.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\common\dao\system;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\system\Cache;
  6. use think\db\exception\DbException;
  7. /**
  8. * Class CacheDao
  9. * @package app\common\dao\system
  10. * @author xaboy
  11. * @day 2020-04-24
  12. */
  13. class CacheDao extends BaseDao
  14. {
  15. /**
  16. * @return BaseModel
  17. * @author xaboy
  18. * @day 2020-03-30
  19. */
  20. protected function getModel(): string
  21. {
  22. return Cache::class;
  23. }
  24. /**
  25. * @param $key
  26. * @return mixed
  27. * @author xaboy
  28. * @day 2020-04-24
  29. */
  30. public function getResult($key)
  31. {
  32. $val = Cache::getDB()->where('key', $key)->value('result');
  33. return $val ? json_decode($val, true) : null;
  34. }
  35. /**
  36. * @param string $key
  37. * @param $data
  38. * @throws DbException
  39. * @author xaboy
  40. * @day 2020-04-24
  41. */
  42. public function keyUpdate(string $key, $data)
  43. {
  44. if (isset($data['result']))
  45. $data['result'] = json_encode($data['result'], JSON_UNESCAPED_UNICODE);
  46. Cache::getDB()->where('key', $key)->update($data);
  47. }
  48. }