BaseCache.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * 基础缓存类
  6. * Author: zal
  7. * Date: 2020-04-24
  8. * Time: 16:35
  9. */
  10. namespace common\components\sensitivewords;
  11. use yii;
  12. class BaseCache implements InterfaceCache
  13. {
  14. /** @var int 有效期 单位秒 */
  15. protected $cache;
  16. protected $prefix = "qimall_v1_";
  17. public function __construct()
  18. {
  19. $this->cache = Yii::$app->cache;
  20. }
  21. /**
  22. * 设置键值对缓存数据
  23. * @param $key
  24. * @param $value
  25. * @param int $duration
  26. * @return bool
  27. */
  28. public function setValue($key, $value,$duration = 3600)
  29. {
  30. // TODO: Implement setValue() method.
  31. if ($this->cache)
  32. return $this->cache->set($this->prefix.$key, $value,$duration);
  33. return false;
  34. }
  35. /**
  36. * 获取键值
  37. * @param $key
  38. * @return bool
  39. */
  40. public function getValue($key)
  41. {
  42. if (isset($_GET['noCache']) && $_GET['noCache']=='y')
  43. return false;
  44. // TODO: Implement getValue() method.
  45. if ($this->cache)
  46. return $this->cache->get($this->prefix.$key);
  47. return false;
  48. }
  49. /**
  50. * 删除键
  51. * @param $key
  52. * @return bool
  53. */
  54. public function deleteValue($key)
  55. {
  56. // TODO: Implement deleteValue() method.
  57. if ($this->cache)
  58. return $this->cache->delete($this->prefix.$key);
  59. return false;
  60. }
  61. /**
  62. * 获取有效期
  63. * @return int
  64. */
  65. public function getDuration()
  66. {
  67. return $this->duration;
  68. }
  69. }