/** * 根据ID获取用户 * * @param id * @return 取不到返回null */ public static User get(String id) { User user = (User) CacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id); if (user == null) { user = userDao.selectOne(id); if (user == null) { return null; } user.setRoleList(roleDao.findList(new Role(user))); CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user); CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user); } return user; }
/** * 根据登录名获取用户 * * @param loginName * @return 取不到返回null */ public static User getByLoginName(String loginName) { User user = (User) CacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName); if (user == null) { User queryUser = new User(); queryUser.setLoginName(loginName); ; user = userDao.getByLoginName(queryUser); if (user == null) { return null; } user.setRoleList(roleDao.findList(new Role(user))); CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user); CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user); } return user; }
/** * 获取当前用户授权菜单 * * @return */ public static List<Menu> getMenuList() { @SuppressWarnings("unchecked") List<Menu> menuList = (List<Menu>) getCache(CACHE_MENU_LIST); if (menuList == null) { User user = getUser(); if (user.isAdmin()) { menuList = menuDao.findAllList(); } else { Menu m = new Menu(); m.setUserId(user.getId()); menuList = menuDao.findByUserId(m); } putCache(CACHE_MENU_LIST, menuList); } return menuList; }
/** * 清除指定用户缓存 * * @param user */ public static void clearCache(User user) { CacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId()); CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName()); CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getOldLoginName()); }