记录SpringBoot的缓存机制
当前记录的是SpringBoot的缓存处理
前言:
学习的地址:https://www.majiaxueyuan.com/front/showcoulist 、https://www.bilibili.com/video/av47407342/
当前版本是记录的SpringBoot的缓存处理机制
SpringBoot的pom依赖(以2.0版本为例的)
1.缓存注解的添加
1.数据缓存需要在Application的主方法中添加注解
@EnableCaching
2. 在mapper接口中添加注解:
@CacheConfig(cacheName="")
.因为cacheName中是需要扫描对应的xml文件中的cachename=baseCache的参数
这个xml文件中是去配置这个缓存的参数的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<!-- 默认配置 eternal 持久化 timeToIdleSeconds 最大空闲时间 s timeToLiveSeconds 生命周期 s memoryStoreEvictionPolicy 策略 overflowToDisk 保存到硬盘 -->
<defaultCache maxElementsInMemory="5000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU" overflowToDisk="false"/>
<cache name="baseCache" maxElementsInMemory="10000"
maxElementsOnDisk="100000"/>
</ehcache>
这个是和application.properties相同路径下。
缓存机制多用在查询阶段,减少大量连接数据库大影响效佬率
所以在mapper接口查询的方法中添加一个注解
@Cacheable
3.举个例子
咱查询一个数据等存到缓存中后修改下在数据库的参数 看第二次查询是否会成功
代码如下:
@SpringBootApplication
@EnableCaching
public class Springboot2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
controller:
@Controller
@RequestMapping("/game")
public class GameController {
@Autowired
private GamesMapper gamesMapper;
@GetMapping("/selectByName")
@ResponseBody
public String getGame(@Param("gameName") String gameName) {
GamesPO gamesPO = gamesMapper.selectByName(gameName);
if (gamesPO==null){
return "1232131";
}
return gamesPO.toString();
}
}
mapper:
@Mapper
@Repository@CacheConfig(cacheNames = "gameCache")
public interface GamesMapper {
@Select("SELECT game_id , game_name,game_description FROM games"
public List<GamesPO> listGames();
@Select("SELECT game_id , game_name,game_description FROM games WHERE game_name = #{gameName}")
@Cacheable
public GamesPO selectByName(@Param("gameName") String gameName);
}
ehcache.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<!-- 默认配置 eternal 持久化 timeToIdleSeconds 最大空闲时间 s timeToLiveSeconds 生命周期 s memoryStoreEvictionPolicy 策略 overflowToDisk 保存到硬盘 -->
<defaultCache maxElementsInMemory="5000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU" overflowToDisk="false"/>
<cache name="baseCache" maxElementsInMemory="10000"
maxElementsOnDisk="100000"/>
<cache name="gameCache" maxElementsInMemory="10000"
maxElementsOnDisk="100000"/>
</ehcache>
4.运行
接下来我们在数据库中进行一个修改
我们再查询一次
说明cache缓存起作用了。
注意:
但是怎么在update数据后 将查到的数据也进行更改?
记录到下一章
以上。