* @version : HUOSDK 8.0 */ namespace huo\controller\gift; use huo\controller\common\Base; use huo\logic\game\GiftLogic; use huo\model\game\GiftModel; use think\Cache; class GiftCache extends Base { public static function ins() { return new static(); } /** * 获取礼包KEY * * @param string $gift_id * * @return string */ private function getGiftKey($gift_id) { return 'gift_id_key_'.$gift_id; } /** * 获取礼包信息 * * @param string $gift_id * * @return array|bool|mixed */ public function getInfoByGiftId($gift_id) { $_key = $this->getGiftKey($gift_id); $_gift_data_json = Cache::get($_key); $_gift_data = json_decode($_gift_data_json, true); if (!is_array($_gift_data)) { $_gift_data = $_gift_data_json; } if (!is_array($_gift_data) || empty($_gift_data)) { $_gift_data = (new GiftLogic())->getInfoByGiftId($gift_id); if (empty($_gift_data)) { return false; } $this->saveGiftCache($gift_id, $_gift_data); } return $_gift_data; } /** * 保存礼包cache 数据 * * @param $gift_id * @param $_gift_data * @param int $ttl */ public function saveGiftCache($gift_id, $_gift_data, $ttl = 3600) { $_key = $this->getGiftKey($gift_id); Cache::set($_key, json_encode($_gift_data), $ttl); } /** * 更新礼包信息 * * @param string $gift_id * @param array $gift_data * * @return bool * @throws \think\Exception */ public function updateGift($gift_id, $gift_data) { $this->deleteGiftCache($gift_id); $_rs = (new GiftModel())->updateGift($gift_data, $gift_id); if (true == $_rs) { (new GiftLogic())->checkGameGift($gift_id); $this->updateGameGift($gift_id); } return $_rs; } /** * 删除礼包cache 数据 * * @param string $gift_id * * @return void * @author chenbingling * @date 2018/5/9 14:15 */ public function deleteGiftCache($gift_id) { $_key = $this->getGiftKey($gift_id); Cache::rm($_key); } /** * 获取游戏礼包KEY * * @param string $game_id * * @return string */ private function getGameGiftKey($game_id) { return 'game_gift_key_'.$game_id; } /** * 通过游戏ID获取礼包数量 * * @param $game_id * * @return int */ public function getGiftCntByGameId($game_id) { if (empty($game_id)) { return 0; } $_game_gift_data = $this->getGameGiftByGameId($game_id); return count($_game_gift_data['list']); } /** * 获取游戏礼包信息 * * @param string $game_id * * @return array|bool|mixed */ public function getGameGiftByGameId($game_id) { $_key = $this->getGameGiftKey($game_id); $_gg_data_json = Cache::get($_key); $_gg_data = json_decode($_gg_data_json, true); if (!is_array($_gg_data)) { $_gg_data = $_gg_data_json; } if (!is_array($_gg_data) || empty($_gg_data)) { $_gg_data = (new GiftLogic())->getGameGiftByGameId($game_id); if (is_numeric($_gg_data)) { return false; } $this->saveGameGiftCache($game_id, $_gg_data); } return $_gg_data; } /** * 保存礼包cache 数据 * * @param string $game_id * @param array $game_gift_data * @param int $ttl */ public function saveGameGiftCache($game_id, $game_gift_data, $ttl = 3600) { $_key = $this->getGameGiftKey($game_id); Cache::set($_key, json_encode($game_gift_data), $ttl); } /** * 更新礼包信息 * * @param string $game_id */ public function updateGameGift($game_id) { $_key = $this->getGameGiftKey($game_id); Cache::rm($_key); } }