ActivityLimitCache.Class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * 秒杀活动限购
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2020/4/27
  7. * Time: 10:30
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Cache\Redis;
  11. use Mall\Framework\Factory;
  12. class ActivityLimitCache
  13. {
  14. /**
  15. * @var Redis
  16. */
  17. private $cache;
  18. private $enterpriseId;
  19. private static $goodsLimitKey = 'list';
  20. private static $userLimitTable = 'table';
  21. private static $activityDetails = 'details';
  22. /**
  23. * ActivityLimitCache constructor.
  24. * @param $enterpriseId
  25. * @throws \Exception
  26. */
  27. public function __construct($enterpriseId)
  28. {
  29. $this->enterpriseId = $enterpriseId;
  30. $this->cache = Factory::cache('activity');
  31. }
  32. /**************************************秒杀商品总数量 start********************************************************/
  33. /**
  34. * @param $activityId
  35. * @param $goodsId
  36. * @return string
  37. */
  38. public function createListKey($activityId, $goodsId,$skuId)
  39. {
  40. return $this->enterpriseId . '_' . $activityId . '_' . $goodsId . '_' .$skuId.'_'. self::$goodsLimitKey;
  41. }
  42. /**
  43. * 插入链表d
  44. * @param $activityId
  45. * @param $goodsId
  46. * @param array $value
  47. * @return mixed
  48. */
  49. public function lPush($activityId, $goodsId, $skuId,array $value)
  50. {
  51. $key = self::createListKey($activityId, $goodsId,$skuId);
  52. $dbResult = false;
  53. foreach ($value as $val) {
  54. $dbResult = $this->cache->push($key, $val, 'start');
  55. }
  56. return $dbResult;
  57. }
  58. /**
  59. * 减商品限购数量
  60. * @param $activityId
  61. * @param $goodsId
  62. * @param int $number
  63. * @return array
  64. */
  65. public function rPop($activityId, $goodsId, $skuId,$number = 1)
  66. {
  67. $key = self::createListKey($activityId, $goodsId,$skuId);
  68. $result = [];
  69. for ($i = 0; $i < $number; $i++) {
  70. if ($value = $this->cache->pop($key, 'end')) {
  71. $result[] = $value;
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. * 秒杀活动商品剩余数量
  78. * @param $activityId
  79. * @param $goodsId
  80. * @param $skuId
  81. * @return mixed
  82. */
  83. public function getLen($activityId, $goodsId,$skuId)
  84. {
  85. $key = self::createListKey($activityId, $goodsId,$skuId);
  86. return $this->cache->llen($key);
  87. }
  88. /**
  89. * 删除key
  90. * @param $activityId
  91. * @param $goodsId
  92. * @return mixed
  93. */
  94. public function delKey($activityId, $goodsId,$skuId)
  95. {
  96. $key = self::createListKey($activityId, $goodsId,$skuId);
  97. return $this->cache->del($key);
  98. }
  99. /**************************************秒杀商品总数量 end********************************************************/
  100. /**************************************用户活动商品限购数量 start******************************************************/
  101. /**
  102. * 用户限购表
  103. * @param $activityId
  104. * @return string
  105. */
  106. public function createTableKey($activityId)
  107. {
  108. return $this->enterpriseId . '_' . $activityId . '_' . self::$userLimitTable;
  109. }
  110. /**
  111. * 活动商品购买记录
  112. * @param $activityId
  113. * @param $goodsId
  114. * @param $skuId
  115. * @param $userCenterId
  116. * @param int $number
  117. * @return mixed
  118. */
  119. public function writeLimit($activityId, $goodsId, $skuId, $userCenterId, $number = 1)
  120. {
  121. $table = self::createTableKey($activityId);
  122. return $this->cache->hset($table, $goodsId . '_' . $skuId . '_' . $userCenterId, $number);
  123. }
  124. /**
  125. * 缓存活动数据
  126. * @param $activityId
  127. * @param $goodsId
  128. * @param $value
  129. * @return mixed
  130. */
  131. public function writeActivity($activityId, $value)
  132. {
  133. $table = self::createTableKey($activityId);
  134. return $this->cache->hset($table, self::$activityDetails, json_encode($value));
  135. }
  136. /**
  137. * 获取活动详情
  138. * @param $activityId
  139. * @return mixed
  140. */
  141. public function getActivity($activityId)
  142. {
  143. $table = self::createTableKey($activityId);
  144. $result = $this->cache->hget($table, self::$activityDetails);
  145. if (!empty($result)) {
  146. $result = json_decode($result, true);
  147. }
  148. return $result;
  149. }
  150. /**
  151. * 获取用户已购购数量
  152. * @param $activityId
  153. * @param $goodsId
  154. * @param $skuId
  155. * @param $userCenterId
  156. * @return mixed
  157. */
  158. public function getLimit($activityId, $goodsId, $skuId,$userCenterId)
  159. {
  160. $table = self::createTableKey($activityId);
  161. return $this->cache->hget($table, $goodsId . '_' . $skuId . '_' . $userCenterId);
  162. }
  163. }