温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot下EhCache缓存的使用

发布时间:2021-08-26 16:40:51 阅读:186 作者:chen 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容主要讲解“Spring Boot下EhCache缓存的使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Boot下EhCache缓存的使用”吧!

使用EhCache

本篇我们将介绍如何在Spring Boot中使用EhCache进程内缓存。这里我们将沿用之前的案例结果来进行改造,以实现EhCache的使用。

先回顾下这个基础案例的三个部分:

User实体的定义

@Entity@Data@NoArgsConstructorpublic class User {    @Id    @GeneratedValue    private Long id;    private String name;    private Integer age;    public User(String name, Integer age) {        this.name = name;        this.age = age;    }}

User实体的数据访问实现(涵盖了缓存注解)

@CacheConfig(cacheNames = 
"users")public interface UserRepository extends JpaRepository<User, 
Long> {    @Cacheable    User 
findByName(String name);}

测试验证用例(涵盖了CacheManager的注入,可用来观察使用的缓存管理类)

@Slf4j@RunWith(SpringRunner.class)@SpringBootTestpublic class Chapter51ApplicationTests {    @Autowired    private UserRepository userRepository;    @Autowired    private CacheManager cacheManager;    @Test    public void test() throws Exception {        // 创建1条记录        userRepository.save(new User("AAA", 10));        User u1 = userRepository.findByName("AAA");        System.out.println("第一次查询:" + u1.getAge());        User u2 = userRepository.findByName("AAA");        System.out.println("第二次查询:" + u2.getAge());    }}

接下来我们通过下面的几步操作,就可以轻松的把上面的缓存应用改成使用ehcache缓存管理。

第一步:在pom.xml中引入ehcache依赖

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

在Spring Boot的parent管理下,不需要指定具体版本,会自动采用Spring Boot中指定的版本号。


Spring Boot下EhCache缓存的使用

第二步:在src/main/resources目录下创建:ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
xsi:noNamespaceSchemaLocation="ehcache.xsd">    <cache name="users"           
maxEntriesLocalHeap="200"           
timeToLiveSeconds="600">    </cache></ehcache>

完成上面的配置之后,再通过debug模式运行单元测试,观察此时CacheManager已经是EhCacheManager实例,说明EhCache开启成功了。或者在测试用例中加一句CacheManager的输出,比如:

@Autowiredprivate CacheManager cacheManager;@Testpublic 
void test(
) throws Exception {    System.out.println("CacheManager type : " + cacheManager.getClass());    userRepository.save(new User("AAA", 
10));    User u1 = userRepository.findByName("AAA");    System.out.println("第一次查询:" + u1.getAge());    User u2 = userRepository.findByName("AAA");    System.out.println("第二次查询:" + u2.getAge());}

执行测试输出可以得到:

CacheManager type : class org.springframework.cache.ehcache.EhCacheCacheManagerHibernate: 
select next_val 
as id_val 
from hibernate_sequence 
for updateHibernate: 
update hibernate_sequence 
set next_val= ? 
where next_val=?Hibernate: 
insert into user (age, 
name, 
id) 
values (?, ?, ?)2020-07-14 18:09:28.465  INFO 
58538 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactoryHibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?第一次查询:10第二次查询:10

可以看到:

  1. 第一行输出的CacheManager type为org.springframework.cache.ehcache.EhCacheCacheManager,而不是之前讲的ConcurrentHashMap了。

  2. 第二次查询的时候,没有输出SQL语句,所以是走的缓存获取

整合成功!

到此,相信大家对“Spring Boot下EhCache缓存的使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:http://blog.itpub.net/69923331/viewspace-2705033/

AI

开发者交流群×