| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | <?php/** * GmCache.php UTF-8 * 游戏币缓存 * * @date    : 2018/6/5 13:35 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : wuyonghong <wyh@huosdk.com> * @version : HUOSDK 8.0 */namespace huo\controller\wallet;use huo\controller\common\Base;use huo\model\finance\GmMemModel;use think\Cache;class GmCache extends Base {    public static function ins() {        return new static();    }    /**     * 获取玩家游戏币KEY     *     * @param string $mem_id     *     * @param        $app_id     *     * @return string     */    public function getGmKey($mem_id, $app_id) {        return 'gm_key_'.$mem_id.'_'.$app_id;    }    /**     * 获取游戏币余额     *     * @param int $mem_id     * @param int $app_id     *     * @return float|false     */    public function getRemainByMemGame($mem_id, $app_id) {        $_gm_data = $this->getInfoByMemGame($mem_id, $app_id);        if (empty($_gm_data)) {            return false;        }        return $_gm_data['remain'];    }    /**     * 获取账户游戏币信息     *     * @param int $mem_id     * @param int $app_id     *     * @return array|bool|mixed     */    public function getInfoByMemGame($mem_id, $app_id) {        $_key = $this->getGmKey($mem_id, $app_id);        $_gm_data_json = Cache::get($_key);        $_gm_data = json_decode($_gm_data_json, true);        if (!is_array($_gm_data)) {            $_gm_data = $_gm_data_json;        }        if (!is_array($_gm_data) || empty($_gm_data)) {            $_gm_data = (new GmMemModel())->getInfoByMemGame($mem_id, $app_id);            if (empty($_gm_data)) {                return false;            }            $this->saveGmCache($mem_id, $app_id, $_gm_data);        }        return $_gm_data;    }    /**     * 保存玩家游戏币数据     *     * @param int   $mem_id     * @param int   $app_id     * @param array $gm_data     * @param int   $ttl     */    public function saveGmCache($mem_id, $app_id, $gm_data, $ttl = 3600) {        $_key = $this->getGmKey($mem_id, $app_id);        Cache::set($_key, json_encode($gm_data), $ttl);    }    /**     * 更新帐号信息     *     * @param int   $mem_id     * @param int   $app_id     * @param array $gm_data     *     * @return bool     */    public function updateGm($mem_id, $app_id, $gm_data) {        $_key = $this->getGmKey($mem_id, $app_id);        Cache::rm($_key);        return (new GmMemModel())->updateDataGm($mem_id, $app_id, $gm_data);    }}
 |