Redis で複数のデータベースを構成する方法
はじめに
redis にはデフォルトでデータベース 0 ~ 16 があります。通常、redis を操作するときはデータベース 0 を使用しますが、プロジェクトによっては複数のデータベースを同時に操作したい場合があります。毎回他のデータベースにアクセスしたい データベースのデータを選択する際に、データベースを切り替えるのが面倒
そのため、複数の Jedis Client を設定する必要がありますが、Jedis はブロックされやすく効率があまり良くないため、ここでは Reactive 接続方式で比較的効率の良い Lettuce Client を使用します。しかし、Lettuce Client を使用するにはどうすればよいでしょうか? 実際には、通常、spring-boot-starter-data-redis 依存関係を追加し、RedisTemplate を通じて Redis 機能を使用します。バージョンが非常に高い場合、デフォルトの RedisTemplate の最下層は Lettuce Client を使用して接続と操作を確立します。データ。
1. pom 依存関係を追加します
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.5.RELEASE</version> </dependency>
2. 複数のデータ ソースの構成とスプリング コンテナーへの追加
下のスクリーンショットでは 4 つのデータ ソース、つまりライブラリ No. を使用しています。 1、2、3、4。
1) 新しい構成構成クラスを作成します
2) new RedisStandaloneConfiguration(host, port); Redis 構成を初期化し、ライブラリ番号を選択します。
3) LettuceConnectionFactory を初期化します。
4) RedisTemplate をインスタンス化し、キー値のシリアル化メソッドを設定します。ここではキーと値は両方とも文字列であるため、シリアライザーは StringRedisSerializer を選択します。
5) 3番目で作成したLettuceConnectionFactoryをRedisTemplateに設定し、@Beanアノテーションを付けてSpringコンテナに注入します 使用する場合は直接Springコンテナ内でメソッド名で検索してアセンブルしますそれを参照するインスタンスにコピーします。
import io.lettuce.core.resource.ClientResources; import io.lettuce.core.resource.DefaultClientResources; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.util.ObjectUtils; import java.time.Duration; /** * reactive redis * @Author:wangqipeng * @Date:14:38 2019-07-03 */ @Configuration public class RedisDatasourceConfiguration { @Value("${redis.isCleanRedisCache:false}") private String cleanRedisCache; @Value("${redis.host:127.0.0.1}") public String host; @Value("${redis.port:6379}") public Integer port; private String password; @Value("${redis.timeout:2000}") public Integer timeout; public Integer maxIdle = 16; public Integer minIdle = 5; public Integer maxTotal = 30; @Bean public RedisTemplate<String, String> stringRedisTemplate1() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(1); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate2() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(2); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate3() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(3); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate4() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(4); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate5() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(5); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } private RedisTemplate<String, String> getSerializerRedisTemplate(){ RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); return redisTemplate; } private RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = getSerializerRedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } private GenericObjectPoolConfig getGenericObjectPoolConfig(){ GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxTotal(maxTotal); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMaxWaitMillis(timeout); return genericObjectPoolConfig; } private LettuceConnectionFactory creatFactory(RedisStandaloneConfiguration configuration){ LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); builder.poolConfig(getGenericObjectPoolConfig()); // LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); // builder.clientResources(clientResources()); // builder.commandTimeout(Duration.ofSeconds(3000)); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); connectionFactory.afterPropertiesSet(); return connectionFactory; } }
3. 使い方
ここで参照するのは、上記の @Bean を通じて Spring コンテナにロードされるライブラリ No.2 です。
以上がRedis で複数のデータベースを構成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Redisクラスターモードは、シャードを介してRedisインスタンスを複数のサーバーに展開し、スケーラビリティと可用性を向上させます。構造の手順は次のとおりです。異なるポートで奇妙なRedisインスタンスを作成します。 3つのセンチネルインスタンスを作成し、Redisインスタンスを監視し、フェールオーバーを監視します。 Sentinel構成ファイルを構成し、Redisインスタンス情報とフェールオーバー設定の監視を追加します。 Redisインスタンス構成ファイルを構成し、クラスターモードを有効にし、クラスター情報ファイルパスを指定します。各Redisインスタンスの情報を含むnodes.confファイルを作成します。クラスターを起動し、CREATEコマンドを実行してクラスターを作成し、レプリカの数を指定します。クラスターにログインしてクラスター情報コマンドを実行して、クラスターステータスを確認します。作る

Redisは、キーの一意性を確保するために5つの戦略を使用します。1。名前空間分離。 2。ハッシュデータ構造。 3.データ構造を設定します。 4。文字列キーの特殊文字。 5。LUAスクリプト検証。特定の戦略の選択は、データ組織、パフォーマンス、およびスケーラビリティ要件に依存します。

Redisはハッシュテーブルを使用してデータを保存し、文字列、リスト、ハッシュテーブル、コレクション、注文コレクションなどのデータ構造をサポートします。 Redisは、スナップショット(RDB)を介してデータを維持し、書き込み専用(AOF)メカニズムを追加します。 Redisは、マスタースレーブレプリケーションを使用して、データの可用性を向上させます。 Redisは、シングルスレッドイベントループを使用して接続とコマンドを処理して、データの原子性と一貫性を確保します。 Redisは、キーの有効期限を設定し、怠zyな削除メカニズムを使用して有効期限キーを削除します。

Redis Clusterは、Redisインスタンスの水平拡張を可能にする分散展開モデルであり、ノード間通信、ハッシュスロット部門キースペース、ノード選挙、マスター奴隷レプリケーション、コマンドリダイレクトを通じて実装されます。ハッシュスロット:キースペースをハッシュスロットに分割して、キーの責任ノードを決定します。ノード選挙:少なくとも3つのマスターノードが必要であり、選挙メカニズムを通じて1つのアクティブマスターノードのみが保証されます。マスタースレーブレプリケーション:マスターノードはリクエストの書き込みを担当し、スレーブノードはリクエストとデータレプリケーションを読む責任があります。コマンドリダイレクト:クライアントはキーを担当するノードに接続し、ノードは誤ったリクエストをリダイレクトします。トラブルシューティング:障害検出、オフラインのマーク、および再

Redisバージョン番号を表示するには、次の3つの方法を使用できます。(1)情報コマンドを入力し、(2) - versionオプションでサーバーを起動し、(3)構成ファイルを表示します。

Redisのすべてのキーを表示するには、3つの方法があります。キーコマンドを使用して、指定されたパターンに一致するすべてのキーを返します。スキャンコマンドを使用してキーを繰り返し、キーのセットを返します。情報コマンドを使用して、キーの総数を取得します。

Redis Orderedセット(ZSET)は、並べ替えられた要素を保存し、関連するスコアでソートするために使用されます。 zsetを使用する手順には次のものがあります。1。zsetを作成します。 2。メンバーを追加します。 3.メンバースコアを取得します。 4。ランキングを取得します。 5.ランキング範囲のメンバーを取得します。 6.メンバーを削除します。 7.要素の数を取得します。 8。スコア範囲のメンバーの数を取得します。

Redisコマンドラインツール(Redis-Cli)を使用して、次の手順を使用してRedisを管理および操作します。サーバーに接続し、アドレスとポートを指定します。コマンド名とパラメーターを使用して、コマンドをサーバーに送信します。ヘルプコマンドを使用して、特定のコマンドのヘルプ情報を表示します。 QUITコマンドを使用して、コマンドラインツールを終了します。
