123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace JinDouYun\Cache;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- use Mall\Framework\Cache\Redis;
- class SkuCache
- {
- private $onlineEnterpriseId;
- private static $skuKey = 'skuHash';
- private static $goodsBasicSkuKey = 'goodsBasicSku';
- /**
- * @var Redis
- */
- private $cache;
- public function __construct($enterpriseId)
- {
- $this->onlineEnterpriseId = $enterpriseId;
- $this->cache = Factory::cache('mapping');
- }
- /**
- * @param int $skuId
- * @param array $data
- * @return ResultWrapper
- */
- public function cacheSku(int $skuId,array $data)
- {
- $data = gzcompress(serialize($data));
- $result = $this->cache->hset(self::$skuKey. '::' . $this->onlineEnterpriseId, $skuId, $data);
- if ($result) {
- return ResultWrapper::success('添加缓存成功');
- } else {
- return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
- }
- }
- /**
- * $fields
- * true : 返回所有字段信息(array)
- * field : 返回对应字段值 (string)
- * @param int $skuId
- * @param string $fields
- * @return array|bool|mixed|string
- */
- public function getSku(int $skuId, $fields='unitName')
- {
- $result = $this->cache->hget(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
- if(empty($result) || $result == false) return '';
- $result = unserialize(zlib_decode($result));
- $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
- if($fields === true){
- return $result;
- }
- if (isset($result[$fields])){
- return $result[$fields];
- }
- return '';
- }
- /**
- * @param int $skuId
- * @return bool|int
- */
- public function delSku(int $skuId)
- {
- return $this->cache->hdel(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
- }
- /********************************以商品基础资料id为key*******************************/
- /**
- * 添加sku
- * @param int $goodsBasicId
- * @param array $data
- * @return ResultWrapper
- */
- public function addGoodsBasicSku(int $goodsBasicId,array $data)
- {
- $data = gzcompress(serialize($data));
- $result = $this->cache->hset(self::$goodsBasicSkuKey. '::' . $this->onlineEnterpriseId, $goodsBasicId, $data);
- if ($result) {
- return ResultWrapper::success('添加缓存成功');
- } else {
- return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
- }
- }
- /**
- * 获取sku
- * true : 返回所有字段信息(array)
- * field : 返回对应字段值 (string)
- * @param int $goodsBasicId
- * @return array|bool|mixed|string
- */
- public function getGoodsBasicSku($goodsBasicId)
- {
- $result = $this->cache->hget(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
- if(empty($result) || $result == false) return [];
- $result = unserialize(zlib_decode($result));
- return $result;
- }
- /**
- * 删除sku
- * @param int $goodsBasicId
- * @return bool|int
- */
- public function delGoodsBasicSku(int $goodsBasicId)
- {
- return $this->cache->hdel(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
- }
- }
|