| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 | <?php/** * GamePaySwitchCache.php  UTF-8 * 切换规则信息缓存 * * @date    : 2018/6/7 14:09 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : chenbingling <cbl@huosdk.com> * @version : HUOSDK 8.0 */namespace huo\controller\game;use huo\controller\common\Base;use huo\logic\game\GamePaySwitchLogic;use think\Cache;class GamePaySwitchCache extends Base {    public static function ins() {        return new static();    }    /**     * 获取KEY     *     * @param string $app_id     *     * @return string     */    private function getPaySwitchKey($app_id) {        return 'switch_id_key_'.$app_id;    }    /**     * 获取信息     *     * @param string $app_id     *     * @return array|bool|mixed     */    public function getInfoByAppId($app_id) {        $_key = $this->getPaySwitchKey($app_id);        $_data_json = Cache::get($_key);        $_data = json_decode($_data_json, true);        if (!is_array($_data)) {            $_data = $_data_json;        }        if (!is_array($_data) || empty($_data)) {            $_data = (new GamePaySwitchLogic())->getInfoByAppId($app_id);            if (empty($_data)) {                return false;            }            $this->savePaySwitchCache($app_id, $_data);        }        return $_data;    }    /**     * 保存cache 数据     *     * @param     $app_id     * @param     $data     * @param int $ttl     */    public function savePaySwitchCache($app_id, $data, $ttl = 3600) {        $_key = $this->getPaySwitchKey($app_id);        Cache::set($_key, json_encode($data), $ttl);    }    /**     * 更新信息     *     * @param string $app_id     * @param array  $data     *     * @param bool   $rm_cache 是否删除缓存     *     * @return \huo\model\game\GamePaySwitchModel     */    public function updatePaySwitch($app_id, $data, $rm_cache = false) {        if (true == $rm_cache) {            $_key = $this->getPaySwitchKey($app_id);            Cache::rm($_key);        } else {            $_old_data = $this->getInfoByAppId($app_id);            if (!empty($_old_data)) {                $data = array_merge($_old_data, $data);            }            $this->savePaySwitchCache($app_id, $data);        }        return (new GamePaySwitchLogic())->updatePaySwitch($data, $app_id);    }}
 |