前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot入门建站全系列(十三)本地缓存的使用(Ehcache和caffeine的使用)

SpringBoot入门建站全系列(十三)本地缓存的使用(Ehcache和caffeine的使用)

原创
作者头像
品茗IT
修改2019-08-15 16:59:35
1.1K0
修改2019-08-15 16:59:35
举报
文章被收录于专栏:品茗IT品茗IT

SpringBoot入门建站全系列(十三)本地缓存的使用(Ehcache和caffeine的使用)

一、概述

本地缓存,就是使用应用内使用本地内存将数据暂缓存储,一般数据库的查询如果不怎么改动,可以用本地缓存暂存。

远程缓存,比如redis,就是第三方缓存服务器,不是在当前应用的,需要用过tcp请求去获得的缓存。

Springboot的官方文档中列出了9种:

  • Generic
  • JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)
  • EhCache 2.x
  • Hazelcast
  • Infinispan
  • Couchbase
  • Redis
  • Caffeine
  • Simple

本地缓存常用的是Ehcache,很早就出现了,用的很广泛,是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider.

Caffeine是使用Java8对Guava缓存的重写版本,有人称它为缓存之王,虽然我不知道为啥这么称呼它。我没做过性能测试哦。

本文假设你已经引入spring-boot-starter-web。已经是个SpringBoot项目了,如果不会搭建,可以打开这篇文章看一看《SpringBoot入门建站全系列(一)项目建立》

二、Spring缓存的一些概念

缓存的注解包含:

@Cacheable:加入缓存,使用前查询缓存。

@CacheEvict:清除缓存

@CachePut:每次都执行方法,并直接存入数据到缓存,使用前不查询缓存

@Caching:可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。

缓存的key是有讲究的,Springboot里面没讲,Spring占用了一大篇幅去说它,这里就不多说了,官方文档在这儿:

https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/integration.html#cache

三、Ehcache

3.1 Maven依赖

这里要访问数据库进行缓存,所以要依赖数据库相关jar包,同时要依赖ehcache。

代码语言:txt
复制
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache</artifactId>
</dependency>
3.2 配置文件

在application.properties 中需要添加下面的配置:

代码语言:txt
复制
spring.cache.ehcache.config=classpath:ehcache.xml

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=60000
spring.datasource.dbcp2.min-idle=20
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.test-on-return=false

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=cff
spring.datasource.password=123456

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

这里的spring.cache.ehcache.config指定了ehcache的配置文件位置。

ehcache.xml:

代码语言:txt
复制
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

  <!-- 磁盘缓存位置 -->
  <diskStore path="java.io.tmpdir/ehcache"/>

  <!-- 默认缓存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU"/>

  <!-- userCache缓存 -->
  <cache name="userCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="0"
         timeToLiveSeconds="1000"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>

这里配置了一个defaultCache和一个userCache。不多说了,顾名思义的配置。

3.3 开启注解并使用

需要使用@EnableCaching开启注解。使用@Cacheable注解需要缓存的service。

EhcacheService:

代码语言:txt
复制
package com.cff.springbootwork.ehcache.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import com.cff.springbootwork.mybatis.domain.UserInfo;
import com.cff.springbootwork.mybatis.service.UserInfoService;

@Service
@EnableCaching
public class EhcacheService {
	@Autowired
	UserInfoService userInfoService;

	@Cacheable(value = "userCache", key = "#root.targetClass.simpleName+'-'+#root.methodName+'-'+#userName")
	public UserInfo getUserInfoByUserName(String userName) {
		return userInfoService.getUserInfoByUserName(userName);
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

}
3.4 测试Web
代码语言:txt
复制
package com.cff.springbootwork.ehcache.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cff.springbootwork.ehcache.service.EhcacheService;
import com.cff.springbootwork.mybatis.domain.UserInfo;

@RestController
@RequestMapping("/ehcache")
public class EhcacheRest {

	@Autowired
	private EhcacheService ehcacheService;

	@RequestMapping(value = "/test/{name}", method = { RequestMethod.GET })
	public UserInfo test(@PathVariable("name") String name) {
		return ehcacheService.getUserInfoByUserName(name);
	}

}

四、Caffeine

Caffeine配置起来相对简单点,没有xml这样的配置。

4.1 Maven依赖
代码语言:txt
复制
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
	<groupId>com.github.ben-manes.caffeine</groupId>
	<artifactId>caffeine</artifactId>
</dependency>
4.2 配置文件

在application.properties 中需要添加下面的配置:

代码语言:txt
复制
spring.cache.type=caffeine
spring.cache.cache-names=userCache

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=60000
spring.datasource.dbcp2.min-idle=20
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.test-on-return=false

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=cff
spring.datasource.password=123456

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

这里的spring.cache.type指定了cache是用caffeine,spring.cache.cache-names=userCache是建立了一个名字为userCache的cache。

4.3 开启注解并使用

需要使用@EnableCaching开启注解。使用@Cacheable注解需要缓存的service。

CaffeineService :

代码语言:txt
复制
package com.cff.springbootwork.caffeine.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import com.cff.springbootwork.mybatis.domain.UserInfo;
import com.cff.springbootwork.mybatis.service.UserInfoService;

@Service
@EnableCaching
public class CaffeineService {
	@Autowired
	UserInfoService userInfoService;

	@Cacheable(value = "userCache", key = "#root.targetClass.simpleName+'-'+#root.methodName+'-'+#userName")
	public UserInfo getUserInfoByUserName(String userName) {
		return userInfoService.getUserInfoByUserName(userName);
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

}
4.4 测试Web

CaffeineRest :

代码语言:txt
复制
package com.cff.springbootwork.caffeine.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cff.springbootwork.caffeine.service.CaffeineService;
import com.cff.springbootwork.mybatis.domain.UserInfo;

@RestController
@RequestMapping("/caffeine")
public class CaffeineRest {

	@Autowired
	private CaffeineService caffeineService;

	@RequestMapping(value = "/test/{name}", method = { RequestMethod.GET })
	public UserInfo test(@PathVariable("name") String name) {
		return caffeineService.getUserInfoByUserName(name);
	}

}

五、数据访问相关文件

UserInfoService:

代码语言:txt
复制
package com.cff.springbootwork.mybatis.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cff.springbootwork.mybatis.dao.UserInfoDao;
import com.cff.springbootwork.mybatis.domain.UserInfo;

@Service
public class UserInfoService {
	@Autowired
	UserInfoDao userInfoDao;
	public UserInfo getUserInfoByUserName(String userName){
		return userInfoDao.findByUserName(userName);
	}
}

UserInfoDao :

UserInfo :

详细完整的实体,可以访问品茗IT-博客《SpringBoot入门建站全系列(十三)本地缓存的使用(Ehcache和caffeine的使用)》进行查看

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot入门建站全系列(十三)本地缓存的使用(Ehcache和caffeine的使用)
    • 一、概述
      • 二、Spring缓存的一些概念
        • 三、Ehcache
          • 3.1 Maven依赖
          • 3.2 配置文件
          • 3.3 开启注解并使用
          • 3.4 测试Web
        • 四、Caffeine
          • 4.1 Maven依赖
          • 4.2 配置文件
          • 4.3 开启注解并使用
          • 4.4 测试Web
        • 五、数据访问相关文件
          相关产品与服务
          网站建设
          网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档