`

37. Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

阅读更多

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

 

【本文章是否对你有用以及是否有好的建议,请留言】

写后感:博主写这么一系列文章也不容易啊,请评论支持下。

 

       如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了。

       那么我们先说说这一篇文章我们都会学到的技术点:Spring Data JPA,Spring Boot 使用Mysql,Spring MVC,EHCache,Spring Cache等(其中@Cacheable请看上一节的理论知识),具体分如下几个步骤:

(1)新建Maven Java Project
(2)在pom.xml中加入依赖包
(3)编写Spring Boot启动类;
(4)配置application.properties;
(5)编写缓存配置类以及ehcache.xml配置文件;
(6)编写DemoInfo实体类进行测试;
(7)编写持久类DemoInfoRepository;
(8)编写处理类DemoInfoService;
(9)编写DemoInfoController测试类;
(10)运行测试;

 

 

以上就是具体的步骤了,那么接下来我们一起按照这个步骤来进行实现吧。

 

1)新建Maven Java Project

       新建一个工程名为spring-boot-ehcachemaven java project

 

2)在pom.xml中加入依赖包

       pom.xml文件中加入相应的依赖包,Spring Boot父节点依赖包;spring boot web支持;缓存依赖spring-context-support;集成ehcache需要的依赖;JPA操作数据库;mysql 数据库驱动,具体pom.xml文件:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.kfit</groupId>
	<artifactId>spring-boot-ehcache</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-ehcache</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

		<!-- 配置JDK编译版本. -->
		<java.version>1.8</java.version>
	</properties>


	<!-- spring boot 父节点依赖, 
		引入这个之后相关的引入就不需要添加version配置,
		 spring boot会自动选择最合适的版本进行添加。 
	-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<dependencies>
		<!-- 单元测试. -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- spring boot web支持:mvc,aop... -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 
			包含支持UI模版(Velocity,FreeMarker,JasperReports),
			邮件服务,
			脚本服务(JRuby),
			缓存Cache(EHCache),
			任务计划Scheduling(uartz)。
		 -->
		<dependency>
	      <groupId>org.springframework</groupId>
	      <artifactId>spring-context-support</artifactId>
	    </dependency>

		<!-- 集成ehcache需要的依赖-->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
		</dependency>
		
		<!-- JPA操作数据库. -->
		<dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-data-jpa</artifactId>
	    </dependency>
		
		<!-- mysql 数据库驱动. -->
		<dependency>
	      <groupId>mysql</groupId>
	      <artifactId>mysql-connector-java</artifactId>
	    </dependency>
	    
	    <!-- Spring boot单元测试. -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
	</dependencies>
</project>

 

 

 

3)编写Spring Boot启动类(com.kfit.App.java);

package com.kfit;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 *
 *
 * @SpringBootApplication申明让spring boot自动给程序进行必要的配置,
 *
@SpringBootApplication
等待于:
@Configuration
@EnableAutoConfiguration
@ComponentScan
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
       SpringApplication.run(App.class, args);
    }
}

 

 

 

4)配置application.properties;

       application.properties中主要配置数据库连接和JPA的基本配置,具体如下:

Src/main/resouces/application.properties

########################################################

###datasource ,mysql数据库连接配置

########################################################

spring.datasource.url = jdbc:mysql://localhost:3306/test

spring.datasource.username = root

spring.datasource.password = root

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

 

 

 

########################################################

### Java Persistence Api JPA自动建表操作配置

########################################################

# Specify the DBMS

spring.jpa.database = MYSQL

# Show or not log for each sql query

spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)

spring.jpa.hibernate.ddl-auto = update

# Naming strategy

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

 

5)编写缓存配置类以及ehcache.xml配置文件:

       这个类主要是注册缓存管理对象EhCacheCacheManager、缓存工厂对象EhCacheManagerFactoryBean,具体代码如下:
EhCacheManagerFactoryBean:

package com.kfit.config;
 
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
 
/**
 * 缓存配置.
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Configuration
@EnableCaching//标注启动缓存.
public class CacheConfiguration {
   
    /**
     *  ehcache 主要的管理器
     * @param bean
     * @return
     */
    @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
       System.out.println("CacheConfiguration.ehCacheCacheManager()");
       return new EhCacheCacheManager(bean.getObject());
    }
   
    /*
       * 据shared与否的设置,
       * Spring分别通过CacheManager.create()
       * 或new CacheManager()方式来创建一个ehcache基地.
       *
       * 也说是说通过这个来设置cache的基地是这里的Spring独用,还是跟别的(如hibernate的Ehcache共享)
       *
       */
      @Bean
      public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()");
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
        cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("conf/ehcache.xml"));
        cacheManagerFactoryBean.setShared(true);
        return cacheManagerFactoryBean;
      }
   
}

 

 

src/main/resouces/conf下编写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:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:
       user.home – 用户主目录
       user.dir  – 用户当前工作目录
       java.io.tmpdir – 默认临时文件路径
      
     -->
    <diskStore path="java.io.tmpdir/Tmp_EhCache" />
   
    <!--
       defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
     -->
     
     <!--
       name:缓存名称。
       maxElementsInMemory:缓存最大数目
       maxElementsOnDisk:硬盘最大缓存个数。 
       eternal:对象是否永久有效,一但设置了,timeout将不起作用。 
       overflowToDisk:是否保存到磁盘,当系统当机时
       timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
       timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
       diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 
       diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
       diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
       memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 
        clearOnFlush:内存数量最大时是否清除。
         memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
            FIFO,first in first out,这个是大家最熟的,先进先出。
            LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
            LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
    -->
    <defaultCache
       eternal="false"
       maxElementsInMemory="1000"
       overflowToDisk="false"
       diskPersistent="false"
       timeToIdleSeconds="0"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LRU" />
 
    <cache
       name="demo"  
       eternal="false"
       maxElementsInMemory="100"
       overflowToDisk="false"
       diskPersistent="false"
       timeToIdleSeconds="0"
       timeToLiveSeconds="300"
       memoryStoreEvictionPolicy="LRU" />
      
</ehcache>

 

 

6)编写DemoInfo实体类进行测试;

com.kfit.bean下编写DemoInfo实体类进行缓存测试:

package com.kfit.bean;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
/**
 * 测试实体类.
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Entity
public class DemoInfo {
    @Id @GeneratedValue
    private longid;//主键.
    private String name;//名称;
    private String pwd;//密码;
    private int state;
    public long getId() {
       returnid;
    }
    public void setId(longid) {
       this.id = id;
    }
    public String getName() {
       returnname;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getPwd() {
       returnpwd;
    }
    public void setPwd(String pwd) {
       this.pwd = pwd;
    }
    public int getState() {
       returnstate;
    }
    public void setState(intstate) {
       this.state = state;
    }
    @Override
    public String toString() {
       return "DemoInfo [id=" + id + ", name=" + name + ", pwd=" + pwd + ", state=" + state + "]";
    }
}

 

 

7)编写持久类DemoInfoRepository;

       编写持久类DemoInfoRepository

com.kfit.repository.DemoInfoRepository

package com.kfit.repository;
 
import org.springframework.data.repository.CrudRepository;
 
import com.kfit.bean.DemoInfo;
 
/**
 * 操作数据库.
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
public interface DemoInfoRepository extends CrudRepository<DemoInfo,Long>{
 
}

 

 

8)编写处理类DemoInfoService;

       编写增删改查的方法,在这几个方法中都使用注解缓存,进行缓存的创建以及删除,修改等操作:

 com.kfit.service.DemoInfoService

package com.kfit.service;
 
import com.kfit.bean.DemoInfo;
 
import javassist.NotFoundException;
 
public interface DemoInfoService {
 
    void delete(Long id);
 
    DemoInfo update(DemoInfo updated) throws NotFoundException;
 
    DemoInfo findById(Long id);
 
    DemoInfo save(DemoInfo demoInfo);
 
}

 

 

com.kfit.service.impl.DemoInfoServiceImpl

package com.kfit.service.impl;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.kfit.bean.DemoInfo;
import com.kfit.repository.DemoInfoRepository;
import com.kfit.service.DemoInfoService;
 
import javassist.NotFoundException;
 
@Service
public class DemoInfoServiceImpl implements DemoInfoService {
   
    //这里的单引号不能少,否则会报错,被识别是一个对象;
    public static final String CACHE_KEY = "'demoInfo'";
  
     @Resource
     private DemoInfoRepository demoInfoRepository;
   
    /**
     * value属性表示使用哪个缓存策略,缓存策略在ehcache.xml
    */
    public static final String DEMO_CACHE_NAME = "demo";
   
    /**
     * 保存数据.
     * @param demoInfo
     */
    @CacheEvict(value=DEMO_CACHE_NAME,key=CACHE_KEY)
    @Override
    public DemoInfo save(DemoInfo demoInfo){
       return demoInfoRepository.save(demoInfo);
    }
   
    /**
     * 查询数据.
     * @param id
     * @return
     */
    @Cacheable(value=DEMO_CACHE_NAME,key="'demoInfo_'+#id")
    @Override
    public DemoInfo findById(Long id){
       System.err.println("没有走缓存!"+id);
       return demoInfoRepository.findOne(id);
    }
   
    /**
     * http://www.mincoder.com/article/2096.shtml:
     *
     * 修改数据.
     *
     * 在支持Spring Cache的环境下,对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
 
       @CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的。
     *
     * @param updated
     * @return
     *
     * @throws NotFoundException
     */
    @CachePut(value = DEMO_CACHE_NAME,key = "'demoInfo_'+#updated.getId()")
    //@CacheEvict(value = DEMO_CACHE_NAME,key = "'demoInfo_'+#updated.getId()")//这是清除缓存.
    @Override
    public DemoInfo update(DemoInfo updated) throws NotFoundException{
       DemoInfo demoInfo = demoInfoRepository.findOne(updated.getId());
       if(demoInfo == null){
           thrownew NotFoundException("No find");
       }
       demoInfo.setName(updated.getName());
       demoInfo.setPwd(updated.getPwd());
       return demoInfo;
    }
   
   
    /**
     * 删除数据.
     * @param id
     */
    @CacheEvict(value = DEMO_CACHE_NAME,key = "'demoInfo_'+#id")//这是清除缓存.
    @Override
    public void delete(Long id){
       demoInfoRepository.delete(id);
    }
      
   
}

 

 

9)编写DemoInfoController测试类;

编写一个rest进行测试:

com.kfit.controller.DemoInfoController

package com.kfit.controller;
 
import javax.annotation.Resource;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.kfit.bean.DemoInfo;
import com.kfit.service.DemoInfoService;
 
import javassist.NotFoundException;
 
@RestController
public class DemoInfoController {
 
    @Resource
    private DemoInfoService demoInfoService;
   
    @RequestMapping("/test")
    public String test(){
      
       //存入两条数据.
        DemoInfo demoInfo = new DemoInfo();
        demoInfo.setName("张三");
        demoInfo.setPwd("123456");
        DemoInfo demoInfo2 = demoInfoService.save(demoInfo);
      
        //不走缓存.
        System.out.println(demoInfoService.findById(demoInfo2.getId()));
        //走缓存.
        System.out.println(demoInfoService.findById(demoInfo2.getId()));
       
       
        demoInfo = new DemoInfo();
        demoInfo.setName("李四");
        demoInfo.setPwd("123456");
        DemoInfo demoInfo3 = demoInfoService.save(demoInfo);
       
        //不走缓存.
        System.out.println(demoInfoService.findById(demoInfo3.getId()));
        //走缓存.
        System.out.println(demoInfoService.findById(demoInfo3.getId()));
       
        System.out.println("============修改数据=====================");
        //修改数据.
        DemoInfo updated = new DemoInfo();
        updated.setName("李四-updated");
        updated.setPwd("123456");
        updated.setId(demoInfo3.getId());
        try {
           System.out.println(demoInfoService.update(updated));
       } catch (NotFoundException e) {
           e.printStackTrace();
       }
       
        //走缓存.
        System.out.println(demoInfoService.findById(updated.getId()));
       
       return "ok";
    }
   
   
}

 

 

10)运行测试;

运行App.java进行测试,访问:http://127.0.0.1:8080/test 进行测试,主要是观察控制台的打印信息。

Hibernate: insert into demo_info (name, pwd, state) values (?, ?, ?)

没有走缓存!52

DemoInfo [id=52, name=张三, pwd=123456, state=0]

DemoInfo [id=52, name=张三, pwd=123456, state=0]

Hibernate: insert into demo_info (name, pwd, state) values (?, ?, ?)

没有走缓存!53

DemoInfo [id=53, name=李四, pwd=123456, state=0]

DemoInfo [id=53, name=李四, pwd=123456, state=0]

============修改数据=====================

DemoInfo [id=53, name=李四-updated, pwd=123456, state=0]

DemoInfo [id=53, name=李四-updated, pwd=123456, state=0]

C:\Users\ADMINI~1.ANG\AppData\Local\Temp\

Hibernate: insert into demo_info (name, pwd, state) values (?, ?, ?)

没有走缓存!54

DemoInfo [id=54, name=张三, pwd=123456, state=0]

DemoInfo [id=54, name=张三, pwd=123456, state=0]

Hibernate: insert into demo_info (name, pwd, state) values (?, ?, ?)

没有走缓存!55

DemoInfo [id=55, name=李四, pwd=123456, state=0]

DemoInfo [id=55, name=李四, pwd=123456, state=0]

============修改数据=====================

DemoInfo [id=55, name=李四-updated, pwd=123456, state=0]

DemoInfo [id=55, name=李四-updated, pwd=123456, state=0]

 

       好了本篇文章就写到这里吧,打烊休息了,实在是动不了!

 

 

 Spring Boot 系列博客】

54. spring boot日志升级篇—logback【从零开始学Spring Boot

 

52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot 

 

51. spring boot属性文件之多环境配置【从零开始学Spring Boot

 

50. Spring Boot志升级篇—log4j【从零开始学Spring Boot

 

49. spring boot日志升级篇理论【从零开始学Spring Boot

 

48. spring boot单元测试restfull API【从零开始学Spring Boot

 

47. Spring Boot发送邮件【从零开始学Spring Boot

 

46. Spring Boot中使用AOP统一处理Web请求日志

 

45. Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot

 

44. Spring Boot日志记录SLF4J【从零开始学Spring Boot

 

43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot

 

42. Spring Boot多数据源【从零开始学Spring Boot

 

41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot

 

40. springboot + devtools(热部署)【从零开始学Spring Boot 

 

39.4 Spring Boot Shiro权限管理【从零开始学Spring Boot

 

39.3 Spring Boot Shiro权限管理【从零开始学Spring Boot

 

39.2. Spring Boot Shiro权限管理【从零开始学Spring Boot

 

39.1 Spring Boot Shiro权限管理【从零开始学Spring Boot

 

38 Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot 

 

37 Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot 

 

36 Spring Boot Cache理论篇【从零开始学Spring Boot

 

35 Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot 

 

34Spring Boot的启动器Starter详解【从零开始学Spring Boot

 

33 Spring Boot 监控和管理生产环境【从零开始学Spring Boot

 

32 Spring Boot使用@SpringBootApplication注解从零开始学Spring Boot 

 

31 Spring Boot导入XML配置【从零开始学Spring Boot

 

 

更多查看博客: http://412887952-qq-com.iteye.com/

 

 

à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

 

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

7
8
分享到:
评论
32 楼 林祥纤 2017-09-01  
YQuite 写道
YQuite 写道
YQuite 写道
        //不走缓存.  
        System.out.println(demoInfoService.findById(updated.getId())); 


这个走缓存了吧,在update的时候设置@CachePut,已经更新缓存了

更新了,而且没有删除缓存,所以这里走的确实是缓存。。。
如果update是用的@CacheEvict这个注解,那么System.out.println(demoInfoService.findById(updated.getId())); 就不会走缓存了,博主忘了改了呦!o(∩_∩)o

不对,不是没有删除缓存,是更新了缓存。。。哇


说的对。
31 楼 YQuite 2017-09-01  
YQuite 写道
YQuite 写道
        //不走缓存.  
        System.out.println(demoInfoService.findById(updated.getId())); 


这个走缓存了吧,在update的时候设置@CachePut,已经更新缓存了

更新了,而且没有删除缓存,所以这里走的确实是缓存。。。
如果update是用的@CacheEvict这个注解,那么System.out.println(demoInfoService.findById(updated.getId())); 就不会走缓存了,博主忘了改了呦!o(∩_∩)o

不对,不是没有删除缓存,是更新了缓存。。。哇
30 楼 YQuite 2017-09-01  
YQuite 写道
        //不走缓存.  
        System.out.println(demoInfoService.findById(updated.getId())); 


这个走缓存了吧,在update的时候设置@CachePut,已经更新缓存了

更新了,而且没有删除缓存,所以这里走的确实是缓存。。。
如果update是用的@CacheEvict这个注解,那么System.out.println(demoInfoService.findById(updated.getId())); 就不会走缓存了,博主忘了改了呦!o(∩_∩)o
29 楼 YQuite 2017-09-01  
        //不走缓存.  
        System.out.println(demoInfoService.findById(updated.getId())); 


这个走缓存了吧,在update的时候设置@CachePut,已经更新缓存了
28 楼 林祥纤 2017-03-09  
冰做的风铃 写道
对楼主这种无私奉献的精神表示无别的敬佩!



谢谢!
27 楼 冰做的风铃 2017-03-09  
对楼主这种无私奉献的精神表示无别的敬佩!
26 楼 林祥纤 2016-12-15  
wangyj0898 写道
幸苦了,文章写的真是不错。111


Thank you !
25 楼 wangyj0898 2016-12-15  
幸苦了,文章写的真是不错。111
24 楼 林祥纤 2016-12-11  
yangjigang 写道
确实好文,正在学习,感谢分享。



Thank you very much !
23 楼 yangjigang 2016-12-11  
确实好文,正在学习,感谢分享。
22 楼 林祥纤 2016-11-12  
happerlan 写道
感谢分享 感谢分享 感谢分享


happy
21 楼 happerlan 2016-11-10  
感谢分享 感谢分享 感谢分享
20 楼 hehuanhay 2016-10-21  
按照博主的教程,遇到一个异常。
博主是否遇到过这种情况:
java.lang.IllegalArgumentException: Cannot find cache named 'dictionary' for Builder[public java.util.List com.scbd.ocs.user.provider.serviceimp.DictionaryServiceImp.findAll()] caches=[dictionary] | key=''demoInfo'' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''

at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:81)
at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:242)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:671)
at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:255)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:577)


19 楼 林祥纤 2016-09-24  
chanzhq 写道
楼主,真是非常感谢,近期都在学习你的springBoot系列,简单易懂啊,官方文档实在看的懵逼...


哈哈,官方说的都是思路。
18 楼 林祥纤 2016-09-24  
z736732419 写道
必须点赞


Thx.
17 楼 chanzhq 2016-09-23  
楼主,真是非常感谢,近期都在学习你的springBoot系列,简单易懂啊,官方文档实在看的懵逼...
16 楼 z736732419 2016-09-23  
必须点赞
15 楼 林祥纤 2016-09-14  
hu1013 写道
版主,这个都没有走缓存,那什么时候能走缓存呢?


本文中的打印信息可能给你造成了一个误解了,没有走缓存,是指本次查询没有走缓存,之后的查询就走缓存了,所以也就没有打印信息“没有走缓存”了。
14 楼 hu1013 2016-09-13  
版主,这个都没有走缓存,那什么时候能走缓存呢?
13 楼 林祥纤 2016-08-23  
xingguang0106 写道
37和35的例子,我都去试了下,都报错,有点难受 


代码都是在本地严格测试过的,所以可能你的代码编写有误。

相关推荐

    从零开始学Spring Boot

    1.40 Spring Boot集成EHCache实现缓存机制 1.41 Spring Boot分布式Session状态保存Redis 1.42 Spring Boot Shiro权限管理 1.43 Spring Boot Shiro权限管理 1.44 Spring Boot Shiro权限管理 1.45 Spring Boot Shiro...

    Spring Boot中使用EhCache实现缓存支持

    Spring Boot中使用EhCache实现缓存支持,介绍整合方法和配置

    SpringBoot 集成Ehcache实现缓存

    使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。

    详解spring boot集成ehcache 2.x 用于hibernate二级缓存

    本篇文章主要介绍了详解spring boot集成ehcache 2.x 用于hibernate二级缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    SpringBoo2.x,整合Ehcache3.x

    SpringBoo2.x整合Ehcache3.x作为缓存底层存储。

    spring3整合EhCache注解实例

    spring3整合EhCache注解实例

    spring-boot整合ehcache实现缓存机制的方法

    spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。这篇文章主要介绍了spring-boot整合ehcache实现缓存机制,需要的朋友可以参考下

    Spring Boot的EhCache缓存使用.docx

    当我们不指定具体其他第三方实现的时候,Spring Boot的Cache模块会使用ConcurrentHashMap来存储。而实际生产使用的时候,因为我们可能需要更多其他特性,往往就会采用其他缓存框架,所以接下来我们会分几篇分别介绍...

    Spring+Ehcache集成

    基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!

    Spring AOP+ehCache简单缓存系统解决方案

    需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法...

    Spring4 整合EhCache实现 页面缓存 零配置

    以java配置类方式 整合EhCache来实现页面整体缓存及页面局部缓存。详情见博客http://blog.csdn.net/poorCoder_/article/details/56483954

    spring boot 集成activemq Datajpa Ehcache

    spring boot 集成activemq Datajpa Ehcache 里面有详细的例子

    Spring Boot 2.x的EhCache缓存的使用问题详解.docx

    除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。我们也可以通过debug调试查看cacheManager对象的实例来判断当前使用了什么缓存。在上一篇中,我们也展示了如何去查看当前使用情况。

    Spring Boot整合EhCache的步骤详解

    EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。这篇文章主要介绍了Spring Boot整合EhCache的步骤详解,需要的朋友可以参考下

    Ehcache分布式缓存与其在SpringBoot应用

    EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和...

    Spring Boot集成Ehcache缓存解决方式

    在本篇文章里小编给大家整理的是关于Spring Boot集成Ehcache缓存解决方式,需要的朋友们可以学习下。

    Spring boot hibernate ehcache maven 整合源码

    Spring-boot,hibernate ehcache 整合源码,可直接运行

    spring-boot-reference.pdf

    Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...

    借助Ehcache缓存框架实现对页面的缓存Demo

    本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-...

Global site tag (gtag.js) - Google Analytics