>  Q&A  >  본문

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

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
*/
怪我咯怪我咯2704일 전802

모든 응답(3)나는 대답할 것이다

  • 世界只因有你

    世界只因有你2017-04-26 09:03:33

    자동 프록시 AOP(스프링 기본값)를 사용하면 클래스 내부 호출 문제를 해결할 수 없습니다. 이 문제를 해결하려면 Aspectj를 사용해야 합니다.(Spring은 Aspectj와 통합될 수 있습니다.) Aspectj는 런타임 Weaving 또는 컴파일 타임 Weaving을 사용하는데, 이는 이 문제를 해결할 수 있는 바이트코드를 수정합니다.

    회신하다
    0
  • 仅有的幸福

    仅有的幸福2017-04-26 09:03:33

    정말 불가능합니다. 이는 spring Aop의 원리 때문입니다

    회신하다
    0
  • phpcn_u1582

    phpcn_u15822017-04-26 09:03:33

    Spring클래스 내에서 호출this 프록시 객체가 아닌 대상 객체를 얻으려면

    1. BeanPostProcessor을 통해 클래스가 정상적으로 초기화된 후 자신을 빈에 인젝션한다

    2. AopContext.currentProxy

    3. 을 통해 현재 프록시 객체를 가져옵니다.

    회신하다
    0
  • 취소회신하다