TempSaveCache.Class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wxj
  5. * Date: 2020-03-24
  6. * Time: 15:11
  7. */
  8. namespace JinDouYun\Cache;
  9. use http\Exception;
  10. use Mall\Framework\Factory;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Core\ErrorCode;
  13. class TempSaveCache
  14. {
  15. private $cache;
  16. protected $vipCardTemporarySaveKey = 'vipCardTempSave';//会员卡暂存
  17. protected $goodsTemporarySaveKey = 'goodsTempSave';//商品暂存
  18. protected $customerTempSaveKey = 'customerTempSave';//客户暂存
  19. protected $shopTempSaveKey = 'shopTempSave';//商铺暂存
  20. public function __construct()
  21. {
  22. $this->cache = Factory::cache('default');
  23. }
  24. /**
  25. * 会员卡暂存
  26. * @param $enterpriseId
  27. * @param $userCenterId
  28. * @param $data
  29. * @return ResultWrapper
  30. */
  31. public function saveVipCard($enterpriseId, $userCenterId, $data)
  32. {
  33. if (empty($data)) {
  34. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  35. }
  36. return $this->cache->hset($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
  37. }
  38. /**
  39. * 获取会员卡暂存数据
  40. * @param $enterpriseId
  41. * @param $userCenterId
  42. * @return boolean
  43. */
  44. public function getVipCard($enterpriseId, $userCenterId)
  45. {
  46. $data = $this->cache->hget($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
  47. return $data ? json_decode($data, true) : [];
  48. }
  49. /**
  50. * 删除会员卡暂存数据
  51. * @param $enterpriseId
  52. * @param $userCenterId
  53. */
  54. public function delVipCard($enterpriseId, $userCenterId)
  55. {
  56. $result = $this->cache->hdel($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
  57. }
  58. /**
  59. * 客户暂存
  60. * @param $enterpriseId
  61. * @param $userCenterId
  62. * @param $data
  63. * @return ResultWrapper
  64. */
  65. public function saveCustomer($enterpriseId, $userCenterId, $data)
  66. {
  67. if (empty($data)) {
  68. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  69. }
  70. return $this->cache->hset($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
  71. }
  72. /**
  73. * 获取客户暂存数据
  74. * @param $enterpriseId
  75. * @param $userCenterId
  76. * @return boolean
  77. */
  78. public function getCustomer($enterpriseId, $userCenterId)
  79. {
  80. $data = $this->cache->hget($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId);
  81. return $data ? json_decode($data, true) : [];
  82. }
  83. /**
  84. * 删除客户暂存数据
  85. * @param $enterpriseId
  86. * @param $userCenterId
  87. */
  88. public function delCustomer($enterpriseId, $userCenterId)
  89. {
  90. $result = $this->cache->hdel($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId);
  91. }
  92. /**
  93. * 商铺暂存
  94. * @param $enterpriseId
  95. * @param $userCenterId
  96. * @param $data
  97. * @return ResultWrapper
  98. */
  99. public function saveShop($enterpriseId, $userCenterId, $data)
  100. {
  101. if (empty($data)) {
  102. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  103. }
  104. return $this->cache->hset($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
  105. }
  106. /**
  107. * 获取商铺暂存数据
  108. * @param $enterpriseId
  109. * @param $userCenterId
  110. * @return boolean
  111. */
  112. public function getShop($enterpriseId, $userCenterId)
  113. {
  114. $data = $this->cache->hget($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId);
  115. return $data ? json_decode($data, true) : [];
  116. }
  117. /**
  118. * 删除商铺暂存数据
  119. * @param $enterpriseId
  120. * @param $userCenterId
  121. */
  122. public function delShop($enterpriseId, $userCenterId)
  123. {
  124. $result = $this->cache->hdel($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId);
  125. }
  126. /**
  127. * 商品暂存
  128. * @param $enterpriseId
  129. * @param $userCenterId
  130. * @param $data
  131. * @return ResultWrapper
  132. */
  133. public function saveGoods($enterpriseId, $userCenterId, $data)
  134. {
  135. if (empty($data)) {
  136. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  137. }
  138. return $this->cache->hset($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
  139. }
  140. /**
  141. * 获取商品暂存数据
  142. * @param $enterpriseId
  143. * @param $userCenterId
  144. * @return boolean
  145. */
  146. public function getGoods($enterpriseId, $userCenterId)
  147. {
  148. $data = $this->cache->hget($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
  149. return $data ? json_decode($data, true) : [];
  150. }
  151. /**
  152. * 删除商品暂存数据
  153. * @param $enterpriseId
  154. * @param $userCenterId
  155. */
  156. public function delGoods($enterpriseId, $userCenterId)
  157. {
  158. $result = $this->cache->hdel($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
  159. }
  160. }