springboot使用缓存cache

springboot使用缓存cache


1.引入jar包

		<!-- Springboot中开启缓存 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

2.在启动类中开启缓存

@SpringBootApplication
@EnableAutoConfiguration
@EnableCaching
@MapperScan("com.learning.www.mapper")
public class LearningApplication {

	public static void main(String[] args) {
		SpringApplication.run(LearningApplication.class, args);
	}
}

3.在service的实现中加入缓存(重点) 

	@Override
	@Cacheable(value = "ZphInfo", key = "#id")
	public ZphInfo getZphInfoById(int id) {
		return zphinfomapper.getZphInfoById(id);
	}

4.编写controller

	@RequestMapping("getZphInfobyid")
	@ResponseBody
	public String getZphInfoById(String id) { 
		
		int zphid = Integer.parseInt(id);
		ZphInfo zphinfo = zphservice.getZphInfoById(zphid);
		
		return "根据查找到了招聘会信息:"+zphinfo.toString();
		
	}

5.验证结果

第一次访问:

————权限认证————
2018-11-24 09:33:26.572 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : ==>  Preparing: select username,password,role from user where username=? 
2018-11-24 09:33:26.573 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : ==> Parameters: joke (String)
2018-11-24 09:33:26.576 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : <==      Total: 1
2018-11-24 09:33:40.554  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : URL : http://localhost:8080/zph/getZphInfobyid
2018-11-24 09:33:40.555  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : HTTP_METHOD : GET
2018-11-24 09:33:40.555  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : IP : 0:0:0:0:0:0:0:1
2018-11-24 09:33:40.557  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : CLASS_METHOD : com.learning.www.controller.ZphInfoController.getZphInfoById
2018-11-24 09:33:40.559  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : ARGS : [2]
2018-11-24 09:33:40.620 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : ==>  Preparing: select * from zphinfo where id = ? 
2018-11-24 09:33:40.621 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : ==> Parameters: 2(Integer)
2018-11-24 09:33:40.658 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : <==      Total: 1
2018-11-24 09:33:40.658  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : RESPONSE : 根据查找到了招聘会信息:ZphInfo(id=2, title=测试2, time=2019, place=null, add=null)

第二次访问:没有sql语句打印,说明未访问数据库,而是走了缓存。

2018-11-24 09:35:06.512  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : URL : http://localhost:8080/zph/getZphInfobyid
2018-11-24 09:35:06.513  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : HTTP_METHOD : GET
2018-11-24 09:35:06.514  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : IP : 0:0:0:0:0:0:0:1
2018-11-24 09:35:06.515  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : CLASS_METHOD : com.learning.www.controller.ZphInfoController.getZphInfoById
2018-11-24 09:35:06.515  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : ARGS : [2]
2018-11-24 09:35:06.520  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : RESPONSE : 根据查找到了招聘会信息:ZphInfo(id=2, title=测试2, time=2019, place=null, add=null)

6.切换缓存(未实测)

切换为EhCache作为缓存

pom中添加一下依赖:

<dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

在resource 文件夹下新建ehcache的配置文件ehcache.xml 内容如下,此文件spring boot 会自动扫描 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--切换为ehcache 缓存时使用-->
<cache name="people" maxElementsInMemory="1000" />
</ehcache>

切换为Guava作为缓存

只需要在pom中添加依赖

     <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>

问题:

不知道如何将List存入缓存中,有大神知道的话,麻烦您给个链接或者下方评论指导一下,感激不尽!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rm-r

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值