SkuCache.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace JinDouYun\Cache;
  3. use Mall\Framework\Core\ErrorCode;
  4. use Mall\Framework\Core\ResultWrapper;
  5. use Mall\Framework\Factory;
  6. use Mall\Framework\Cache\Redis;
  7. class SkuCache
  8. {
  9. private $onlineEnterpriseId;
  10. private static $skuKey = 'skuHash';
  11. private static $goodsBasicSkuKey = 'goodsBasicSku';
  12. /**
  13. * @var Redis
  14. */
  15. private $cache;
  16. public function __construct($enterpriseId)
  17. {
  18. $this->onlineEnterpriseId = $enterpriseId;
  19. $this->cache = Factory::cache('mapping');
  20. }
  21. /**
  22. * @param int $skuId
  23. * @param array $data
  24. * @return ResultWrapper
  25. */
  26. public function cacheSku(int $skuId,array $data)
  27. {
  28. $data = gzcompress(serialize($data));
  29. $result = $this->cache->hset(self::$skuKey. '::' . $this->onlineEnterpriseId, $skuId, $data);
  30. if ($result) {
  31. return ResultWrapper::success('添加缓存成功');
  32. } else {
  33. return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
  34. }
  35. }
  36. /**
  37. * $fields
  38. * true : 返回所有字段信息(array)
  39. * field : 返回对应字段值 (string)
  40. * @param int $skuId
  41. * @param string $fields
  42. * @return array|bool|mixed|string
  43. */
  44. public function getSku(int $skuId, $fields='unitName')
  45. {
  46. $result = $this->cache->hget(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
  47. if(empty($result) || $result == false) return '';
  48. $result = unserialize(zlib_decode($result));
  49. $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
  50. if($fields === true){
  51. return $result;
  52. }
  53. if (isset($result[$fields])){
  54. return $result[$fields];
  55. }
  56. return '';
  57. }
  58. /**
  59. * @param int $skuId
  60. * @return bool|int
  61. */
  62. public function delSku(int $skuId)
  63. {
  64. return $this->cache->hdel(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
  65. }
  66. /********************************以商品基础资料id为key*******************************/
  67. /**
  68. * 添加sku
  69. * @param int $goodsBasicId
  70. * @param array $data
  71. * @return ResultWrapper
  72. */
  73. public function addGoodsBasicSku(int $goodsBasicId,array $data)
  74. {
  75. $data = gzcompress(serialize($data));
  76. $result = $this->cache->hset(self::$goodsBasicSkuKey. '::' . $this->onlineEnterpriseId, $goodsBasicId, $data);
  77. if ($result) {
  78. return ResultWrapper::success('添加缓存成功');
  79. } else {
  80. return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
  81. }
  82. }
  83. /**
  84. * 获取sku
  85. * true : 返回所有字段信息(array)
  86. * field : 返回对应字段值 (string)
  87. * @param int $goodsBasicId
  88. * @return array|bool|mixed|string
  89. */
  90. public function getGoodsBasicSku($goodsBasicId)
  91. {
  92. $result = $this->cache->hget(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
  93. if(empty($result) || $result == false) return [];
  94. $result = unserialize(zlib_decode($result));
  95. return $result;
  96. }
  97. /**
  98. * 删除sku
  99. * @param int $goodsBasicId
  100. * @return bool|int
  101. */
  102. public function delGoodsBasicSku(int $goodsBasicId)
  103. {
  104. return $this->cache->hdel(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
  105. }
  106. }