GoodsBasicCache.Class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * 缓存基础商品数据
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/25
  7. * Time: 12:05
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Cache\Redis;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Factory;
  13. /**
  14. * todo(这个类中有很多没用的缓存,之后清理)
  15. * Class GoodsBasicCache
  16. * @package JinDouYun\Cache
  17. */
  18. class GoodsBasicCache
  19. {
  20. /**
  21. * @var Redis
  22. */
  23. private $cache;
  24. private $onlineEnterpriseId;
  25. protected $categoryKey = 'categoryAndBasicNum';
  26. protected $brandKey = 'brandAndBasicNum';
  27. /**
  28. * GoodsBasicCache constructor.
  29. * @param $enterpriseId
  30. * @throws \Exception
  31. */
  32. public function __construct($enterpriseId)
  33. {
  34. $this->onlineEnterpriseId = $enterpriseId;
  35. $this->cache = Factory::cache('default');
  36. }
  37. /**
  38. * @param $categoryId
  39. * @param $num
  40. * @return ResultWrapper
  41. */
  42. public function cacheCategoryRelationGoodsBasicNum($categoryId, $num)
  43. {
  44. $result = $this->cache->zadd($this->categoryKey.'::'.$this->onlineEnterpriseId, $num, $categoryId);
  45. return $result;
  46. }
  47. /**
  48. * @param $categoryId
  49. * @return mixed
  50. */
  51. public function categoryKeyScoreIncr($categoryId)
  52. {
  53. $result = $this->cache->zincrby($this->categoryKey.'::'.$this->onlineEnterpriseId, 1, $categoryId);
  54. return $result;
  55. }
  56. /**
  57. * @param $categoryId
  58. * @return mixed
  59. */
  60. public function categoryKeyScoreDecr($categoryId)
  61. {
  62. $result = $this->cache->zincrby($this->categoryKey.'::'.$this->onlineEnterpriseId, -1, $categoryId);
  63. return $result;
  64. }
  65. /**
  66. * @param $categoryId
  67. * @return int
  68. */
  69. public function getCategoryGoodsBasicNum($categoryId)
  70. {
  71. $result = $this->cache->zscore($this->categoryKey.'::'.$this->onlineEnterpriseId, $categoryId);
  72. return $result;
  73. }
  74. /**********************************************brand start*****************************************************************/
  75. /**
  76. * @param $brandId
  77. * @param $num
  78. * @return mixed
  79. */
  80. public function cacheBrandRelationGoodsBasicNum($brandId,$num)
  81. {
  82. $result = $this->cache->zadd($this->brandKey.'::'.$this->onlineEnterpriseId, $num, $brandId);
  83. if ($result == false){
  84. return 0;
  85. }
  86. return $result;
  87. }
  88. /**
  89. * @param $brandId
  90. * @return mixed
  91. */
  92. public function brandKeyScoreIncr($brandId)
  93. {
  94. $result = $this->cache->zincrby($this->brandKey.'::'.$this->onlineEnterpriseId, 1, $brandId);
  95. return $result;
  96. }
  97. /**
  98. * @param $brandId
  99. * @return mixed
  100. */
  101. public function brandKeyScoreDecr($brandId)
  102. {
  103. $result = $this->cache->zincrby($this->brandKey.'::'.$this->onlineEnterpriseId, -1, $brandId);
  104. return $result;
  105. }
  106. /**
  107. * @param $brandId
  108. * @return mixed
  109. */
  110. public function getBrandGoodsBasicNum($brandId)
  111. {
  112. $result = $this->cache->zscore($this->brandKey.'::'.$this->onlineEnterpriseId, $brandId);
  113. return $result;
  114. }
  115. }