关于spring:spring-boot-整合-ehcache

9次阅读

共计 2690 个字符,预计需要花费 7 分钟才能阅读完成。

1. 该说的话

每个人都该当学会独立地去思考、去寻找答案,而不是一味地伸手向别人索取所谓的标准答案。首先,别成为“拿来主义”者,其次远离 ” 拿来主义 ” 的人。

2. ehcache

2.1 次要个性

  1. 疾速,简略.
  2. 多种缓存策略
  3. 缓存数据有两级:内存和磁盘,因而无需放心容量问题
  4. 缓存数据会在虚拟机重启的过程中写入磁盘
  5. 能够通过 RMI、可插入 API 等形式进行分布式缓存
  6. 具备缓存和缓存管理器的侦听接口
  7. 反对多缓存管理器实例,以及一个实例的多个缓存区域
  8. 提供 Hibernate 的缓存实现

2.2 和 redis 相比

  • ehcache 间接在 jvm 虚拟机中缓存,速度快,效率高;然而缓存共享麻烦,集群分布式应用不不便。
  • redis 是通过 socket 拜访到缓存服务,效率比 ecache 低,比数据库要快很多.

2.3 在应用程序中的地位

3. spring boot 整合

1. 搭建 spring boot 我的项目

  1. pom.xml 文件中增加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
  1. 增加 ehcache.xml 配置文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

<!--
        磁盘存储: 将缓存中临时不应用的对象, 转移到硬盘, 相似于 Windows 零碎的虚拟内存
        path: 指定在硬盘上存储对象的门路
        path 能够配置的目录有:user.home(用户的家目录)user.dir(用户以后的工作目录)java.io.tmpdir(默认的长期目录)ehcache.disk.store.dir(ehcache 的配置目录)绝对路径(如:d:\\ehcache)查看门路办法:String tmpDir = System.getProperty("java.io.tmpdir");
     -->
    <diskStore path="java.io.tmpdir" />

    <!--
        defaultCache: 默认的缓存配置信息, 如果不加非凡阐明, 则所有对象依照此配置项解决
        maxElementsInMemory: 设置了缓存的下限, 最多存储多少个记录对象
        eternal: 代表对象是否永不过期 (指定 true 则上面两项配置需为 0 无限期)
        timeToIdleSeconds: 最大的发愣工夫 / 秒
        timeToLiveSeconds: 最大的存活工夫 / 秒
        overflowToDisk: 是否容许对象被写入到磁盘
        阐明:下列配置自缓存建设起 600 秒 (10 分钟) 无效。在无效的 600 秒 (10 分钟) 内,如果间断 120 秒 (2 分钟) 未拜访缓存,则缓存生效。就算有拜访,也只会存活 600 秒。-->
    <defaultCache maxElementsInMemory="10000" eternal="false"
                  timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" />

    <!-- 无效工夫:7200 秒 = 2 小时,间断 180 秒 = 3 分钟未拜访缓存,则生效 -->
    <cache name="userCache" maxElementsInMemory="10000" eternal="false"
           timeToIdleSeconds="1800" timeToLiveSeconds="7200" overflowToDisk="true" />

</ehcache>
  1. 启用缓存

在启动类增加 @EnableCaching 注解。
5. 利用
如下代码所示,你能够在函数上应用 @Cacheable,@CachePut,@CacheEvict,来新增、更新、删除缓存。
留神,当这个类中所有的缓存都处于同一缓存区时,你能够在类名上方应用 @CacheConfig(cacheNames =userCache)来配置,这样在函数注解上就不须要再写 value = userCache。(cacheNames 的值在 ehcache.xml 文件中配置。)

@Service
public class UserServiceImpl implements IUserService {


    @Resource
    private UserRepository userRepository;

    @Override
    @Cacheable(value = "userCache", key = "#user.id")
    public boolean addUser(UserEntity user) {return userRepository.saveAndFlush(user).getId() != null;}

    @Override
    @CachePut(value = "userCache", key = "#user.id")
    public boolean updateUser(UserEntity user) {return userRepository.saveAndFlush(user).getId() != null;}

    @Override
    @CacheEvict(value = "userCache", key = "#id")
    public boolean deleteUser(Long id) {return false;}

    @Override
    @Cacheable(value = "userCache", key = "#id")
    public UserEntity selectUser(Long id) {return null;}
}

当你想要删除某一缓存区所有缓存时,能够应用 @CacheEvict(value = “userCache”, allEntries = true),删除 userCache 中所有的缓存。

4.“毒鸡汤”,和我一起干了吧!

每个人都“画地为牢”,把志同道合者划入圈内,把异己者排除在外。选好你的“地”,划好你的“圈”。“亲贤臣,远君子,此先汉所以兴旺也;亲君子,远贤臣,尔后汉所以倾颓也。”把本人经营好,就相当于把本人的圈子经营好,和志同道合者,一起去驯服星辰大海!

示例代码可在我的 github.com 中找到。
欢送关注我。

关注公众号:锅外的大佬
千河流银的博客

正文完
 0