一、SpringBoot整合Ehhcache
添加maven依賴
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-cache</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>net.sf.ehcache</groupId>
7 <artifactId>ehcache</artifactId>
8 </dependency>
創建Ehcache配置文件src/main/resources/ehcache.xml
1 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
2 <diskStore path="java.io.tmpdir"/>
3 <!--defaultCache:echcache 的默認緩存策略-->
4 <defaultCache 5 maxElementsInMemory="10000"
6 eternal="false"
7 timeToIdleSeconds="120"
8 timeToLiveSeconds="120"
9 maxElementsOnDisk="10000000"
10 diskExpiryThreadIntervalSeconds="120"
11 memoryStoreEvictionPolicy="LRU">
12 <persistence strategy="localTempSwap"/>
13 </defaultCache>
14 <!-- 自定義緩存策略-->
15 <cache name="user"
16 maxElementsInMemory="10000"
17 eternal="false"
18 timeToIdleSeconds="120"
19 timeToLiveSeconds="120"
20 maxElementsOnDisk="10000000"
21 diskExpiryThreadIntervalSeconds="120"
22 memoryStoreEvictionPolicy="LRU">
23 <persistence strategy="localTempSwap"/>
24 </cache>
25 </ehcache>
在application.properties中添加
spring.cache.ehcache.config=ehcache.xml
在啟動類上加注解
@EnableCaching
在業務層可以使用注解緩存數據
1 @Override 2 @Cacheable(value="user") 3 public User findUserById(Integer id) { 4 return this.userMapper.findUserById(id); 5 }
@Cacheable的作用就是把方法的返回值添加到ehcache緩存中,value屬性為ehcache配置文件中的緩存策略,不設定使用默認。
要求實體類需要實現Serializable接口。
@CacheEvict為清除緩存
1 @Override 2 @CacheEvict(value="user", allEntries=true) 3 public void saveUser(User user) { 4 this.userMapper.save(user); 5 }
二、SpringBoot整合Redis
SpringDataRedis是屬於SpringData下的一個模塊,作用是簡化redis的操作。添加mvaen依賴:
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-data-redis</artifactId>
4 </dependency>
編寫SpringDataRedis配置類
1 @Configuration 2 public class RedisConfig { 3 @Bean 4 public JedisPoolConfig jedisPoolConfig() { 5 JedisPoolConfig config = new JedisPoolConfig(); 6 config.setMaxIdle(10); 7 config.setMinIdle(5); 8 config.setMaxTotal(20); 9 return config; 10 } 11 @Bean 12 public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) { 13 JedisConnectionFactory factory = new JedisConnectionFactory(); 14 factory.setPoolConfig(config); 15 factory.setHostName("127.0.0.1"); 16 factory.setPort(6379); 17 return factory; 18 } 19 @Bean 20 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { 21 RedisTemplate<String, Object> template = new RedisTemplate<>(); 22 template.setConnectionFactory(factory); 23 template.setKeySerializer(new StringRedisSerializer()); 24 template.setValueSerializer(new StringRedisSerializer()); 25 return template; 26 } 27 }
也可以把參數獨立出來
1 spring.redis.pool.max-idle=10
2 spring.redis.pool.min-idle=5
3 spring.redis.pool.max-total=20
4 spring.redis.hostname=127.0.0.1
5 spring.redis.port=6379
1 @Configuration 2 public class RedisConfig { 3 @Bean 4 @ConfigurationPropeties(prefix="spring.redis.pool") 5 public JedisPoolConfig jedisPoolConfig() { 6 JedisPoolConfig config = new JedisPoolConfig(); 7 //其他參數已經自動注入
8 return config; 9 } 10 @Bean 11 @ConfigurationProperties(prefix="spring.redis") 12 public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) { 13 JedisConnectionFactory factory = new JedisConnectionFactory(); 14 factory.setPoolConfig(config); 15 //其他參數已經自動注入
16 return factory; 17 } 18 @Bean 19 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { 20 RedisTemplate<String, Object> template = new RedisTemplate<>(); 21 template.setConnectionFactory(factory); 22 template.setKeySerializer(new StringRedisSerializer()); 23 template.setValueSerializer(new StringRedisSerializer()); 24 return template; 25 } 26 }
使用springboot中的redis非常簡單
1 this.redisTemplate.opsForValue().set("user", user); 2 (User)this.redisTemplate.opsForValue().get("user");