CacheRepository.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\repositories\system;
  12. use app\common\dao\system\CacheDao;
  13. use app\common\repositories\BaseRepository;
  14. use think\db\exception\DbException;
  15. /**
  16. * Class CacheRepository
  17. * @package app\common\repositories\system
  18. * @author xaboy
  19. * @day 2020-04-24
  20. * @mixin CacheDao
  21. */
  22. class CacheRepository extends BaseRepository
  23. {
  24. /**
  25. * CacheRepository constructor.
  26. * @param CacheDao $dao
  27. */
  28. public function __construct(CacheDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * @param string $key
  34. * @param $result
  35. * @param int $expire_time
  36. * @throws DbException
  37. * @author xaboy
  38. * @day 2020-04-24
  39. */
  40. public function save(string $key, $result, int $expire_time = 0)
  41. {
  42. if (!$this->dao->fieldExists('key', $key)) {
  43. $this->dao->create(compact('key', 'result', 'expire_time'));
  44. } else {
  45. $this->dao->keyUpdate($key, compact('result', 'expire_time'));
  46. }
  47. }
  48. }