123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- /**
- * AgentCache.php UTF-8
- * 渠道缓存
- *
- * @date : 2018/4/26 14:45
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\agent;
- use huo\controller\common\Base;
- use huo\logic\agent\AgentLogic;
- use huo\model\agent\AgentGameModel;
- use huo\model\user\AgentExtModel;
- use think\Cache;
- class AgentCache extends Base {
- /**
- * 获取实例
- *
- * @return static
- */
- public static function ins() {
- return new static();
- }
- /**
- * 获取agent key
- *
- * @param int $agent_id
- *
- * @return string
- */
- public function getAgentKey($agent_id) {
- return 'agent_key_'.$agent_id;
- }
- /**
- * 通过$agent_id获取代理信息
- *
- * @param int $agent_id
- *
- * @return array|false
- */
- public function getInfoByAgentId($agent_id) {
- $_key = $this->getAgentKey($agent_id);
- $_agent_data_json = Cache::get($_key);
- $_agent_data = json_decode($_agent_data_json, true);
- if (!is_array($_agent_data)) {
- $_agent_data = $_agent_data_json;
- }
- if (!is_array($_agent_data) || empty($_agent_data)) {
- $_agent_data = (new AgentLogic())->getInfoByAgentId($agent_id);
- if (empty($_agent_data)) {
- return false;
- }
- $this->saveAgentCache($agent_id, $_agent_data);
- }
- return $_agent_data;
- }
- /**
- * 保存玩家cache 数据
- *
- * @param int $agent_id
- * @param array $agent_data
- * @param int $ttl
- *
- * @return bool
- */
- public function saveAgentCache($agent_id, $agent_data, $ttl = 3600) {
- $_key = $this->getAgentKey($agent_id);
- return Cache::set($_key, json_encode($agent_data), $ttl);
- }
- /**
- * 更新渠道帐号信息
- *
- * @param string $agent_id
- * @param array $agent_data
- *
- * @return bool
- */
- public function updateAgent($agent_id, $agent_data) {
- $_key = $this->getAgentKey($agent_id);
- Cache::rm($_key);
- return (new AgentLogic())->updateAgent($agent_data, $agent_id);
- }
- /**
- * 通过Agent_id 获取 玩家ID
- *
- * @param int $agent_id 渠道ID
- *
- * @return int
- */
- public function getMemIdByAgentId($agent_id) {
- $_agent_info = $this->getInfoByAgentId($agent_id);
- if (empty($_agent_info)) {
- return 0;
- }
- return $_agent_info['mem_id'];
- }
- /**
- * 获取agent key
- *
- * @param int $agent_id
- *
- * @return string
- */
- public function getAgentExtKey($agent_id) {
- return 'agent_ext_key_'.$agent_id;
- }
- /**
- * 通过agent_id获取代理扩展信息
- *
- * @param int $agent_id
- *
- * @return array|false
- */
- public function getAgentExtByAgentId($agent_id) {
- $_key = $this->getAgentExtKey($agent_id);
- Cache::rm($_key);
- $_ae_data_json = Cache::get($_key);
- $_ae_data = json_decode($_ae_data_json, true);
- if (!is_array($_ae_data)) {
- $_ae_data = $_ae_data_json;
- }
- if (!is_array($_ae_data) || empty($_ae_data)) {
- $_ae_data = (new AgentExtModel())->getAgentExtByAgentId($agent_id);
- if (empty($_ae_data)) {
- return false;
- }
- $this->saveAgentExtCache($agent_id, $_ae_data);
- }
- return $_ae_data;
- }
- /**
- * 保存渠道扩展数据
- *
- * @param int $agent_id
- * @param array $ae_data
- * @param int $ttl
- *
- * @return bool
- */
- public function saveAgentExtCache($agent_id, $ae_data, $ttl = 3600) {
- $_key = $this->getAgentExtKey($agent_id);
- return Cache::set($_key, json_encode($ae_data), $ttl);
- }
- /**
- * 更新渠道帐号信息
- *
- * @param string $agent_id
- * @param array $ae_data
- *
- * @param bool $rm_cache 是否删除缓存
- *
- * @return bool
- */
- public function updateAgentExt($agent_id, $ae_data, $rm_cache = false) {
- if (true == $rm_cache) {
- $_key = $this->getAgentExtKey($agent_id);
- Cache::rm($_key);
- $_ae_data = $ae_data;
- } else {
- $_old_data = $this->getAgentExtByAgentId($agent_id);
- $_ae_data = array_merge($_old_data, $ae_data);
- $this->saveAgentExtCache($agent_id, $_ae_data);
- }
- return (new AgentExtModel())->updateData($_ae_data, $agent_id);
- }
- /**
- * 获取agentgame key
- *
- * @param int $ag_id
- *
- * @return string
- */
- public function getAgKey($ag_id) {
- return 'ag_key_'.$ag_id;
- }
- /**
- * 通过$agent_game获取代理ID
- *
- *
- * @param string $agent_game
- *
- * @return string
- */
- public function getAgentIdByAg($agent_game) {
- $_agent_id = (new AgentGameModel())->getAgentIdByAg($agent_game);
- if (empty($_agent_id)) {
- return 0;
- }
- return $_agent_id;
- }
- /**
- * @param $ag_id
- *
- * @return mixed
- */
- public function getAgentIdByAgId($ag_id) {
- $_ag_info = $this->getAgInfoByAgId($ag_id);
- return $_ag_info['agent_id'];
- }
- /**
- * @param $ag_id
- *
- * @return mixed
- */
- public function getAppIdByAgId($ag_id) {
- $_ag_info = $this->getAgInfoByAgId($ag_id);
- return $_ag_info['app_id'];
- }
- /**
- * 获取渠道游戏信息
- *
- * @param $ag_id
- *
- * @return bool|mixed
- */
- public function getAgInfoByAgId($ag_id) {
- $_key = $this->getAgKey($ag_id);
- $_ag_data_json = Cache::get($_key);
- $_ag_data = json_decode($_ag_data_json, true);
- if (!is_array($_ag_data)) {
- $_ag_data = $_ag_data_json;
- }
- if (empty($_ag_data) || !isset($_ag_data['agent_id'])) {
- $_ag_data = (new AgentGameModel())->getAgInfoByAgId($ag_id);
- if (empty($_ag_data)) {
- return false;
- }
- $this->saveAgCache($ag_id, $_ag_data);
- }
- return $_ag_data;
- }
- /**
- * 保存玩家cache 数据
- *
- * @param INT $ag_id
- * @param array $ag_data
- * @param int $ttl
- *
- * @return bool
- */
- public function saveAgCache($ag_id, $ag_data, $ttl = 3600) {
- $_key = $this->getAgKey($ag_id);
- return Cache::set($_key, json_encode($ag_data), $ttl);
- }
- /**
- * 更新游戏渠道信息
- *
- * @param int $ag_id
- * @param array $ag_data
- *
- * @param bool $rm_cache 是否删除缓存
- *
- * @return bool
- */
- public function updateAg($ag_id, $ag_data, $rm_cache = false) {
- if (true == $rm_cache) {
- $_key = $this->getAgKey($ag_id);
- Cache::rm($_key);
- $_ag_data = $ag_data;
- } else {
- $_old_data = $this->getAgInfoByAgId($ag_id);
- $_ag_data = array_merge($_old_data, $ag_data);
- $this->saveAgCache($ag_id, $_ag_data);
- }
- return (new AgentGameModel())->updateData($_ag_data, $ag_id);
- }
- }
|