spring boot spring cache ehcache3.x整合

在Spring Boot中通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。默认是simple类型。
由于ehcache3.x实现了jsr-107的标准接口,而本文通过整合ehcache3.x来使用JCache的方式。
引入依赖如下:

        <dependency>
          <groupId>org.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>3.1.3</version>
        </dependency>
        <!-- JSR107 API -->
        <dependency>
          <groupId>javax.cache</groupId>
          <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

ehcache 3.x配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<config
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
  xmlns='http://www.ehcache.org/v3'
  xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
  <!-- <service>
    <jsr107:defaults>
      <jsr107:cache name="city" template="heap-cache"/> 
    </jsr107:defaults>
  </service> -->

  <cache-template name="heap-cache">
    <resources>
      <heap unit="entries">2000</heap> 
      <offheap unit="MB">100</offheap> 
    </resources>
  </cache-template>

  <cache alias="city" uses-template="heap-cache">
    <expiry>
      <ttl unit="seconds">40</ttl>
    </expiry>
  </cache> 

</config>

spring boot application.properties配置如下:

#注意:ehcache3.x配置文件路径必须指定
spring.cache.jcache.config=classpath:ehcache.xml

在spring boot 使用@EnableCaching 开启缓存
最后,贴出spring cache注解例子伪代码:

package com.lrh.service;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lrh.dao.CityMapper;
import com.lrh.domain.City;
import com.lrh.iservice.CityService;

@Service
@Transactional
@CacheConfig(cacheNames = "city")
//@CacheDefaults(cacheName = "city")
public class CityServiceImpl implements CityService{

    @Autowired
    private CityMapper cityMapper;

    @Override
    @CachePut(key = "#id")
    public City editCity(String id, String name) {
        cityMapper.edit(id, name);
        City city=new City();
        city.setId(Long.valueOf(id));
        city.setName(name);
        return city;
    }

    @Override
    public PageInfo<City> selectCityByPage() {
         PageHelper.startPage(1,5);
         List<City> list = cityMapper.selectAll();
         PageInfo<City> page = new PageInfo(list);
         return page;
    }
    /**
     * condition满足缓存条件的数据才会放入缓存,condition在调用方法之前和之后都会判断
     * unless用于否决缓存更新的,不像condition,该表达只在方法执行之后判断,此时可以拿到返回值result进行判断了   
     */
    @Override
    @Cacheable(key = "#id",unless="#result == null")
    //@CacheResult
    public City findById(String id) {
        return cityMapper.selectCityById(id);
    }

    @Override
    @CacheEvict(key="#id")
    public void delete(String id) {
        //cityMapper.delete(id);
    }

    /**
     * allEntries移除所有
     */
    @Override
    @CacheEvict(allEntries = true)
    public void deleteAll() {
        cityMapper.deleteAll();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值