Commit fa0af318 authored by macro's avatar macro
Browse files

添加redis缓存支持

parent 944a305b
...@@ -14,6 +14,7 @@ import com.macro.mall.mapper.UmsAdminPermissionRelationMapper; ...@@ -14,6 +14,7 @@ import com.macro.mall.mapper.UmsAdminPermissionRelationMapper;
import com.macro.mall.mapper.UmsAdminRoleRelationMapper; import com.macro.mall.mapper.UmsAdminRoleRelationMapper;
import com.macro.mall.model.*; import com.macro.mall.model.*;
import com.macro.mall.security.util.JwtTokenUtil; import com.macro.mall.security.util.JwtTokenUtil;
import com.macro.mall.service.UmsAdminCacheService;
import com.macro.mall.service.UmsAdminService; import com.macro.mall.service.UmsAdminService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -61,14 +62,20 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -61,14 +62,20 @@ public class UmsAdminServiceImpl implements UmsAdminService {
private UmsAdminPermissionRelationDao adminPermissionRelationDao; private UmsAdminPermissionRelationDao adminPermissionRelationDao;
@Autowired @Autowired
private UmsAdminLoginLogMapper loginLogMapper; private UmsAdminLoginLogMapper loginLogMapper;
@Autowired
private UmsAdminCacheService adminCacheService;
@Override @Override
public UmsAdmin getAdminByUsername(String username) { public UmsAdmin getAdminByUsername(String username) {
UmsAdmin admin = adminCacheService.getAdmin(username);
if(admin!=null) return admin;
UmsAdminExample example = new UmsAdminExample(); UmsAdminExample example = new UmsAdminExample();
example.createCriteria().andUsernameEqualTo(username); example.createCriteria().andUsernameEqualTo(username);
List<UmsAdmin> adminList = adminMapper.selectByExample(example); List<UmsAdmin> adminList = adminMapper.selectByExample(example);
if (adminList != null && adminList.size() > 0) { if (adminList != null && adminList.size() > 0) {
return adminList.get(0); admin = adminList.get(0);
adminCacheService.setAdmin(admin);
return admin;
} }
return null; return null;
} }
...@@ -119,6 +126,7 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -119,6 +126,7 @@ public class UmsAdminServiceImpl implements UmsAdminService {
*/ */
private void insertLoginLog(String username) { private void insertLoginLog(String username) {
UmsAdmin admin = getAdminByUsername(username); UmsAdmin admin = getAdminByUsername(username);
if(admin==null) return;
UmsAdminLoginLog loginLog = new UmsAdminLoginLog(); UmsAdminLoginLog loginLog = new UmsAdminLoginLog();
loginLog.setAdminId(admin.getId()); loginLog.setAdminId(admin.getId());
loginLog.setCreateTime(new Date()); loginLog.setCreateTime(new Date());
...@@ -176,12 +184,17 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -176,12 +184,17 @@ public class UmsAdminServiceImpl implements UmsAdminService {
admin.setPassword(passwordEncoder.encode(admin.getPassword())); admin.setPassword(passwordEncoder.encode(admin.getPassword()));
} }
} }
return adminMapper.updateByPrimaryKeySelective(admin); int count = adminMapper.updateByPrimaryKeySelective(admin);
adminCacheService.delAdmin(id);
return count;
} }
@Override @Override
public int delete(Long id) { public int delete(Long id) {
return adminMapper.deleteByPrimaryKey(id); adminCacheService.delAdmin(id);
int count = adminMapper.deleteByPrimaryKey(id);
adminCacheService.delResourceList(id);
return count;
} }
@Override @Override
...@@ -202,6 +215,7 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -202,6 +215,7 @@ public class UmsAdminServiceImpl implements UmsAdminService {
} }
adminRoleRelationDao.insertList(list); adminRoleRelationDao.insertList(list);
} }
adminCacheService.delResourceList(adminId);
return count; return count;
} }
...@@ -212,7 +226,15 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -212,7 +226,15 @@ public class UmsAdminServiceImpl implements UmsAdminService {
@Override @Override
public List<UmsResource> getResourceList(Long adminId) { public List<UmsResource> getResourceList(Long adminId) {
return adminRoleRelationDao.getResourceList(adminId); List<UmsResource> resourceList = adminCacheService.getResourceList(adminId);
if(CollUtil.isNotEmpty(resourceList)){
return resourceList;
}
resourceList = adminRoleRelationDao.getResourceList(adminId);
if(CollUtil.isNotEmpty(resourceList)){
adminCacheService.setResourceList(adminId,resourceList);
}
return resourceList;
} }
@Override @Override
...@@ -276,6 +298,7 @@ public class UmsAdminServiceImpl implements UmsAdminService { ...@@ -276,6 +298,7 @@ public class UmsAdminServiceImpl implements UmsAdminService {
} }
umsAdmin.setPassword(passwordEncoder.encode(param.getNewPassword())); umsAdmin.setPassword(passwordEncoder.encode(param.getNewPassword()));
adminMapper.updateByPrimaryKey(umsAdmin); adminMapper.updateByPrimaryKey(umsAdmin);
adminCacheService.delAdmin(umsAdmin.getId());
return 1; return 1;
} }
......
...@@ -5,6 +5,7 @@ import com.github.pagehelper.PageHelper; ...@@ -5,6 +5,7 @@ import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.UmsResourceMapper; import com.macro.mall.mapper.UmsResourceMapper;
import com.macro.mall.model.UmsResource; import com.macro.mall.model.UmsResource;
import com.macro.mall.model.UmsResourceExample; import com.macro.mall.model.UmsResourceExample;
import com.macro.mall.service.UmsAdminCacheService;
import com.macro.mall.service.UmsResourceService; import com.macro.mall.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -20,6 +21,8 @@ import java.util.List; ...@@ -20,6 +21,8 @@ import java.util.List;
public class UmsResourceServiceImpl implements UmsResourceService { public class UmsResourceServiceImpl implements UmsResourceService {
@Autowired @Autowired
private UmsResourceMapper resourceMapper; private UmsResourceMapper resourceMapper;
@Autowired
private UmsAdminCacheService adminCacheService;
@Override @Override
public int create(UmsResource umsResource) { public int create(UmsResource umsResource) {
umsResource.setCreateTime(new Date()); umsResource.setCreateTime(new Date());
...@@ -29,7 +32,9 @@ public class UmsResourceServiceImpl implements UmsResourceService { ...@@ -29,7 +32,9 @@ public class UmsResourceServiceImpl implements UmsResourceService {
@Override @Override
public int update(Long id, UmsResource umsResource) { public int update(Long id, UmsResource umsResource) {
umsResource.setId(id); umsResource.setId(id);
return resourceMapper.updateByPrimaryKeySelective(umsResource); int count = resourceMapper.updateByPrimaryKeySelective(umsResource);
adminCacheService.delResourceListByResource(id);
return count;
} }
@Override @Override
...@@ -39,7 +44,9 @@ public class UmsResourceServiceImpl implements UmsResourceService { ...@@ -39,7 +44,9 @@ public class UmsResourceServiceImpl implements UmsResourceService {
@Override @Override
public int delete(Long id) { public int delete(Long id) {
return resourceMapper.deleteByPrimaryKey(id); int count = resourceMapper.deleteByPrimaryKey(id);
adminCacheService.delResourceListByResource(id);
return count;
} }
@Override @Override
......
...@@ -8,6 +8,7 @@ import com.macro.mall.mapper.UmsRoleMenuRelationMapper; ...@@ -8,6 +8,7 @@ import com.macro.mall.mapper.UmsRoleMenuRelationMapper;
import com.macro.mall.mapper.UmsRolePermissionRelationMapper; import com.macro.mall.mapper.UmsRolePermissionRelationMapper;
import com.macro.mall.mapper.UmsRoleResourceRelationMapper; import com.macro.mall.mapper.UmsRoleResourceRelationMapper;
import com.macro.mall.model.*; import com.macro.mall.model.*;
import com.macro.mall.service.UmsAdminCacheService;
import com.macro.mall.service.UmsRoleService; import com.macro.mall.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -35,6 +36,8 @@ public class UmsRoleServiceImpl implements UmsRoleService { ...@@ -35,6 +36,8 @@ public class UmsRoleServiceImpl implements UmsRoleService {
private UmsRolePermissionRelationDao rolePermissionRelationDao; private UmsRolePermissionRelationDao rolePermissionRelationDao;
@Autowired @Autowired
private UmsRoleDao roleDao; private UmsRoleDao roleDao;
@Autowired
private UmsAdminCacheService adminCacheService;
@Override @Override
public int create(UmsRole role) { public int create(UmsRole role) {
role.setCreateTime(new Date()); role.setCreateTime(new Date());
...@@ -53,7 +56,9 @@ public class UmsRoleServiceImpl implements UmsRoleService { ...@@ -53,7 +56,9 @@ public class UmsRoleServiceImpl implements UmsRoleService {
public int delete(List<Long> ids) { public int delete(List<Long> ids) {
UmsRoleExample example = new UmsRoleExample(); UmsRoleExample example = new UmsRoleExample();
example.createCriteria().andIdIn(ids); example.createCriteria().andIdIn(ids);
return roleMapper.deleteByExample(example); int count = roleMapper.deleteByExample(example);
adminCacheService.delResourceListByRoleIds(ids);
return count;
} }
@Override @Override
...@@ -137,6 +142,7 @@ public class UmsRoleServiceImpl implements UmsRoleService { ...@@ -137,6 +142,7 @@ public class UmsRoleServiceImpl implements UmsRoleService {
relation.setResourceId(resourceId); relation.setResourceId(resourceId);
roleResourceRelationMapper.insert(relation); roleResourceRelationMapper.insert(relation);
} }
adminCacheService.delResourceListByRole(roleId);
return resourceIds.size(); return resourceIds.size();
} }
} }
...@@ -11,4 +11,10 @@ spring: ...@@ -11,4 +11,10 @@ spring:
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #不统计这些请求数据 exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #不统计这些请求数据
stat-view-servlet: #访问监控网页的登录用户名和密码 stat-view-servlet: #访问监控网页的登录用户名和密码
login-username: druid login-username: druid
login-password: druid login-password: druid
\ No newline at end of file redis:
host: localhost # Redis服务器地址
database: 0 # Redis数据库索引(默认为0)
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 300ms # 连接超时时间(毫秒)
\ No newline at end of file
...@@ -12,6 +12,12 @@ spring: ...@@ -12,6 +12,12 @@ spring:
stat-view-servlet: #访问监控网页的登录用户名和密码 stat-view-servlet: #访问监控网页的登录用户名和密码
login-username: druid login-username: druid
login-password: druid login-password: druid
redis:
host: redis # Redis服务器地址
database: 0 # Redis数据库索引(默认为0)
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 300ms # 连接超时时间(毫秒)
logging: logging:
path: /var/logs #配置日志生成路径 path: /var/logs #配置日志生成路径
\ No newline at end of file
...@@ -14,9 +14,17 @@ mybatis: ...@@ -14,9 +14,17 @@ mybatis:
jwt: jwt:
tokenHeader: Authorization #JWT存储的请求头 tokenHeader: Authorization #JWT存储的请求头
secret: mall-admin-secret #JWT加解密使用的密钥 secret: mall-admin-secret #JWT加解密使用的密钥
expiration: 604800 #JWT的超期限时间(60*60*24) expiration: 604800 #JWT的超期限时间(60*60*24*7)
tokenHead: Bearer #JWT负载中拿到开头 tokenHead: Bearer #JWT负载中拿到开头
redis:
database: mall
key:
admin: 'ums:admin'
resourceList: 'ums:resourceList'
expire:
common: 86400 # 24小时
secure: secure:
ignored: ignored:
urls: #安全路径白名单 urls: #安全路径白名单
......
...@@ -71,4 +71,12 @@ ...@@ -71,4 +71,12 @@
GROUP BY GROUP BY
ur.id ur.id
</select> </select>
<select id="getAdminIdList" resultType="java.lang.Long">
SELECT
DISTINCT ar.admin_id
FROM
ums_role_resource_relation rr
LEFT JOIN ums_admin_role_relation ar ON rr.role_id = ar.role_id
WHERE rr.resource_id=#{resourceId}
</select>
</mapper> </mapper>
\ No newline at end of file
Supports Markdown
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