123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- /**
- * 秒杀活动限购
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2020/4/27
- * Time: 10:30
- */
- namespace JinDouYun\Cache;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Factory;
- class ActivityLimitCache
- {
- /**
- * @var Redis
- */
- private $cache;
- private $enterpriseId;
- private static $goodsLimitKey = 'list';
- private static $userLimitTable = 'table';
- private static $activityDetails = 'details';
- /**
- * ActivityLimitCache constructor.
- * @param $enterpriseId
- * @throws \Exception
- */
- public function __construct($enterpriseId)
- {
- $this->enterpriseId = $enterpriseId;
- $this->cache = Factory::cache('activity');
- }
- /**************************************秒杀商品总数量 start********************************************************/
- /**
- * @param $activityId
- * @param $goodsId
- * @return string
- */
- public function createListKey($activityId, $goodsId,$skuId)
- {
- return $this->enterpriseId . '_' . $activityId . '_' . $goodsId . '_' .$skuId.'_'. self::$goodsLimitKey;
- }
- /**
- * 插入链表d
- * @param $activityId
- * @param $goodsId
- * @param array $value
- * @return mixed
- */
- public function lPush($activityId, $goodsId, $skuId,array $value)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- $dbResult = false;
- foreach ($value as $val) {
- $dbResult = $this->cache->push($key, $val, 'start');
- }
- return $dbResult;
- }
- /**
- * 减商品限购数量
- * @param $activityId
- * @param $goodsId
- * @param int $number
- * @return array
- */
- public function rPop($activityId, $goodsId, $skuId,$number = 1)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- $result = [];
- for ($i = 0; $i < $number; $i++) {
- if ($value = $this->cache->pop($key, 'end')) {
- $result[] = $value;
- }
- }
- return $result;
- }
- /**
- * 秒杀活动商品剩余数量
- * @param $activityId
- * @param $goodsId
- * @param $skuId
- * @return mixed
- */
- public function getLen($activityId, $goodsId,$skuId)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- return $this->cache->llen($key);
- }
- /**
- * 删除key
- * @param $activityId
- * @param $goodsId
- * @return mixed
- */
- public function delKey($activityId, $goodsId,$skuId)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- return $this->cache->del($key);
- }
- /**************************************秒杀商品总数量 end********************************************************/
- /**************************************用户活动商品限购数量 start******************************************************/
- /**
- * 用户限购表
- * @param $activityId
- * @return string
- */
- public function createTableKey($activityId)
- {
- return $this->enterpriseId . '_' . $activityId . '_' . self::$userLimitTable;
- }
- /**
- * 活动商品购买记录
- * @param $activityId
- * @param $goodsId
- * @param $skuId
- * @param $userCenterId
- * @param int $number
- * @return mixed
- */
- public function writeLimit($activityId, $goodsId, $skuId, $userCenterId, $number = 1)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hset($table, $goodsId . '_' . $skuId . '_' . $userCenterId, $number);
- }
- /**
- * 缓存活动数据
- * @param $activityId
- * @param $goodsId
- * @param $value
- * @return mixed
- */
- public function writeActivity($activityId, $value)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hset($table, self::$activityDetails, json_encode($value));
- }
- /**
- * 获取活动详情
- * @param $activityId
- * @return mixed
- */
- public function getActivity($activityId)
- {
- $table = self::createTableKey($activityId);
- $result = $this->cache->hget($table, self::$activityDetails);
- if (!empty($result)) {
- $result = json_decode($result, true);
- }
- return $result;
- }
- /**
- * 获取用户已购购数量
- * @param $activityId
- * @param $goodsId
- * @param $skuId
- * @param $userCenterId
- * @return mixed
- */
- public function getLimit($activityId, $goodsId, $skuId,$userCenterId)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hget($table, $goodsId . '_' . $skuId . '_' . $userCenterId);
- }
- }
|