redis - spring缓存 同一个 类中调用 缓存无效

 wu勿望我_957 发布于 2022-10-26 08:38
package com.github.wuhulala.service;

import com.github.wuhulala.mappers.SecKillDao;
import com.github.wuhulala.model.SecKill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xueaohui
 */
@Service
public class SecKillService {
    @Autowired
    private SecKillDao secKillDao;

    //@CachePut(value = "seckills",key = "#secKill.id")
    public int insertSecKill(SecKill secKill) {
        return secKillDao.createSecKill(secKill);
    }

    @Cacheable(value = "seckills", key = "#secKill.id")
    public SecKill findOne(SecKill secKill) {
        System.out.println("缓存中没有 开始查询");
        return secKillDao.getSecKill(secKill.getId());
    }

    @CachePut(value = "seckills", key = "#secKill.id")
    public SecKill update(SecKill secKill) {
        System.out.println("更新secKill-----" + secKill);
        secKillDao.updateSecKill(secKill);
        return secKill;
    }

    @Transactional
    public int getSecKill(int id) {
        SecKill secKill = findOne(new SecKill(id));
        int number = secKill.getNumber();
        if (number > 0) {
            secKill.setNumber(number - 1);
            update(secKill);
            return id;
        } else {
            return -1;
        }
    }
}
import com.github.wuhulala.AppConfig;
import com.github.wuhulala.model.SecKill;
import com.github.wuhulala.service.SecKillService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import static org.junit.Assert.assertEquals;

/**
 * @author xueaohui
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
@WebAppConfiguration("src/main/resources")
public class SecKillTest {
    @Autowired
    private SecKillService service;

    @Test
    public void TestGetSecKill(){
        for(int i = 0 ; i < 10 ; i++) {
            System.out.println(service.getSecKill(9));
        }
    }

}
/*
缓存中没有 开始查询
23:51:35,977 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,042 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,072 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
更新secKill-----SecKill{id=9, name='秒杀产品1', number=0}
23:51:36,075 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - ==>  Preparing: update seckills set number = ? , name = ? where id=? 
23:51:36,078 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - ==> Parameters: 0(Integer), 秒杀产品1(String), 9(Integer)
23:51:36,081 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - <==    Updates: 1
9
缓存中没有 开始查询
23:51:36,090 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,092 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,095 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,099 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,101 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,103 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,108 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,109 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,112 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,119 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,121 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,124 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,128 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,129 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,135 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,140 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,142 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,145 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,149 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,150 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,155 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,162 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,163 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,166 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
缓存中没有 开始查询
23:51:36,171 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==>  Preparing: select * from seckills where id=? 
23:51:36,173 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,176 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <==      Total: 1
-1
*/
3 个回答
  • Spring类内部调用this获取的是目标对象,不是代理对象

    1. 通过BeanPostProcessor,在类正常初始化完毕以后,把自己注入bean中

    2. 通过AopContext.currentProxy获取当前代理对象

    2022-10-27 00:44 回答
  • 确实不可以 这是spring aop的原理所导致的

    2022-10-27 00:44 回答
  • 使用auto proxy的aop(spring默认)解决不了类内部调用的问题。如果要解决这个问题得上aspectj(spring可以和aspectj的集成),aspectj采用运行时weaving 或者 编译时 weaving,会修改字节码,可以解决这个问题。

    2022-10-27 00:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有