feat : sts
This commit is contained in:
@@ -56,6 +56,16 @@ class ApiRedisKey
|
||||
return 'good:list:stock:cycle_id:'.$cycleId.':kitchen_id:'.$kitchenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $cycleId
|
||||
* @param int $kitchenId
|
||||
* @return string
|
||||
*/
|
||||
public static function purchaseGoodListKey(int $cycleId, int $kitchenId): string
|
||||
{
|
||||
return 'good:list:purchase:cycle_id:'.$cycleId.':kitchen_id:'.$kitchenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return string
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Cache\Redis\RedisCache;
|
||||
use App\Constants\Common\GoodCode;
|
||||
use App\Extend\DateUtil;
|
||||
use App\Model\AdminUser;
|
||||
use App\Model\Purchase;
|
||||
use App\Model\Sku;
|
||||
use App\Model\Spu;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
@@ -35,6 +36,12 @@ class GoodCache
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
/**
|
||||
* @var Purchase
|
||||
*/
|
||||
#[Inject]
|
||||
protected Purchase $purchaseModel;
|
||||
|
||||
/**
|
||||
* @var int $cycleId
|
||||
*/
|
||||
@@ -61,6 +68,11 @@ class GoodCache
|
||||
*/
|
||||
protected string $mealKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $purchaseKey;
|
||||
|
||||
/**
|
||||
* @var array $stockArr
|
||||
*/
|
||||
@@ -116,6 +128,20 @@ class GoodCache
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function initPurchaseCacheByCycleId(): void
|
||||
{
|
||||
$this->purchaseKey = ApiRedisKey::purchaseGoodListKey($this->cycleId,$this->kitchenId);
|
||||
|
||||
if ($this->checkPurchaseGoodCache()) return;
|
||||
|
||||
$this->setPurchaseGoodCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
@@ -190,6 +216,30 @@ class GoodCache
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function setPurchaseGoodCache(): void
|
||||
{
|
||||
$list = $this->purchaseModel->getListByCycleIdAndKitchenId($this->cycleId, $this->kitchenId);
|
||||
|
||||
if ($list->isEmpty()) return;
|
||||
|
||||
$list = $list->toArray();
|
||||
|
||||
$imageIds = array_column($list,'image_id');
|
||||
$imageList = $this->getOssObjects($imageIds);
|
||||
|
||||
foreach ($list as &$item) {
|
||||
$item['url'] = $imageList[$sku['image_id']]['url'] ?? '';
|
||||
}
|
||||
|
||||
$this->redis->set($this->purchaseKey, json_encode($list));
|
||||
$this->redis->expire($this->purchaseKey,$this->expireTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
@@ -223,6 +273,20 @@ class GoodCache
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function checkPurchaseGoodCache(): bool
|
||||
{
|
||||
if ($this->redis->exists($this->purchaseKey)) return true;
|
||||
|
||||
$this->redis->delete($this->purchaseKey);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ContainerExceptionInterface
|
||||
@@ -257,6 +321,24 @@ class GoodCache
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getPurchaseGoodList(): mixed
|
||||
{
|
||||
$this->initPurchaseCacheByCycleId();
|
||||
|
||||
$data = $this->redis->get($this->purchaseKey);
|
||||
|
||||
if (empty($data)) return [];
|
||||
|
||||
$data = json_decode($data,true);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
@@ -319,4 +401,18 @@ class GoodCache
|
||||
|
||||
$this->redis->eval($script, $keyArr,count($keyArr));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $kitchenId
|
||||
* @param int $cycleId
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function delPurchaseCache(int $kitchenId,int $cycleId): void
|
||||
{
|
||||
$key = ApiRedisKey::purchaseGoodListKey($cycleId,$kitchenId);
|
||||
|
||||
$this->redis->delete($key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user