* @version : HUOSDK 8.0 */ namespace huo\controller\game; use huo\controller\common\Base; use huo\model\game\CategoryModel; use huolib\constant\CacheConst; use huolib\constant\GameConst; use huolib\tool\StrUtils; use think\Cache; class CategoryCache extends Base { public static function ins() { return new static(); } /** * 获取标签类型KEY * * @param int $type 1 标签 2 类型 * * @return string */ private function getCategoryKey($type) { if (GameConst::CATEGORY_TYPE_TAG == $type) { return CacheConst::KEY_CACHE_GAME_TAG; //游戏标签key } else { return CacheConst::KEY_CACHE_GAME_CATEGORY; //游戏类型 } } /** * 获取所有类型 * * @param int $type 1 标签 2 类型 * * @return array|bool|mixed */ public function getIdNames($type) { $_key = $this->getCategoryKey($type); $_cate_data_json = Cache::get($_key); $_cate_data = json_decode($_cate_data_json, true); if (!is_array($_cate_data)) { $_cate_data = $_cate_data_json; } if (!is_array($_cate_data) || empty($_cate_data)) { $_cate_data = (new CategoryModel())->getIdNames($type); if (empty($_cate_data)) { return false; } $this->saveCategoryCache($type, $_cate_data); } return $_cate_data; } /** * 保存玩家cache 数据 * * @param int $type 1 标签 2 类型 * @param array $cate_data * @param int $ttl */ public function saveCategoryCache($type, $cate_data, $ttl = 3600) { $_key = $this->getCategoryKey($type); Cache::set($_key, json_encode($cate_data), $ttl); } /** * 更新游戏类型 * * @param int $type 1 标签 2 类型 4 首页标记 * @param array $cate_data * * @return bool */ public function updateCategory($type, $cate_data) { $_key = $this->getCategoryKey($type); Cache::rm($_key); return (new CategoryModel())->updateCategory($cate_data, $cate_data['id']); } /** * 添加游戏类型 * * @param $type * @param $cate_data * * @return mixed */ public function addCategory($type, $cate_data) { $_key = $this->getCategoryKey($type); Cache::rm($_key); return (new CategoryModel())->addCategory($cate_data); } /** * 根据cate_id 获取对应名称 * * @param int $type * @param string $ids * * @return array */ public function getNameByIds($type = 2, $ids) { $_ids = $ids; if (is_string($_ids)) { $_ids_arr = StrUtils::strToArr($ids); } if (empty($_ids_arr)) { return []; } $_cates = $this->getIdNames($type); $_name_arr = []; foreach ($_ids_arr as $_k => $_v) { $_cate_id = intval($_v); if (empty($_cates[$_cate_id])) { continue; } $_name_arr[$_k] = $_cates[$_cate_id]; } return array_values($_name_arr); } /** * 删除类型 * * @param $id * * @return bool */ public function deleteCategory($id) { $_data = (new CategoryModel())->getInfoById($id); if (empty($_data)) { return false; } $_rs = (new CategoryModel())->deleteCategory($id); $_key = $this->getCategoryKey($_data['type']); Cache::rm($_key); return $_rs; } /** * 判断数组中值是否为类型|标签|首页标签,返回相应的数组 * * @param int $type 1 标签 2 类型 4 首页标记 * @param array $array * * @return array */ public function returnTypeArray($type, $array = []) { if (empty($array)) { return $array; } $_cate_arr = $this->getIdNames($type); foreach ($array as $_k => $_v) { if (empty($_cate_arr[$_v])) { //不存在表示非相应类型 unset($array[$_k]); } } return $array; } }