redis - spring缓存 同一个 类中调用 缓存无效
怪我咯
怪我咯 2017-04-26 09:01:33
0
3
828
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
*/
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
世界只因有你

Using auto proxy's aop (spring default) cannot solve the problem of internal calls in the class. If you want to solve this problem, you have to use aspectj (spring can be integrated with aspectj). Aspectj uses runtime weaving or compile-time weaving, which will modify the bytecode, which can solve this problem.

仅有的幸福

It’s really not possible. This is caused by the principle of spring aop

phpcn_u1582

Spring类内部调用thisWhat you get is the target object, not the proxy object

  1. By BeanPostProcessor, after the class is initialized normally, inject yourself into the bean

  2. Get the current proxy object through AopContext.currentProxy

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!