前回の記事ではspring でのキャッシュの利用を中心に紹介しましたが、今回の記事ではいずれも記憶媒体としてローカルメモリを利用していますが、実際にはプロジェクトがオンラインになった後は基本的にクラスタの形でデプロイされることになります。データはローカル メモリに保存されます, クラスターは共有できません. データを Redis に保存してキャッシュ共有を実現できます. Spring で @EnableCaching がどのように Redis に接続するかを見てみましょう。
Redisをインストールする
ダウンロードアドレス: https://redis.io/download
pom.xml に Redis 構成を導入する
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.3</version>
</dependency>
プロジェクト内に Redis 構成ファイルを作成する
新しく作成したcom/javacode2018/cache/demo2/redis.yml
内容は次のとおりです。
singleServerConfig:
address: "redis://127.0.0.1:6379"
password: null
clientName: null
database: 7 #选择使用哪个数据库0~15
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
codec:
class: "org.redisson.codec.JsonJacksonCodec"
Redis 関連の Bean を作成する
package com.javacode2018.cache.demo2;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
@ComponentScan
@EnableCaching //@1
public class MainConfig2 {
@Bean //@2
public CacheManager cacheManager() throws IOException {
RedissonSpringCacheManager cacheManager = new RedissonSpringCacheManager(this.redissonClient());
cacheManager.setCacheNames(Arrays.asList("cache1"));
return cacheManager;
}
@Bean //@3
public RedissonClient redissonClient() throws IOException {
InputStream is = MainConfig2.class.getResourceAsStream("/com/javacode2018/cache/demo2/redis.yml");
Config config = Config.fromYAML(is);
return Redisson.create(config);
}
}
@1: Spring Cache機能を有効にします。
@2: 春のキャッシュマネージャーのカスタマイズ. ここでは、redis タイプマネージャーを定義し、最下層はキャッシュの記憶媒体として redis を使用します。
@3: redis.yml 構成ファイルを使用して RedissonClient を作成し、redis と対話します。
テストクラスに来てください
package com.javacode2018.cache.demo2;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class BookService {
@Cacheable(cacheNames = "cache1", key = "#root.targetClass.name+'-'+#root.method.name")
public List<String> list() {
System.out.println("---模拟从db中获取数据---");
return Arrays.asList("java高并发", "springboot", "springcloud");
}
}
テストケース
@Test
public void test7() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
BookService bookService = context.getBean(BookService.class);
System.out.println(bookService.list());
System.out.println(bookService.list());
{
System.out.println("下面打印出cache1缓存中的key列表");
RedissonSpringCacheManager cacheManager = context.getBean(RedissonSpringCacheManager.class);
RedissonCache cache1 = (RedissonCache) cacheManager.getCache("cache1");
cache1.getNativeCache().keySet().stream().forEach(System.out::println);
}
}
実行出力
--- データベースからのデータ取得をシミュレートします --- [java high concurrency, springboot, springcloud] [java high concurrency, springboot, springcloud]以下は、 cache1 内のキーリスト com.javacode2018.cache.demo2.BookService-listを出力します。キャッシュ
この時点でデータは Redis に入力されました。Redisクライアント ツールでRedisDesktopManager
見てみましょう。
RedisDesktopManager
ダウンロードリンク
リンク: https://pan.baidu.com/s/1WCd-tk8dDDJnFIKciVIQsA 抽出コード: x728
解凍後、以下をクリックして直接実行してください
ケースのソースコード
https://gitee.com/javacode2018/spring-series
通行人 A 今後、すべての Java ケース コードがこれに載せられる予定です。皆さんはそれを見て、引き続きダイナミクスに注目してください。
出典: https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648936334&idx=2&sn=7565a7528bb24d090ce170e456e991ce&scene=21#wechat_redirect