/** * 根据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; }
/** * 清除指定用户缓存 * * @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()); }