CacheDao.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\Cache;
  15. use think\db\exception\DbException;
  16. /**
  17. * Class CacheDao
  18. * @package app\common\dao\system
  19. * @author xaboy
  20. * @day 2020-04-24
  21. */
  22. class CacheDao extends BaseDao
  23. {
  24. /**
  25. * @return BaseModel
  26. * @author xaboy
  27. * @day 2020-03-30
  28. */
  29. protected function getModel(): string
  30. {
  31. return Cache::class;
  32. }
  33. /**
  34. * @param $key
  35. * @return mixed
  36. * @author xaboy
  37. * @day 2020-04-24
  38. */
  39. public function getResult($key)
  40. {
  41. $val = Cache::getDB()->where('key', $key)->value('result');
  42. return $val ? json_decode($val, true) : null;
  43. }
  44. /**
  45. * @param string $key
  46. * @param $data
  47. * @throws DbException
  48. * @author xaboy
  49. * @day 2020-04-24
  50. */
  51. public function keyUpdate(string $key, $data)
  52. {
  53. if (isset($data['result']))
  54. $data['result'] = json_encode($data['result'], JSON_UNESCAPED_UNICODE);
  55. Cache::getDB()->where('key', $key)->update($data);
  56. }
  57. public function search(array $keys)
  58. {
  59. $cache = $this->getModel()::getDB()->whereIn('key',$keys)->column('result','key');
  60. $ret = [];
  61. foreach ($cache as $k => $v) {
  62. $ret[$k] = json_decode($v);
  63. }
  64. return $ret;
  65. }
  66. }