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

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

membalas semua(3)
世界只因有你

Menggunakan auto proxy aop (spring default) tidak dapat menyelesaikan masalah panggilan dalaman dalam kelas. Jika anda ingin menyelesaikan masalah ini, anda perlu menggunakan aspectj (spring boleh disepadukan dengan aspectj Aspectj menggunakan runtime weaving atau compile-time weaving, yang akan mengubah suai bytecode, yang boleh menyelesaikan masalah ini.

仅有的幸福

Ini benar-benar tidak mungkin. Ini disebabkan oleh prinsip spring aop

phpcn_u1582

SpringPanggil dalam kelasthis untuk mendapatkan objek sasaran, bukan objek proksi

  1. Melalui BeanPostProcessor, selepas kelas dimulakan seperti biasa, masukkan diri anda ke dalam kacang

  2. Dapatkan objek proksi semasa melalui AopContext.currentProxy

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!