InventoryCache.Class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 库存cache添加
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/01/11
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Factory;
  13. class InventoryCache
  14. {
  15. private $cache;
  16. private $enterpriseId;
  17. private $userCenterId;
  18. private $cacheKey = 'Inventory';
  19. private $cacheKeyShopLock = 'InventoryLock';
  20. public function __construct($enterpriseId, $userCenterId = false)
  21. {
  22. $this->enterpriseId = $enterpriseId;
  23. $this->userCenterId = $userCenterId;
  24. $this->cache = Factory::cache('mapping');
  25. }
  26. /**---------------------------------------------仓库库存------------------------------------------------**/
  27. /**
  28. * 添加
  29. * @param $key
  30. * @param $value
  31. * @return ResultWrapper
  32. */
  33. public function addCacheHash($key,$value)
  34. {
  35. $data = json_encode($value);
  36. $result = $this->cache->hset($this->cacheKey . '::' . $this->enterpriseId.'::'.$value['warehouseId'], $key, $data);
  37. if (!$result) {
  38. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  39. }
  40. return ResultWrapper::success('success');
  41. }
  42. /**
  43. * 查询
  44. * @param $skuId
  45. * @param $warehouseId
  46. * @param bool $type
  47. * @return mixed|string
  48. */
  49. public function getCacheHash($skuId, $warehouseId, $type = false)
  50. {
  51. $result = $this->cache->hget($this->cacheKey.'::'.$this->enterpriseId.'::'.$warehouseId, $skuId);
  52. if (!$result) return '';
  53. $returnData = json_decode($result, true);
  54. if($type){
  55. return $returnData[$type];
  56. }
  57. return $returnData;
  58. }
  59. /**
  60. * 删除
  61. * @param $skuId
  62. * @param $warehouseId
  63. */
  64. public function delCacheHash($skuId, $warehouseId)
  65. {
  66. $this->cache->hdel($this->cacheKey.'::'.$this->enterpriseId.'::'.$warehouseId, $skuId);
  67. }
  68. /**---------------------------------------------商铺锁定--有序集合------------------------------------------------**/
  69. /**
  70. * 添加
  71. */
  72. public function addShopLock($shopId, $skuId, $shopLock)
  73. {
  74. $result = $this->cache->zadd($this->cacheKeyShopLock . '::' . $this->enterpriseId.'::'.$shopId, $shopLock, $skuId);
  75. if($result){
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * 查询
  82. * @param $skuId
  83. * @param $shopId
  84. * @param bool $type
  85. * @return mixed|string
  86. */
  87. public function getShopLock($shopId, $skuId)
  88. {
  89. return $this->cache->zscore($this->cacheKeyShopLock.'::'.$this->enterpriseId.'::'.$shopId, $skuId);
  90. }
  91. /**
  92. * 删除
  93. * @param $skuId
  94. * @param $shopId
  95. */
  96. public function delShopLock($skuId, $shopId)
  97. {
  98. return $this->cache->zrem($this->cacheKeyShopLock.'::'.$this->enterpriseId.'::'.$shopId, $skuId);
  99. }
  100. }