Spring-Boot (四) cache/ehcache/redis-cache集成使用

Spring-Boot集成Cache。官文:当当当当


缓存集成引入:

spring cache

application.properties中的配置:spring-boot-starter-cache,在无其他第三方缓存实现依赖被引入情况下,默认使用spring cache,采用concurrentMap实现的缓存,在SpringBootApplication入口类上添加@EnableCaching注解即可在service方法内使用spring缓存注解,生产环境中不推荐采用此形式。


starter引入后,默认自动检测是否存在redis、ehcache等第三方实现(即是否引入依赖),如果存在,则自动配置cacheManager,并启用第三方缓存实现,在代码中如无需个性化配置情况下,则只需要在SpringBootApplication入口类上添加@EnableCaching注解即可。service曾缓存的使用,就是spring得缓存注解如:@Cacheable,@Evict, @Caching...


实际使用过程中往往需要自定义配置,如自定义缓存时间,缓存名称等等。


ehcache缓存集成及使用(引入spring-boot-starter-cache和ehcache)

application.proerties配置如下

#指定使用的缓存类型,参照CacheType类,禁用取none
spring.cache.type=ehcache
#指定缓存配置文件位置
spring.cache.ehcache.config=classpath:cache/ehcache-local.xml

然后再SpringBootApplication入口类添加注解@EnableCaching

package com.tom;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableCaching 
@SpringBootApplication ///@SpringBootApplication 注解代替了 @Configuration, @EnableAutoConfiguration, and @ComponentScan
public class MySpringBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(MySpringBootApplication.class, args);
	}
}

在Service层使用注解:

package com.tom.service.impl;

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

import com.tom.bean.Employee;
import com.tom.dao.IEmployeeDao;
import com.tom.service.IEmployeeService;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

	@Autowired
	private IEmployeeDao dao;
	
	@Cacheable(value="userCache", key="'employee_'+#id")
	@Override
	@Transactional(readOnly=true)
	public Employee findOne(int id) {
		return this.dao.get(id);
	}

}
 

controller层调用时即会自动使用ehcache缓存注解。


redis缓存使用(引入spring-boot-starter-data-redis)

application.proerties配置如下

#指定使用的缓存类型,参照CacheType类,禁用取none
spring.cache.type=redis
#指定缓存配置文件位置
spring.cache.cache-names=userCache,sysCache
#缓存有效时间,单位毫秒
spring.cache.redis.time-to-live=600000
其他地方不需要做任何修改,只需要将配置文件中的缓存配置替换即可,springboot将会使用redis缓存。代码中缓存使用的地方不需要做任何改变。


CacheManager的其他设置

Spring-Boot提供了在cacheManager未完全初始化前的一些设置操作,只需要implements CacheManagerCustomizer,可以在实现customize方法中对cacheManager进行设置,对于customizer的类定义和注入到bean,可以定义多个,数量无限制,启用哪个缓存就会调用哪个缓存的个性化配置。

例如:

package com.tom.cache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.DefaultRedisCachePrefix;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCachePrefix;

/**CacheManagerCustomizer可以在cacheManager未完全初始化前做一些配置操作<br/>
 * 可以定义多个。<br/>
 * 使用时,在application.properties内定义启用哪个作为cache即可。使用哪个cache将会初始化那个cacheManager
 * @author tomZ
 * @date 2017年11月13日
 * @desc TODO
 */
@Configuration
@EnableCaching
public class CacheManagerCustomizerInitializer  {
	
	private static Logger logger = LoggerFactory.getLogger(CacheManagerCustomizerInitializer.class);
	
	@Bean
	public  CacheManagerCustomizer<EhCacheCacheManager> ehcacheManagerCustomizer() {
		return new CacheManagerCustomizer<EhCacheCacheManager>() {

			@Override
			public void customize(EhCacheCacheManager cacheManager) {
				//自定义设置
				logger.info("ehcache cacheManager customizer.");
			}
		};
	}
	
	
	
	@Bean
	public  CacheManagerCustomizer<RedisCacheManager> redisManagerCustomizer() {
		return new CacheManagerCustomizer<RedisCacheManager>() {
			
			@Override
			public void customize(RedisCacheManager cacheManager) {
				logger.info("redis cacheManager customizer.");
				//自定义设置
				//启用前缀
				cacheManager.setUsePrefix(true);
				//设置前缀为
				RedisCachePrefix cachePrefix = new DefaultRedisCachePrefix("u_");
				cacheManager.setCachePrefix(cachePrefix );
			}
		};
	}
	

}

上面就是对cacheManager进行一些初始化配置操作

如果自定义了customizer类,@EnableCaching,则在SpringBootAppcliation的入口类上就不需要添加该注解了。@EnableCaching注解相当于原SpringMVC配置中的<cache:annotation-driven/>标签。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值