Commit 53743dd0bfeabf718cecdad3f596a82595fd56af
1 parent
465b6add
#feat:增加二级缓存过期时间配置项,默认120秒
Showing
2 changed files
with
12 additions
and
5 deletions
src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java
| 1 | package com.irrigation.icl.cache; | 1 | package com.irrigation.icl.cache; |
| 2 | 2 | ||
| 3 | -import com.irrigation.icl.cache.config.CacheConfig; | ||
| 4 | import com.irrigation.icl.cache.core.LayeringCacheLoadCondition; | 3 | import com.irrigation.icl.cache.core.LayeringCacheLoadCondition; |
| 5 | import com.irrigation.icl.cache.core.LayeringCacheManager; | 4 | import com.irrigation.icl.cache.core.LayeringCacheManager; |
| 6 | import com.irrigation.icl.cache.core.LayeringCacheProperties; | 5 | import com.irrigation.icl.cache.core.LayeringCacheProperties; |
| 7 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
| 8 | -import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
| 9 | -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| 10 | import org.springframework.boot.context.properties.EnableConfigurationProperties; | 7 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 11 | import org.springframework.cache.CacheManager; | 8 | import org.springframework.cache.CacheManager; |
| 12 | import org.springframework.context.annotation.Bean; | 9 | import org.springframework.context.annotation.Bean; |
| @@ -34,7 +31,8 @@ public class LayeringCacheAutoFactory { | @@ -34,7 +31,8 @@ public class LayeringCacheAutoFactory { | ||
| 34 | LayeringCacheProperties layeringCacheProperties) { | 31 | LayeringCacheProperties layeringCacheProperties) { |
| 35 | LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); | 32 | LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); |
| 36 | cacheManager.setUsePrefix(true); | 33 | cacheManager.setUsePrefix(true); |
| 37 | - cacheManager.setDefaultExpiration(90); | 34 | + long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L; |
| 35 | + cacheManager.setDefaultExpiration(expire); | ||
| 38 | return cacheManager; | 36 | return cacheManager; |
| 39 | } | 37 | } |
| 40 | 38 | ||
| @@ -64,9 +62,11 @@ public class LayeringCacheAutoFactory { | @@ -64,9 +62,11 @@ public class LayeringCacheAutoFactory { | ||
| 64 | public void onMessage(Message message, byte[] pattern) { | 62 | public void onMessage(Message message, byte[] pattern) { |
| 65 | byte[] content = message.getBody(); | 63 | byte[] content = message.getBody(); |
| 66 | if (content != null) { | 64 | if (content != null) { |
| 65 | +// String commandString = new String(content, Charset.defaultCharset()); | ||
| 66 | +// Command command = JSON.parseObject(commandString, Command.class); | ||
| 67 | Command command = (Command) redisTemplate.getValueSerializer().deserialize(content); | 67 | Command command = (Command) redisTemplate.getValueSerializer().deserialize(content); |
| 68 | if (command != null) { | 68 | if (command != null) { |
| 69 | - log.info(command.toString()); | 69 | + log.debug(command.toString()); |
| 70 | handler.handle(command); | 70 | handler.handle(command); |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java
| @@ -16,7 +16,14 @@ public class LayeringCacheProperties { | @@ -16,7 +16,14 @@ public class LayeringCacheProperties { | ||
| 16 | * L1缓存配置 | 16 | * L1缓存配置 |
| 17 | */ | 17 | */ |
| 18 | private L1 cacheL1; | 18 | private L1 cacheL1; |
| 19 | + /** | ||
| 20 | + * 是否启用缓存 | ||
| 21 | + */ | ||
| 19 | private boolean enable = true; | 22 | private boolean enable = true; |
| 23 | + /** | ||
| 24 | + * L2级缓存的过期时间,单位秒 | ||
| 25 | + */ | ||
| 26 | + private long expire; | ||
| 20 | 27 | ||
| 21 | @Data | 28 | @Data |
| 22 | // ehcache设置 | 29 | // ehcache设置 |