SpringBoot 15 ehcache

1:修改pom.xml文件,添加依赖

<!-- Spring Boot 缓存支持启动器 -->
        <dependency> 
        	<groupId>org.springframework.boot</groupId> 
        	<artifactId>spring-boot-starter-cache</artifactId> 
        </dependency>
        <dependency> 
        	<groupId>net.sf.ehcache</groupId> 
        	<artifactId>ehcache</artifactId>
		</dependency>

2:添加配置文件

在src/main/resources下创建ehcache.xml文件,添加配置。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
	
	<diskStore path="java.io.tmpdir" /> 
	
	<!--defaultCache:echcache 的默认缓存策略 -->
	<defaultCache maxElementsInMemory="10000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120"
		maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
		<persistence strategy="localTempSwap" />
	</defaultCache> 
	
	<!-- 自定义缓存策略 -->
	<cache name="users" maxElementsInMemory="10000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120"
		maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
		<persistence strategy="localTempSwap" />
	</cache>
</ehcache>

 

3:修改application.yml文件

spring: 
    cache: 
      ehcache:
         config: ehcache.xml

4:修改启动类,开启缓存。

@EnableCaching  开启缓存

package com.zhaoy;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
@MapperScan(basePackages = {"com.zhaoy.mapper"})
@EnableCaching
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

 

5:实体类持久化

实体类需要进行持久化才能进行缓存。

package com.zhaoy.entity;

import java.io.Serializable;

import lombok.Data;

@Data
public class Comment implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private String productId;
	private String commentTime;
	private Boolean isDeleted;
}

6:业务层使用缓存

需使用@Cacheable(value = "users")使用缓存,若不指定value,则使用默认的缓存配置。

package com.zhaoy.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
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.zhaoy.entity.Comment;
import com.zhaoy.mapper.CommentMapper;



@Service
public class CommentService {
	
	@Autowired
	private CommentMapper commentMapper;
	
	@CacheEvict
	@Transactional
	public int insertComment(String id, String productId, String commentTime, Boolean isDeleted) {
		int insertComment = commentMapper.insertComment(id,productId,commentTime,isDeleted);
		return insertComment;
	}
	
	@Cacheable(value = "users")
	public PageInfo<Comment> selCommentList(int pageNum, int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<Comment> list = commentMapper.selCommentList();
		PageInfo<Comment> pageInfo = new PageInfo<Comment>(list);
		return pageInfo;
	}
}

7:@Cacheable与@CacheEvict

@Cacheable:把方法的返回值添加到Ehcache中做缓存。

@@CacheEvict:清除缓存,保证数据一致性。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值