123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace JinDouYun\Cache;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Factory;
- class ActivityLimitCache
- {
-
- private $cache;
- private $enterpriseId;
- private static $goodsLimitKey = 'list';
- private static $userLimitTable = 'table';
- private static $activityDetails = 'details';
-
- public function __construct($enterpriseId)
- {
- $this->enterpriseId = $enterpriseId;
- $this->cache = Factory::cache('activity');
- }
-
-
- public function createListKey($activityId, $goodsId,$skuId)
- {
- return $this->enterpriseId . '_' . $activityId . '_' . $goodsId . '_' .$skuId.'_'. self::$goodsLimitKey;
- }
-
- 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;
- }
-
- 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;
- }
-
- public function getLen($activityId, $goodsId,$skuId)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- return $this->cache->llen($key);
- }
-
- public function delKey($activityId, $goodsId,$skuId)
- {
- $key = self::createListKey($activityId, $goodsId,$skuId);
- return $this->cache->del($key);
- }
-
-
-
- public function createTableKey($activityId)
- {
- return $this->enterpriseId . '_' . $activityId . '_' . self::$userLimitTable;
- }
-
- public function writeLimit($activityId, $goodsId, $skuId, $userCenterId, $number = 1)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hset($table, $goodsId . '_' . $skuId . '_' . $userCenterId, $number);
- }
-
- public function writeActivity($activityId, $value)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hset($table, self::$activityDetails, json_encode($value));
- }
-
- 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;
- }
-
- public function getLimit($activityId, $goodsId, $skuId,$userCenterId)
- {
- $table = self::createTableKey($activityId);
- return $this->cache->hget($table, $goodsId . '_' . $skuId . '_' . $userCenterId);
- }
- }
|