ShopCache.Class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * 企业和商铺关联缓存
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/5
  7. * Time: 10:14
  8. */
  9. namespace JinDouYun\Cache;
  10. use http\Exception;
  11. use Mall\Framework\Factory;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Core\ErrorCode;
  14. class ShopCache
  15. {
  16. private $cache;
  17. protected $EnterpriseRelationShopKey = 'EnterpriseRelationShop';//企业和商铺关联缓存
  18. protected $EnterpriseSalesAreaKey = 'EnterpriseSalesArea';//企业下商铺的销售区域
  19. protected $shopToken = 'ShopToken';//商铺token
  20. public function __construct()
  21. {
  22. $this->cache = Factory::cache('default');
  23. }
  24. /**
  25. * 缓存企业和商铺关系数据
  26. * @param $enterpriseId
  27. * @param $shopData
  28. * @return ResultWrapper
  29. */
  30. public function cacheEnterpriseAndShop($enterpriseId, $shopData)
  31. {
  32. if( empty($shopData) ){
  33. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  34. }
  35. // 写入之前先清理
  36. self::cleanUserRelationEnterprise($enterpriseId);
  37. $pipe = $this->cache->multi();
  38. foreach ($shopData as $key => $value){
  39. $writeCache = $this->cache->sadd($this->EnterpriseRelationShopKey.'::'.$enterpriseId, $value['id']);
  40. }
  41. $pipe->exec();
  42. }
  43. /**
  44. * 判断当前商铺id是否在当前企业下
  45. * @param $enterpriseId
  46. * @param $shopId
  47. * @return boolean
  48. */
  49. public function isHaveShopId($enterpriseId, $shopId)
  50. {
  51. $result = $this->cache->sismember($this->EnterpriseRelationShopKey.'::'.$enterpriseId, $shopId);
  52. return $result;
  53. }
  54. /**
  55. * 清除指定企业和商铺关联数据
  56. * @param $enterpriseId
  57. */
  58. public function cleanUserRelationEnterprise($enterpriseId)
  59. {
  60. $result = $this->cache->del($this->EnterpriseRelationShopKey.'::'.$enterpriseId);
  61. }
  62. /**
  63. * 缓存企业下所有商铺的销售区域
  64. * @param $enterpriseId
  65. * @param array $salesArea
  66. */
  67. public function cacheEnterpriseSalesArea($enterpriseId,$salesArea=[]) {
  68. $this->cache->hset($this->EnterpriseSalesAreaKey, $enterpriseId, json_encode(array_unique($salesArea)));
  69. }
  70. /**
  71. * 获取企业下的销售区域
  72. * @param $enterpriseId
  73. * @return mixed
  74. */
  75. public function getEnterpriseSalesArea($enterpriseId) {
  76. return $this->cache->hget($this->EnterpriseSalesAreaKey, $enterpriseId);
  77. }
  78. /**
  79. * 缓存商铺token
  80. * @param $enterpriseId
  81. * @param $shopId
  82. * @param $token
  83. * @return bool
  84. */
  85. public function setShopToken($enterpriseId, $shopId, $token)
  86. {
  87. return $this->cache->zadd($this->shopToken.'::'.$enterpriseId, $shopId, $token);
  88. }
  89. /**
  90. * 根据token获取商铺id 反解token需要
  91. * @param $enterpriseId
  92. * @param $token
  93. * @return float
  94. */
  95. public function getShopToken($enterpriseId, $token)
  96. {
  97. return $this->cache->zscore($this->shopToken.'::'.$enterpriseId, $token);
  98. }
  99. }