一、springboot缓存简介
在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者:
* Generic
* JCache (JSR-107)
* EhCache 2.x
* Hazelcast
* Infinispan
* Redis
* Guava
* Simple
关于 Spring Boot 的缓存机制:
高速缓存抽象不提供实际存储,并且依赖于由org.springframework.cache.Cache和org.springframework.cache.CacheManager接口实现的抽象。 Spring Boot根据实现自动配置合适的CacheManager,只要缓存支持通过@EnableCaching注释启用即可。
二、实例
该实例是多模块下的项目,详情见教程(四),这里采用ehcache
步骤一:在service模块的pom.xml添加以下
<!-- caching -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
步骤二:新建配置类文件(注意启动类的扫描范围,可自定义扫描),放在service模块config包中
package com.wqc.service.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;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache_config.xml")); // 缓存信息配置文件
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
/**
* ehcache 主要的管理器
*
* @param cacheManagerFactoryBean
* @return
*/
@Bean
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean cacheManagerFactoryBean) {
return new EhCacheCacheManager(cacheManagerFactoryBean.getObject());
}
}
步骤三:service模块下的resources添加ehcache_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false">
<diskStore path="e:/storage/project/demo/ehcache"/>
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="FIFO" />
<cache name="userCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="FIFO" />
</ehcache>
步骤四:在service模块的service中的方法分别添加注解
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import org.springframework.transaction.annotation.Transactional;
import com.wqc.data.dao.IUserDb;
import com.wqc.data.model.User;
import com.wqc.service.IUserService;
@Service
public class UserService implements IUserService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
@Autowired
private IUserDb userDb;
@Cacheable(key="'user_all'",value="userCache")
@Override
public List<User> getUsers() {
LOGGER.info("查询数据库...");
return userDb.getUsers();
}
@CachePut(key="'user_'+#user.uid", value="userCache")
@Override
public void update(User user) {
userDb.update(user);
}
@CacheEvict(key="'user_'+#uid", value="userCache")
@Transactional
@Override
public void del(int id) {
userDb.del(id);
}
@CacheEvict(key="'user'",value="userCache")
@Override
public void save(User user) {
userDb.save(user);
}
}
以上四个步骤即可实现缓存...