* @version : HUOSDK 8.0 */ namespace reyun\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use think\Cache; class GameReyunModel extends CommonModel { protected $name = 'game_reyun'; protected $cache_key_prefix = CacheConst::CACHE_GAME_REYUN_PREFIX; /** * 获取单条记录缓存key * * @param int $app_id 游戏ID * * @return string */ protected function getSingleCacheKey($app_id) { return $this->cache_key_prefix.$app_id; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { if (empty($data)) { return false; } $_data = $data; $_model = new static(); $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []); if (false !== $_rs) { return $_model->getLastInsID(); } return false; } /** * 通过ID获取信息 * * @param int $app_id 主键ID * * @return array|false */ public function getInfoByAppId($app_id) { /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($app_id); $_data = Cache::get($_single_cache_key); if (!empty($_data)) { return $_data; } $_map['app_id'] = $app_id; $_info = $this->where($_map)->find(); if (false === $_info) { return []; } if (is_object($_info)) { $_info = $_info->toArray(); } if (empty($_info)) { return []; } Cache::set($_single_cache_key, $_info); return $_info; } /** * 更新单条数据 * * @param array $data 数据 * @param int $app_id 游戏ID * * @return bool */ public function updateDataByAppId($data, $app_id) { $_data = $data; $_map['app_id'] = $app_id; $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($app_id); Cache::rm($_single_cache_key); /* TAG缓存操作 */ return true; } }