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存入缓存中,有大神知道的话,麻烦您给个链接或者下方评论指导一下,感激不尽!!

猜你喜欢

转载自blog.csdn.net/qq_39847344/article/details/84424229
今日推荐