Commit 09537566 by fsn

优化缓存支持

parent bb2749fd
package com.stylefeng.guns.common.constant;
/**
* 缓存名称
*
* @author fengshuonan
* @date 2017-04-24 21:56
*/
public interface Cache {
String CONSTANT = "CONSTANT";
}
package com.stylefeng.guns.common.constant.factory;
import com.stylefeng.guns.common.constant.Cache;
import com.stylefeng.guns.common.constant.state.ManagerStatus;
import com.stylefeng.guns.common.constant.state.MenuStatus;
import com.stylefeng.guns.common.constant.state.Sex;
......@@ -12,6 +13,8 @@ import com.stylefeng.guns.persistence.dao.RoleMapper;
import com.stylefeng.guns.persistence.model.Dept;
import com.stylefeng.guns.persistence.model.Role;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
/**
* 常量的生产工厂
......@@ -19,17 +22,22 @@ import org.springframework.cache.annotation.Cacheable;
* @author fengshuonan
* @date 2017年2月13日 下午10:55:21
*/
@Cacheable("CONSTANT")
@Component
@DependsOn("springContextHolder")
public class ConstantFactory {
private static RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
private static DeptMapper deptMapper = SpringContextHolder.getBean(DeptMapper.class);
private RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
private DeptMapper deptMapper = SpringContextHolder.getBean(DeptMapper.class);
public static ConstantFactory me(){
return SpringContextHolder.getBean("constantFactory");
}
/**
* 通过角色ids获取角色名称
*/
public static String getRoleName(String roleIds) {
@Cacheable(Cache.CONSTANT)
public String getRoleName(String roleIds) {
Integer[] roles = Convert.toIntArray(roleIds);
StringBuilder sb = new StringBuilder();
for (int role : roles) {
......@@ -44,7 +52,8 @@ public class ConstantFactory {
/**
* 通过角色id获取角色名称
*/
public static String getSingleRoleName(Integer roleId) {
@Cacheable(Cache.CONSTANT)
public String getSingleRoleName(Integer roleId) {
if (0 == roleId) {
return "--";
}
......@@ -58,7 +67,8 @@ public class ConstantFactory {
/**
* 通过角色id获取角色英文名称
*/
public static String getSingleRoleTip(Integer roleId) {
@Cacheable(Cache.CONSTANT)
public String getSingleRoleTip(Integer roleId) {
if (0 == roleId) {
return "--";
}
......@@ -72,7 +82,8 @@ public class ConstantFactory {
/**
* 获取部门名称
*/
public static String getDeptName(Integer deptId) {
@Cacheable(Cache.CONSTANT)
public String getDeptName(Integer deptId) {
Dept dept = deptMapper.selectById(deptId);
if (ToolUtil.isNotEmpty(dept) && ToolUtil.isNotEmpty(dept.getFullname())) {
return dept.getFullname();
......@@ -83,21 +94,21 @@ public class ConstantFactory {
/**
* 获取性别名称
*/
public static String getSexName(Integer sex) {
public String getSexName(Integer sex) {
return Sex.valueOf(sex);
}
/**
* 获取用户登录状态
*/
public static String getStatusName(Integer status) {
public String getStatusName(Integer status) {
return ManagerStatus.valueOf(status);
}
/**
* 获取菜单状态
*/
public static String getMenuStatusName(Integer status) {
public String getMenuStatusName(Integer status) {
return MenuStatus.valueOf(status);
}
......
......@@ -57,7 +57,7 @@ public class ShiroFactroy implements IShiro {
shiroUser.setId(user.getId()); // 账号id
shiroUser.setAccount(user.getAccount());// 账号
shiroUser.setDeptId(user.getDeptid()); // 部门id
shiroUser.setDeptName(ConstantFactory.getDeptName(user.getDeptid()));// 部门名称
shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));// 部门名称
shiroUser.setName(user.getName()); // 用户名称
Integer[] roleArray = Convert.toIntArray(user.getRoleid());// 角色集合
......@@ -65,7 +65,7 @@ public class ShiroFactroy implements IShiro {
List<String> roleNameList = new ArrayList<String>();
for (int roleId : roleArray) {
roleList.add(roleId);
roleNameList.add(ConstantFactory.getSingleRoleName(roleId));
roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
}
shiroUser.setRoleList(roleList);
shiroUser.setRoleNames(roleNameList);
......@@ -81,7 +81,7 @@ public class ShiroFactroy implements IShiro {
@Override
public String findRoleNameByRoleId(Integer roleId) {
return ConstantFactory.getSingleRoleTip(roleId);
return ConstantFactory.me().getSingleRoleTip(roleId);
}
@Override
......
......@@ -82,8 +82,8 @@ public class RoleController extends BaseController {
}
Role role = this.roleMapper.selectById(roleId);
model.addAttribute(role);
model.addAttribute("pName", ConstantFactory.getSingleRoleName(role.getPid()));
model.addAttribute("deptName", ConstantFactory.getDeptName(role.getDeptid()));
model.addAttribute("pName", ConstantFactory.me().getSingleRoleName(role.getPid()));
model.addAttribute("deptName", ConstantFactory.me().getDeptName(role.getDeptid()));
LogObjectHolder.me().set(role);
return PREFIX + "/role_edit.html";
}
......@@ -97,7 +97,7 @@ public class RoleController extends BaseController {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
}
model.addAttribute("roleId", roleId);
model.addAttribute("roleName", ConstantFactory.getSingleRoleName(roleId));
model.addAttribute("roleName", ConstantFactory.me().getSingleRoleName(roleId));
return PREFIX + "/role_assign.html";
}
......
......@@ -92,8 +92,8 @@ public class UserMgrController extends BaseController {
}
User user = this.userMapper.selectById(userId);
model.addAttribute(user);
model.addAttribute("roleName", ConstantFactory.getRoleName(user.getRoleid()));
model.addAttribute("deptName", ConstantFactory.getDeptName(user.getDeptid()));
model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid()));
LogObjectHolder.me().set(user);
return PREFIX + "user_edit.html";
}
......@@ -108,8 +108,8 @@ public class UserMgrController extends BaseController {
}
User user = this.userMapper.selectById(userId);
model.addAttribute(user);
model.addAttribute("roleName", ConstantFactory.getRoleName(user.getRoleid()));
model.addAttribute("deptName", ConstantFactory.getDeptName(user.getDeptid()));
model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid()));
return PREFIX + "user_view.html";
}
......
package com.stylefeng.guns.modular.system.dao;
import com.stylefeng.guns.common.constant.Cache;
import com.stylefeng.guns.persistence.model.Dict;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
......@@ -20,5 +22,6 @@ public interface DictDao {
* @return
* @date 2017年2月13日 下午11:11:28
*/
@Cacheable(Cache.CONSTANT)
public List<Dict> selectByCode(@Param("code") String code);
}
......@@ -6,6 +6,7 @@ import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.stylefeng.guns.persistence.model.User;
import org.springframework.cache.annotation.Cacheable;
/**
* 管理员的dao
......
......@@ -20,7 +20,7 @@ public class MenuWarpper extends BaseControllerWarpper {
@Override
public void warpTheMap(Map<String, Object> map) {
map.put("statusName", ConstantFactory.getMenuStatusName((Integer) map.get("status")));
map.put("statusName", ConstantFactory.me().getMenuStatusName((Integer) map.get("status")));
}
}
......@@ -20,8 +20,8 @@ public class RoleWarpper extends BaseControllerWarpper {
@Override
public void warpTheMap(Map<String, Object> map) {
map.put("pName", ConstantFactory.getSingleRoleName((Integer) map.get("pid")));
map.put("deptName", ConstantFactory.getDeptName((Integer) map.get("deptid")));
map.put("pName", ConstantFactory.me().getSingleRoleName((Integer) map.get("pid")));
map.put("deptName", ConstantFactory.me().getDeptName((Integer) map.get("deptid")));
}
}
......@@ -20,10 +20,10 @@ public class UserWarpper extends BaseControllerWarpper {
@Override
public void warpTheMap(Map<String, Object> map) {
map.put("sexName", ConstantFactory.getSexName((Integer) map.get("sex")));
map.put("roleName", ConstantFactory.getRoleName((String) map.get("roleid")));
map.put("deptName", ConstantFactory.getDeptName((Integer) map.get("deptid")));
map.put("statusName", ConstantFactory.getStatusName((Integer) map.get("status")));
map.put("sexName", ConstantFactory.me().getSexName((Integer) map.get("sex")));
map.put("roleName", ConstantFactory.me().getRoleName((String) map.get("roleid")));
map.put("deptName", ConstantFactory.me().getDeptName((Integer) map.get("deptid")));
map.put("statusName", ConstantFactory.me().getStatusName((Integer) map.get("status")));
}
}
......@@ -48,7 +48,7 @@ public class DataSourceConfig {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
Resource[] classPathResources = ResKit.getClassPathResources("classpath:com/stylefeng/guns/**/mapping/*.xml");
Resource[] classPathResources = ResKit.getClassPathResources("classpath*:com/stylefeng/guns/**/mapping/*.xml");
sqlSessionFactory.setMapperLocations(classPathResources);
//以下为mybatis-plus配置
......
......@@ -3,18 +3,10 @@ package com.stylefeng.guns.cache;
import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.base.BaseTest;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.core.support.cache.CacheKit;
import com.stylefeng.guns.modular.system.dao.DeptDao;
import com.stylefeng.guns.modular.system.dao.DictDao;
import com.stylefeng.guns.modular.system.dao.UserMgrDao;
import com.stylefeng.guns.persistence.model.Dict;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* 缓存测试
......@@ -24,9 +16,6 @@ import java.util.Map;
*/
public class CacheTest extends BaseTest{
@Autowired
UserMgrDao userMgrDao;
/**
* 测试没有缓存的情况
*
......@@ -36,14 +25,13 @@ public class CacheTest extends BaseTest{
@Test
public void testNoCache(){
long beginTIme = System.currentTimeMillis();
for(int i = 1;i<1000;i++){
List<Map<String, Object>> maps = userMgrDao.selectUsers(null, null, null);
for(int i = 1;i<2000000;i++){
String singleRoleName = ConstantFactory.me().getSingleRoleName(1);
}
System.out.println(System.currentTimeMillis() - beginTIme);
Object constant = CacheKit.get("CONSTANT", 1);
Object constant = CacheKit.get("CONSTANT", "101");
System.out.println(constant);
List constant1 = CacheKit.getKeys("CONSTANT");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment