
画龙点睛
随着时间的累积,应用程序的用户数量继续增加,数据规模也在增加. 通常,查询操作将成为影响用户体验的瓶颈. 此时使用缓存通常是解决此问题的一个很好的解决方案. 一种手段.
Spring 3开始提供强大的基于注释的缓存支持. 您可以通过注释配置以低侵入性的方式将缓存功能添加到原始Spring应用程序中,以提高数据访问性能.
Spring Boot对缓存的支持提供了一系列自动化配置,使我们可以非常方便地使用缓存.
在这里,我们使用一个简单的示例来说明如何向现有应用程序添加缓存功能.
第二次实战
1介绍依赖关系
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--引入cache依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--引入EhCache缓存-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
</dependencies>

2向数据访问界面添加了缓存配置注释
package com.didispace.domain;
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.data.jpa.repository.JpaRepository;
//在数据访问接口中,增加缓存配置注解
@CacheConfig(cacheNames = "users")
public interface UserRepository extends JpaRepository<User, Long> {
@Cacheable(key = "#p0", condition = "#p0.length() < 10")
User findByName(String name);
}
3个实体类
package com.didispace.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author 程序猿DD
* @version 1.0.0
* @date 16/3/21 下午3:35.
* @blog http://blog.didispace.com
*/
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer age;
public User(){}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
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;
}
}
4个启动类
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
//@EnableCaching注解开启缓存功能
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.properties.hibernate.show_sql=true

6ehcache.xml配置
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="users"
maxEntriesLocalHeap="200"
timeToLiveSeconds="600">
</cache>
</ehcache>
7个测试班
package com.didispace;
import com.didispace.domain.User;
import com.didispace.domain.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
@Autowired
private UserRepository userRepository;
@Autowired
private CacheManager cacheManager;
@Before
public void before() {
userRepository.save(new User("AAA", 10));
}
@Test
public void test() throws Exception {
User u1 = userRepository.findByName("AAA");
System.out.println("第一次查询:" + u1.getAge());
User u2 = userRepository.findByName("AAA");
System.out.println("第二次查询:" + u2.getAge());
u1.setAge(20);
userRepository.save(u1);
User u3 = userRepository.findByName("AAA");
System.out.println("第三次查询:" + u3.getAge());
}
}
三个测试
调用第二个findByName函数时,不执行select语句,并且直接减少了读取操作.

四个实战指示
![]()
1缓存注释的详细说明
value,cacheNames: 两个等效参数(在Spring 4中将cacheNames作为值的别名添加),用于指定缓存存储的集合名称. 自从在Spring 4中添加@CacheConfig以来,Spring 3中最初需要的value属性也变得不再重要.
key: 存储在Map集合中的缓存对象的键值. 没有必要. 默认情况下,该功能的所有参数组合均用作键值. 如果您自行配置,则需要使用SpEL表达式,例如: @Cacheable(key =“#p0”): 使用函数的第一个参数作为缓存的键值
condition: 缓存对象的条件. 没有必要. 还需要SpEL表达式. 仅满足表达式条件的内容将被缓存,例如: @Cacheable(key =“#p0”,condition =“#p0. length()<3”),这意味着仅当长度为第一个参数小于3,如果执行此配置,则不会缓存AAA用户,读者可以自己进行实验.
除非: 另一个缓存条件参数(不是必需的)ehcache 注解使用,否则是SpEL表达式. 它在判断时机上与条件参数不同. 该条件是在调用函数后判断的,因此可以通过结果来判断.
keyGenerator: 用于指定密钥,不是必需的. 如果需要指定自定义密钥,则需要实现org.springframework.cache.interceptor.KeyGenerator接口,并使用此参数进行指定. 请注意,此参数和键是互斥的
cacheManager: 用于指定要使用的缓存管理器,不是必需的. 仅在有多个时才需要使用
cacheResolver: 用于指定要使用的缓存解析器,不是必需的. 您需要通过org.springframework.cache.interceptor.CacheResolver接口实现自己的缓存解析器,并使用此参数进行指定.

除了此处使用的两个注释外,还有以下核心注释:
allEntries: 不需要,默认为false. 设置为true时,将删除所有数据
beforeInvocation: 不是必需的ehcache 注解使用,默认值为false,在调用该方法后将删除数据. 设置为true时,将在调用该方法之前删除数据.
2缓存配置
Spring Boot使用哪种缓存?
通过Spring Boot中的@EnableCaching注释自动配置适当的缓存管理器(CacheManager). Spring Boot按照以下顺序检测缓存提供程序:
除了顺序检测外,我们还可以通过配置spring.cache.type属性来指定它.
我们可以使用debug来查看cacheManager对象的实例,以确定当前使用的缓存.
五个参考文献
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-241673-1.html
从目前美舰已闯进12海里的事实看
其它先不说