PauseSave.Class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 暂存cache
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/03/30
  7. * Time: 16:30
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Factory;
  13. class PauseSave
  14. {
  15. private $cache;
  16. private $enterpriseId;
  17. private $userCenterId;
  18. private $cacheBigKey;
  19. private $pauseSaveTime = '86400';//暂存保存时间
  20. private static $cacheKey = 'PauseSave';
  21. private $moneyCacheKey = 'MoneyPauseSave';
  22. public function __construct($enterpriseId, $userCenterId = false)
  23. {
  24. $userCenterId && $this->userCenterId = $userCenterId;
  25. $enterpriseId && $this->enterpriseId = $enterpriseId;
  26. $this->cacheBigKey = '';
  27. if($enterpriseId && $userCenterId){
  28. $this->cacheBigKey = self::$cacheKey.'::'.$this->enterpriseId . '_' . $this->userCenterId;
  29. $this->moneyCacheKey = $this->moneyCacheKey.'::'.$this->enterpriseId . '_' . $this->userCenterId;
  30. }
  31. $this->cache = Factory::cache('default');
  32. }
  33. /**---------------------------------------------哈希hash------------------------------------------------**/
  34. /**
  35. * 添加
  36. * @param $key
  37. * @param $value
  38. * @return bool
  39. */
  40. public function addCacheHash($key,$value)
  41. {
  42. $data = json_encode($value);
  43. $result = $this->cache->hset($this->cacheBigKey, $key, $data);
  44. if (!$result) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * 查询
  51. * @param $key
  52. * @return mixed|string
  53. */
  54. public function getCacheHash($key)
  55. {
  56. $result = $this->cache->hget($this->cacheBigKey, $key);
  57. if (!$result) return [];
  58. return json_decode($result, true);
  59. }
  60. /**
  61. * 删除
  62. * @param $key
  63. * @return array
  64. */
  65. public function delCacheHash($key)
  66. {
  67. return $this->cache->hdel($this->cacheBigKey, $key);
  68. }
  69. /***********************************价格暂存**************************************/
  70. /**
  71. * 添加
  72. * @param $key
  73. * @param $array
  74. * @return bool
  75. * @throws \Exception
  76. */
  77. public function addMoneyCache($array)
  78. {
  79. if(empty($array)){
  80. return false;
  81. }
  82. $pipe = Factory::cache('default')->multi();
  83. foreach ($array as $key => $value){
  84. Factory::cache('default')->zadd($this->moneyCacheKey, $value, $key);
  85. }
  86. $pipe->exec();
  87. return true;
  88. }
  89. /**
  90. * 查询
  91. * @param $key
  92. * @return mixed|string
  93. */
  94. public function getMoneyCache($key)
  95. {
  96. $result = $this->cache->zscore($this->moneyCacheKey, $key);
  97. return $result;
  98. }
  99. }