发表于: 2020-07-03 23:54:33
1 1940
今天完成的事情
1. 完成所有的压测
这个是我控制好的线程数刚好不让数据库崩溃的情况下得到的数据。
测试的数据比较大有 53m,打开的时候 jmeter 都卡住了。
可以看到首页的访问速度比较慢,符合预期,因为首页需要查询的表最多。
遇到的问题
1. 有一个切点没能生效
切面类
/**
* @ClassName RedisAOP
* @Description Redis 缓存切面
* @Author owlwinter
* @Date 2020/7/2 21:49
* @Version 1.0
**/
@Aspect
public class RedisAOP {
private static Logger logger = LogManager.getLogger(RedisAOP.class);
@Pointcut("execution(* cn.mogeek.jsptiles.service.impl..*(..))")
public void cachePoint(){}
@Around(value = "cachePoint() && @annotation(cache)")
public Object cachedData(ProceedingJoinPoint joinPoint, Cache cache) throws Throwable {
Object proceed;
String key = cache.key();
if (joinPoint.getArgs().length != 0){ key += joinPoint.getArgs().toString();
Object value = RedisUtil.get(key);
if (cache.clazz().isInstance(value)){
proceed = value;
logger.info("\nRedisUtil@Get cache \nkey:{}\nvalue:{}", cache.key(), proceed);
}else {
proceed = joinPoint.proceed(joinPoint.getArgs());
RedisUtil.set(key, proceed, new Date(cache.expiration() * 1000));
logger.info("\nRedisUtil@Set cache \nkey:{}\nvalue:{}\nexpiration:{}",
cache.key(), proceed, cache.expiration());
}
return proceed;
}
}
自定义缓存注解
/**
* @Description: 缓存注解
* @Param: key 缓存key,expiration 有效时间,默认一分钟
* @return:
* @Author: owlwinter
* @Date: 2020/6/27
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Cache {
String key();
Class clazz();
long expiration() default 60;
}
正常运行的连接点函数
/**
* @Description: 获取首页轮播图
* @Param: []
* @return: java.util.List<cn.mogeek.jsptiles.model.Banner>
* @Author: owlwinter
* @Date: 2020/6/16
*/
@Cache(key = "BannerServiceImpl_list", clazz = List.class, expiration = (long) 40)
@Override
public List<Banner> list(){
return bannerMapper.selectByExample(new BannerExample());
}
没能生效的连接点函数
/**
* @Description: 根据用户名获取用户信息
* @Param: [name]
* @return: cn.mogeek.jsptiles.model.LoginUser
* @Author: owlwinter
* @Date: 2020/6/17
*/
@Cache(key = "selectByName_", clazz = List.class)
@Override
public List<LoginUser> selectByName(String name){
LoginUserExample loginUserExample = new LoginUserExample();
loginUserExample.createCriteria().andNameEqualTo(name);
List<LoginUser> loginUserList = loginUserMapper.selectByExample(loginUserExample);
return new ArrayList<>(loginUserList);
}
现在感觉是因为这个连接点方法带参数所以没法正常运作,调了一个晚上都没能弄好。
=============更新===============
https://www.cnblogs.com/duanxz/p/4367362.html
问题已经解决,service 内部调用需要缓存的方法使得 aop 失效。
解决的方法:
在调用内部方法之前切换到当前代理再去调用。
xml 文件
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
代码
/**
* @Description: 对用户信息进行校验
* @Param: [name, password]
* @return: cn.mogeek.jsptiles.model.LoginUser
* @Author: owlwinter
* @Date: 2020/6/16
*/
@Override
public boolean checkUser(String name, String password){
logger.debug("LoginUserServiceImpl.checkUser({})", name);
LoginUserService loginUserService = (LoginUserService) AopContext.currentProxy();
List<LoginUser> loginUserList = loginUserService.selectByName(name);
logger.debug("total:{}", loginUserList.size());
if (loginUserList.size() == 1 && BCrypt.checkpw(password, loginUserList.get(0).getPasswd())){
logger.debug("密码校验成功");
return true;
}else {
logger.debug("密码校验失败, username:{}, password:{}", name, password);
return false;
}
}
/**
* @Description: 根据用户名获取用户信息
* @Param: [name]
* @return: cn.mogeek.jsptiles.model.LoginUser
* @Author: owlwinter
* @Date: 2020/6/17
*/
@Cache(key = "selectByName_", clazz = List.class)
@Override
public List<LoginUser> selectByName(String name){
LoginUserExample loginUserExample = new LoginUserExample();
loginUserExample.createCriteria().andNameEqualTo(name);
List<LoginUser> loginUserList = loginUserMapper.selectByExample(loginUserExample);
return new ArrayList<>(loginUserList);
}
收获
1. 关于 spring 自动代理
明天的计划
1. 完成任务6
评论