前にそれが統合されたspringcahceをspringboot試してみました:https://www.cnblogs.com/a565810497/p/10931426.html
:そして、グアバと効果的な長いspringcahceをセットアップしようhttps://www.cnblogs.com/a565810497/p/10932149.html
グアバが長いだけでデフォルト設定を設定するには、あなたがよりアクティブに設定することはできません、とspringcahceがデータベース上にキャッシュされていない場合、Redisの一緒に収集し、springcahceのために非常に適しているのでしかし、最終的には、あまり柔軟性を感じます
まず、我々は記事を参照して、最初のspringcahce統合のRedisを使用したい:https://blog.battcn.com/2018/05/13/springboot/v2-cache-redis/
依存追加
porm.xmlでのRedisを頼る追加
<依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータデータRedisの</たartifactId> </依存> <依存性> <のgroupId> org.apache.commons </のgroupId> <たartifactId>コモンズ-POOL2 </たartifactId> </依存> <依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータ試験</たartifactId> <スコープ>テスト</スコープ> </依存関係>
構成プロパティ
## Redisのサーバーアドレス spring.redis.host = 127.0.0.1 ## Redisのサーバーポート spring.redis.port = 6379 ## Redisのサーバー接続パスワード(デフォルトでは空白) spring.redis.password = #は、一般的ではありません構成、スプリングキャッシュベースのパケット依存性自己アセンブリ spring.cache.type = Redisの #接続時間(ミリ秒) spring.redis.timeout = 10000 #16のRedisは断片デフォルトケースを有し、この特定の設定ポイントが使用しますシート spring.redis.database = 0 デフォルトで#接続プール最大接続数(負の値制限なし)。8 spring.redis.lettuce.pool.max -active = 8 (負の値をブロック#待ち時間接続プールの最大ない示しますリミット)デフォルト -1 spring.redis.lettuce.pool.max -wait = -1 #接続プールのデフォルトの最大アイドル接続。8 spring.redis.lettuce.pool.max=。8つの-idle #最小接続プールのアイドル接続のデフォルト 0 spring.redis.lettuce.pool.min -idle = 0
その後、我々はspringcacheの有効時間に基づいて、Redisのを設定することができ、記事を参照してください。https://blog.csdn.net/weixin_42047790/article/details/84189275
ときに起動クラスのコンフィグレーション有効長:
@Bean 公共のCacheManagerのCacheManager(RedisConnectionFactory redisConnectionFactory){ 返す 新しいRedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)、 この .getRedisCacheConfigurationWithTtl(30 * 60)、// 这个使用会默认策略、未配置的キー この .getRedisCacheConfigurationMap()//を指定策略キー )。 } プライベートマップの<string、RedisCacheConfiguration> getRedisCacheConfigurationMap(){ 地図 <文字列、RedisCacheConfiguration> redisCacheConfigurationMap = 新規 HashMapの<> (); // DayCache和SecondsCache进行过期时间配置 redisCacheConfigurationMap.put( "DayCache"、この .getRedisCacheConfigurationWithTtl(24 * 60 * 60 )); redisCacheConfigurationMap.put( "SecondsCache"、この .getRedisCacheConfigurationWithTtl(2 ))。 リターンredisCacheConfigurationMap; } プライベートRedisCacheConfiguration getRedisCacheConfigurationWithTtl(整数秒){ Jackson2JsonRedisSerializer <OBJECT> jackson2JsonRedisSerializer = 新しい Jackson2JsonRedisSerializer <>(オブジェクト。クラス)。 ObjectMapper OM = 新しいですObjectMapper(); om.setVisibility(PropertyAccessor.ALL、JsonAutoDetect.Visibility.ANY)。 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)。 jackson2JsonRedisSerializer.setObjectMapper(OM)。 RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()。 redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer) ).entryTtl(Duration.ofSeconds(秒))。 リターン redisCacheConfiguration。 } @Bean 公共するKeyGenerator wiselyKeyGenerator(){ 戻り 新しいするKeyGeneratorを(){ @Override パブリックオブジェクトは(オブジェクトターゲットメソッド方法、...のparamsオブジェクト){生成 StringBuilderのSB = 新規のStringBuilder(); sb.append(target.getClass()のgetName()。)。 sb.append( + "" method.getName()); もし(paramsは== NULL || params.length == 0 || paramsは[0] == NULL ){ 戻り ヌル。 } 文字列に参加 = String.join( "&"、Arrays.stream(paramsは).MAP(オブジェクト::のtoString).collect(Collectors.toList()))。 文字列形式 = String.Formatの( "%sの{%のS}" 、sb.toString()、結合)。 // log.info( "缓存キー:" +形式)。 戻り値の形式。 } }。 }
彼らは、同じ時間長くないキャッシュで使用しました:
@Override @Cacheable(値 = "SecondsCache" ) 公衆TestTime getTestTime(){ 戻り 新しい TestTime(新しい日付())。 } @Override @Cacheable(値 = "DayCache" ) 公衆TestTime getTestTime1(){ 戻り 新しい TestTime(新しい日付())。 }
Secondscacheは、ライフサイクルの2秒を設定することです。
DayCacheは、ライフサイクルの日のために設定されています。
なぜそんなに複雑な設定は、実際にはRedisのは、すでに、時々キャッシュを追加するための効果的な時間を設定しないが、我々は注釈に@cacheableは、コード注釈を実装する必要はありませんので、彼はRedisのとspringcahceを組み合わせた侵襲的な方法を使用します。
この記事の出所:https://gitee.com/Hiro-D/Java/tree/master/springcache-redis