123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- 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();
- }
-
- private function getGiftKey($gift_id) {
- return 'gift_id_key_'.$gift_id;
- }
-
- 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;
- }
-
- public function saveGiftCache($gift_id, $_gift_data, $ttl = 3600) {
- $_key = $this->getGiftKey($gift_id);
- Cache::set($_key, json_encode($_gift_data), $ttl);
- }
-
- 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;
- }
-
- public function deleteGiftCache($gift_id) {
- $_key = $this->getGiftKey($gift_id);
- Cache::rm($_key);
- }
-
- private function getGameGiftKey($game_id) {
- return 'game_gift_key_'.$game_id;
- }
-
- public function getGiftCntByGameId($game_id) {
- if (empty($game_id)) {
- return 0;
- }
- $_game_gift_data = $this->getGameGiftByGameId($game_id);
- return count($_game_gift_data['list']);
- }
-
- 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;
- }
-
- public function saveGameGiftCache($game_id, $game_gift_data, $ttl = 3600) {
- $_key = $this->getGameGiftKey($game_id);
- Cache::set($_key, json_encode($game_gift_data), $ttl);
- }
-
- public function updateGameGift($game_id) {
- $_key = $this->getGameGiftKey($game_id);
- Cache::rm($_key);
- }
- }
|