123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /**
- * GiftCache.php UTF-8
- *
- *
- * @date : 2018/5/3 15:46
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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 <cbl@huosdk.com>
- * @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);
- }
- }
|