spring boot spring cache ehcache3.x整合

http://blog.csdn.net/qq18998401056/article/details/53467671

**************************************************************************

在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();
    }

}

原文地址:https://www.cnblogs.com/zhao1949/p/8124325.html

时间: 2024-08-30 10:27:00

spring boot spring cache ehcache3.x整合的相关文章

spring boot guava cache 缓存学习

http://blog.csdn.net/hy245120020/article/details/78065676 ************************************************************ spring boot guava cache 缓存学习 自定义key 自定义全局key过期时间,缓存个数 针对单个key自定义过期时间,缓存个数 引入依赖 <dependency> <groupId>org.springframework.boo

Spring -&gt; Spring Boot &gt; Spring Cloud

Spring -> Spring Boot > Spring Cloud 这几天刚刚上班,公司用的是Spring Cloud,接触不多.我得赶快学起来. 想学习就必须得知道什么是微服务,什么是Spring Boot,什么是Spring Cloud,以及两者之间有什么关系? 什么是微服务? 简而言之,微服务架构风格是一种将单个应用程序作为一套小型服务开发的方法,每种应用程序都在自己的进程中运行,并与轻量级机制(通常是HTTP资源API)进行通信. 这些服务是围绕业务功能构建的,可以通过全自动部署

Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

spring boot以其众多友谊的特性,如零配置.微服务等,吸引了很多的粉丝.而其与Spring Security安全框架的无缝结合,使其具备的安全的特性.在此基础上使用Thymeleaf模板引擎进行渲染,静动态结合,让页面开发更加简单.直观. 通过表单提交登录的用户名和密码是登录接口比较常见的一种设计.在初学的过程中,我也不例外的采用个这种方式.表单设计见下图. 登录成功,完成正常的主页面跳转,这个不存在问题.存在问题的是,登录失败了该咋办呢?我就在考虑,由于thymeleaf的局部刷新操作

Spring Boot/Spring Cloud、ESB、Dubbo

如何使用Spring Boot/Spring Cloud 实现微服务应用spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状态管理等操作提供了一种简单的开发方式. Spring Cloud与Dubbo对比提到Dubbo,我想顺便提下ESB,目前央视新华社也在用ESB来做任务编排,这里先比较下Dubbo和ESB: ESB(企业数据总线),一般采用集中式

Angular集成Spring Boot,Spring Security,JWT和CORS

本文介绍了Spring Boot的基本配置,Angular集成Spring Boot.Spring Security的方法.当前流行的JWT更适合与Angular集成,优于Spring Secuity提供的CSRF.另外引入了springfox-swagger和spring-boot-starter-actuator,演示了如何利用Swagger生成JSON API文档,如何利用Actuator监控应用. 本文前端基于Angular官方样例Tour of Heroes,请先到官网下载. 技术堆栈

255.Spring Boot+Spring Security:使用md5加密

说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)hibernate5.2.17.Final (6)MySQLDriver 5.1.47 (7)MySQL 8.0.12 需求缘起 很多时候,我们自己已经有现成的一套系统在运行了,这时候要接入spring security的话,那么难免会碰到一个问题:就是自己设计的密码加密方式和spring secur

256.Spring Boot+Spring Security: MD5是加密算法吗?

说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)hibernate5.2.17.Final (6)MySQLDriver 5.1.47 (7)MySQL 8.0.12 前言 有网友在公众号留言:准确的说md5是摘要算法不是加密算法 针对这个问题,当时也没有仔细的思考,空下来的时候,对于这个问题整理了下思路. 一.加密算法 1.1 加密和解密 1.1

Spring Boot 2.X 如何快速整合jpa?

本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.SpringData是Spring的一个子项目.用于简化数据库访问,支持NoSQL和关系数据存储,其主要目标是使数据库的访问变得方便快捷.Spr

Spring Boot的简介与idea整合jsp

一.Spring Boot简介 SpringBoot是一个框架,他的产生简化了框架的使用,所谓简化是指简化了Spring众多框架中所需的大量且繁琐的配置文件.它使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来.使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器).准生产级别的基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很少的Spring配置. SpringB