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配置如下
其他地方不需要做任何修改,只需要将配置文件中的缓存配置替换即可,springboot将会使用redis缓存。代码中缓存使用的地方不需要做任何改变。#指定使用的缓存类型,参照CacheType类,禁用取none spring.cache.type=redis #指定缓存配置文件位置 spring.cache.cache-names=userCache,sysCache #缓存有效时间,单位毫秒 spring.cache.redis.time-to-live=600000
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/>标签。
Spring-Boot (四) cache/ehcache/redis-cache集成使用
最新推荐文章于 2024-05-24 16:23:43 发布