123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * 企业和商铺关联缓存
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/11/5
- * Time: 10:14
- */
- namespace JinDouYun\Cache;
- use http\Exception;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- class ShopCache
- {
- private $cache;
- protected $EnterpriseRelationShopKey = 'EnterpriseRelationShop';//企业和商铺关联缓存
- protected $EnterpriseSalesAreaKey = 'EnterpriseSalesArea';//企业下商铺的销售区域
- protected $shopToken = 'ShopToken';//商铺token
- public function __construct()
- {
- $this->cache = Factory::cache('default');
- }
- /**
- * 缓存企业和商铺关系数据
- * @param $enterpriseId
- * @param $shopData
- * @return ResultWrapper
- */
- public function cacheEnterpriseAndShop($enterpriseId, $shopData)
- {
- if( empty($shopData) ){
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- // 写入之前先清理
- self::cleanUserRelationEnterprise($enterpriseId);
- $pipe = $this->cache->multi();
- foreach ($shopData as $key => $value){
- $writeCache = $this->cache->sadd($this->EnterpriseRelationShopKey.'::'.$enterpriseId, $value['id']);
- }
- $pipe->exec();
- }
- /**
- * 判断当前商铺id是否在当前企业下
- * @param $enterpriseId
- * @param $shopId
- * @return boolean
- */
- public function isHaveShopId($enterpriseId, $shopId)
- {
- $result = $this->cache->sismember($this->EnterpriseRelationShopKey.'::'.$enterpriseId, $shopId);
- return $result;
- }
- /**
- * 清除指定企业和商铺关联数据
- * @param $enterpriseId
- */
- public function cleanUserRelationEnterprise($enterpriseId)
- {
- $result = $this->cache->del($this->EnterpriseRelationShopKey.'::'.$enterpriseId);
- }
- /**
- * 缓存企业下所有商铺的销售区域
- * @param $enterpriseId
- * @param array $salesArea
- */
- public function cacheEnterpriseSalesArea($enterpriseId,$salesArea=[]) {
- $this->cache->hset($this->EnterpriseSalesAreaKey, $enterpriseId, json_encode(array_unique($salesArea)));
- }
- /**
- * 获取企业下的销售区域
- * @param $enterpriseId
- * @return mixed
- */
- public function getEnterpriseSalesArea($enterpriseId) {
- return $this->cache->hget($this->EnterpriseSalesAreaKey, $enterpriseId);
- }
- /**
- * 缓存商铺token
- * @param $enterpriseId
- * @param $shopId
- * @param $token
- * @return bool
- */
- public function setShopToken($enterpriseId, $shopId, $token)
- {
- return $this->cache->zadd($this->shopToken.'::'.$enterpriseId, $shopId, $token);
- }
- /**
- * 根据token获取商铺id 反解token需要
- * @param $enterpriseId
- * @param $token
- * @return float
- */
- public function getShopToken($enterpriseId, $token)
- {
- return $this->cache->zscore($this->shopToken.'::'.$enterpriseId, $token);
- }
- }
|