引子
我门知道一个程序的瓶颈在于数据库,我门也知道内存的速度是大大快于硬盘的速度的。当我门需要重复的获取相同的数据的时候,我门一次又一次的请求数据库或者远程服务,导致大量的时间耗费在数据库查询或者远程方法的调用上,导致程序性能的恶化,这更是数据缓存要解决的问题。
spring 缓存支持
spring定义了 org.springframework.cache.cachemanager和org.springframework.cache.cache接口来统一不同的缓存技术。其中,cachemanager是spring提供的各种缓存技术抽象接口,cache接口包含了缓存的各种操作(增加、删除获得缓存,我门一般不会直接和此接口打交道)
spring 支持的cachemanager
针对不同的缓存技术,需要实现不同的cachemanager ,spring 定义了如下表的cachemanager实现。
实现任意一种cachemanager 的时候,需要注册实现cachemanager的bean,当然每种缓存技术都有很多额外的配置,但配置cachemanager 是必不可少的。
声明式缓存注解
spring提供了4个注解来声明缓存规则(又是使用注解式的aop的一个生动例子),如表。
开启声明式缓存
开启声明式缓存支持非常简单,只需要在配置类上使用@enabelcaching 注解即可。
springboot 的支持
在spring中国年使用缓存技术的关键是配置cachemanager 而springbok 为我门自动配置了多个cachemanager的实现。在spring boot 环境下,使用缓存技术只需要在项目中导入相关缓存技术的依赖包,并配置类使用@enabelcaching开启缓存支持即可。
小例子
小例子是使用 springboot+jpa +cache 实现的。
实例步骤目录
- 创建maven项目
- 数据库配置
- jpa配置和cache配置
- 编写bean 和dao层
- 编写service层
- 编写controller
- 启动cache
- 测试校验
1.创建maven项目
新建maven 项目pom.xml文件如下内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?xml version= "1.0" encoding= "utf-8" ?>
<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.us</groupid>
<artifactid>springboot-cache</artifactid>
<version> 1.0 -snapshot</version>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version> 1.3 . 0 .release</version>
</parent>
<properties>
<start- class >com.us.application</start- class >
<maven.compiler.target> 1.8 </maven.compiler.target>
<maven.compiler.source> 1.8 </maven.compiler.source>
</properties>
<!-- add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-cache</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupid>net.sf.ehcache</groupid>
<artifactid>ehcache</artifactid>
</dependency>
<!--db-->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version> 6.0 . 5 </version>
</dependency>
<dependency>
<groupid>com.mchange</groupid>
<artifactid>c3p0</artifactid>
<version> 0.9 . 5.2 </version>
<exclusions>
<exclusion>
<groupid>commons-logging</groupid>
<artifactid>commons-logging</artifactid>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
|
2.数据库配置
在src/main/esouces目录下新建application.properties 文件,内容为数据库连接信息,如下:
application.properties
1
2
3
4
5
|
ms.db.driverclassname=com.mysql.jdbc.driver
ms.db.url=jdbc:mysql: //localhost:3306/cache?prepstmtcachesize=517&cacheprepstmts=true&autoreconnect=true&useunicode=true&characterencoding=utf-8&usessl=false&allowmultiqueries=true
ms.db.username=root
ms.db.password=xxxxxx
ms.db.maxactive= 500
|
新建dbconfig.java 配置文件,配置数据源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.us.example.config;
/**
* created by yangyibo on 17/1/13.
*/
import java.beans.propertyvetoexception;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.env.environment;
import com.mchange.v2.c3p0.combopooleddatasource;
@configuration
public class dbconfig {
@autowired
private environment env;
@bean (name= "datasource" )
public combopooleddatasource datasource() throws propertyvetoexception {
combopooleddatasource datasource = new combopooleddatasource();
datasource.setdriverclass(env.getproperty( "ms.db.driverclassname" ));
datasource.setjdbcurl(env.getproperty( "ms.db.url" ));
datasource.setuser(env.getproperty( "ms.db.username" ));
datasource.setpassword(env.getproperty( "ms.db.password" ));
datasource.setmaxpoolsize( 20 );
datasource.setminpoolsize( 5 );
datasource.setinitialpoolsize( 10 );
datasource.setmaxidletime( 300 );
datasource.setacquireincrement( 5 );
datasource.setidleconnectiontestperiod( 60 );
return datasource;
}
}
|
数据库设计,数据库只有一张person表,设计如下:
3.jpa配置
spring-data- jpa 配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.us.example.config;
import java.util.hashmap;
import java.util.map;
import javax.persistence.entitymanagerfactory;
import javax.sql.datasource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import org.springframework.data.jpa.repository.config.enablejparepositories;
import org.springframework.orm.jpa.jpatransactionmanager;
import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean;
import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter;
import org.springframework.transaction.platformtransactionmanager;
import org.springframework.transaction.annotation.enabletransactionmanagement;
/**
* created by yangyibo on 17/1/13.
*/
@configuration
@enablejparepositories ( "com.us.example.dao" )
@enabletransactionmanagement
@componentscan
public class jpaconfig {
@autowired
private datasource datasource;
@bean
public entitymanagerfactory entitymanagerfactory() {
hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter();
localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean();
factory.setjpavendoradapter(vendoradapter);
factory.setpackagestoscan( "com.us.example.bean" );
factory.setdatasource(datasource);
map<string, object> jpaproperties = new hashmap<>();
jpaproperties.put( "hibernate.ejb.naming_strategy" , "org.hibernate.cfg.improvednamingstrategy" );
jpaproperties.put( "hibernate.jdbc.batch_size" , 50 );
factory.setjpapropertymap(jpaproperties);
factory.afterpropertiesset();
return factory.getobject();
}
@bean
public platformtransactionmanager transactionmanager() {
jpatransactionmanager txmanager = new jpatransactionmanager();
txmanager.setentitymanagerfactory(entitymanagerfactory());
return txmanager;
}
}
|
4.编写bean 和dao层
实体类 person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package com.us.example.bean;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.id;
import javax.persistence.table;
/**
* created by yangyibo on 17/1/13.
*/
@entity
@table (name = "person" )
public class person {
@id
@generatedvalue
private long id;
private string name;
private integer age;
private string address;
public person() {
super ();
}
public person( long id, string name, integer age, string address) {
super ();
this .id = id;
this .name = name;
this .age = age;
this .address = address;
}
public long getid() {
return id;
}
public void setid( long id) {
this .id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this .name = name;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this .age = age;
}
public string getaddress() {
return address;
}
public void setaddress(string address) {
this .address = address;
}
}
|
dao层,personrepository.java
1
2
3
4
5
6
7
8
9
10
|
package com.us.example.dao;
import com.us.example.bean.person;
import org.springframework.data.jpa.repository.jparepository;
/**
* created by yangyibo on 17/1/13.
*/
public interface personrepository extends jparepository<person, long > {
}
|
5.编写service层
service 接口
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.us.example.service;
import com.us.example.bean.person;
/**
* created by yangyibo on 17/1/13.
*/
public interface demoservice {
public person save(person person);
public void remove( long id);
public person findone(person person);
}
|
实现:(重点,此处加缓存)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.us.example.service.impl;
import com.us.example.bean.person;
import com.us.example.dao.personrepository;
import com.us.example.service.demoservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.cache.annotation.cacheput;
import org.springframework.cache.annotation.cacheable;
import org.springframework.stereotype.service;
/**
* created by yangyibo on 17/1/13.
*/
@service
public class demoserviceimpl implements demoservice {
@autowired
private personrepository personrepository;
@override
//@cacheput缓存新增的或更新的数据到缓存,其中缓存名字是 people 。数据的key是person的id
@cacheput (value = "people" , key = "#person.id" )
public person save(person person) {
person p = personrepository.save(person);
system.out.println( "为id、key为:" +p.getid()+ "数据做了缓存" );
return p;
}
@override
//@cacheevict 从缓存people中删除key为id 的数据
@cacheevict (value = "people" )
public void remove( long id) {
system.out.println( "删除了id、key为" +id+ "的数据缓存" );
//这里不做实际删除操作
}
@override
//@cacheable缓存key为person 的id 数据到缓存people 中,如果没有指定key则方法参数作为key保存到缓存中。
@cacheable (value = "people" , key = "#person.id" )
public person findone(person person) {
person p = personrepository.findone(person.getid());
system.out.println( "为id、key为:" +p.getid()+ "数据做了缓存" );
return p;
}
}
|
6.编写controller
为了测试方便请求方式都用了get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.us.example.controller;
import com.us.example.bean.person;
import com.us.example.service.demoservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.bind.annotation.restcontroller;
/**
* created by yangyibo on 17/1/13.
*/
@restcontroller
public class cachecontroller {
@autowired
private demoservice demoservice;
//http://localhost:8080/put?name=abel&age=23&address=shanghai
@requestmapping ( "/put" )
public person put(person person){
return demoservice.save(person);
}
//http://localhost:8080/able?id=1
@requestmapping ( "/able" )
@responsebody
public person cacheable(person person){
return demoservice.findone(person);
}
//http://localhost:8080/evit?id=1
@requestmapping ( "/evit" )
public string evit( long id){
demoservice.remove(id);
return "ok" ;
}
}
|
7.启动cache
启动类中要记得开启缓存配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.us.example;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.context.annotation.componentscan;
import static org.springframework.boot.springapplication.*;
/**
* created by yangyibo on 17/1/13.
*/
@componentscan (basepackages = "com.us.example" )
@springbootapplication
@enablecaching
public class application {
public static void main(string[] args) {
configurableapplicationcontext run = run(application. class , args);
}
}
|
8.测试校验检验able:
启动application 类,启动后在浏览器输入:http://localhost:8080/able?id=1(首先要在数据库中初始化几条数据。)
控制台输出:
“为id、key为:1数据做了缓存“ 此时已经为此次查询做了缓存,再次查询该条数据将不会出现此条语句,也就是不查询数据库了。
检验put
在浏览器输入:http://localhost:8080/put?name=abel&age=23&address=shanghai(向数据库插入一条数据,并将数据放入缓存。)
此时控制台输出为该条记录做了缓存:
然后再次调用able 方法,查询该条数据,将不再查询数据库,直接从缓存中读取数据。
测试evit
在浏览器输入:http://localhost:8080/evit?id=1(将该条记录从缓存中清楚,清除后,在次访问该条记录,将会重新将该记录放入缓存。)
控制台输出:
切换缓存
1.切换为ehcache作为缓存
pom.xml 文件中添加依赖
1
2
3
4
|
<dependency>
<groupid>net.sf.ehcache</groupid>
<artifactid>ehcache</artifactid>
</dependency>
|
在resource 文件夹下新建ehcache的配置文件ehcache.xml 内容如下,此文件spring boot 会自动扫描
1
2
3
4
5
|
<?xml version= "1.0" encoding= "utf-8" ?>
<ehcache>
<!--切换为ehcache 缓存时使用-->
<cache name= "people" maxelementsinmemory= "1000" />
</ehcache>
|
2.切换为guava作为缓存
只需要在pom中添加依赖
1
2
3
4
5
|
<dependency>
<groupid>com.google.guava</groupid>
<artifactid>guava</artifactid>
<version> 18.0 </version>
</dependency>
|
3.切换为redis作为缓存
请看下篇博客
本文参考:《javaee开发的颠覆者:spring boot实战 》
本文源代码:https://github.com/527515025/springboot.git
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u012373815/article/details/54564076/