123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace JinDouYun\Cache;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- class InventoryCache
- {
- private $cache;
- private $enterpriseId;
- private $userCenterId;
- private $cacheKey = 'Inventory';
- private $cacheKeyShopLock = 'InventoryLock';
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->cache = Factory::cache('mapping');
- }
-
-
- public function addCacheHash($key,$value)
- {
- $data = json_encode($value);
- $result = $this->cache->hset($this->cacheKey . '::' . $this->enterpriseId.'::'.$value['warehouseId'], $key, $data);
- if (!$result) {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- return ResultWrapper::success('success');
- }
-
- public function getCacheHash($skuId, $warehouseId, $type = false)
- {
- $result = $this->cache->hget($this->cacheKey.'::'.$this->enterpriseId.'::'.$warehouseId, $skuId);
- if (!$result) return '';
- $returnData = json_decode($result, true);
- if($type){
- return $returnData[$type];
- }
- return $returnData;
- }
-
- public function delCacheHash($skuId, $warehouseId)
- {
- $this->cache->hdel($this->cacheKey.'::'.$this->enterpriseId.'::'.$warehouseId, $skuId);
- }
-
-
- public function addShopLock($shopId, $skuId, $shopLock)
- {
- $result = $this->cache->zadd($this->cacheKeyShopLock . '::' . $this->enterpriseId.'::'.$shopId, $shopLock, $skuId);
- if($result){
- return true;
- }
- return false;
- }
-
- public function getShopLock($shopId, $skuId)
- {
- return $this->cache->zscore($this->cacheKeyShopLock.'::'.$this->enterpriseId.'::'.$shopId, $skuId);
- }
-
- public function delShopLock($skuId, $shopId)
- {
- return $this->cache->zrem($this->cacheKeyShopLock.'::'.$this->enterpriseId.'::'.$shopId, $skuId);
- }
- }
|