はじめに
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 サイトの他の関連記事を参照してください。

Redisの主な機能には、速度、柔軟性、豊富なデータ構造のサポートが含まれます。 1)速度:Redisはメモリ内データベースであり、読み取り操作はほとんど瞬間的で、キャッシュとセッション管理に適しています。 2)柔軟性:複雑なデータ処理に適した文字列、リスト、コレクションなど、複数のデータ構造をサポートします。 3)データ構造のサポート:さまざまなビジネスニーズに適した文字列、リスト、コレクション、ハッシュテーブルなどを提供します。

Redisのコア関数は、高性能のメモリ内データストレージおよび処理システムです。 1)高速データアクセス:Redisはデータをメモリに保存し、マイクロ秒レベルの読み取り速度と書き込み速度を提供します。 2)豊富なデータ構造:文字列、リスト、コレクションなどをサポートし、さまざまなアプリケーションシナリオに適応します。 3)永続性:RDBとAOFを介してディスクにデータを持続します。 4)サブスクリプションを公開:メッセージキューまたはリアルタイム通信システムで使用できます。

Redisは、次のようなさまざまなデータ構造をサポートしています。1。文字列、単一価値データの保存に適しています。 2。キューやスタックに適したリスト。 3.非重複データの保存に使用されるセット。 4。ランキングリストと優先キューに適した注文セット。 5。オブジェクトまたは構造化されたデータの保存に適したハッシュテーブル。

Redisカウンターは、Redisキー価値ペアストレージを使用して、カウンターキーの作成、カウントの増加、カウントの減少、カウントのリセット、およびカウントの取得など、カウント操作を実装するメカニズムです。 Redisカウンターの利点には、高速速度、高い並行性、耐久性、シンプルさと使いやすさが含まれます。ユーザーアクセスカウント、リアルタイムメトリック追跡、ゲームのスコアとランキング、注文処理などのシナリオで使用できます。

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

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

Redisのキューを読むには、キュー名を取得し、LPOPコマンドを使用して要素を読み、空のキューを処理する必要があります。特定の手順は次のとおりです。キュー名を取得します:「キュー:キュー」などの「キュー:」のプレフィックスで名前を付けます。 LPOPコマンドを使用します。キューのヘッドから要素を排出し、LPOP Queue:My-Queueなどの値を返します。空のキューの処理:キューが空の場合、LPOPはnilを返し、要素を読む前にキューが存在するかどうかを確認できます。

RedisクラスターでのZsetの使用:Zsetは、要素をスコアに関連付ける順序付けられたコレクションです。シャード戦略:a。ハッシュシャーディング:ZSTキーに従ってハッシュ値を分配します。 b。範囲シャード:要素スコアに従って範囲に分割し、各範囲を異なるノードに割り当てます。操作の読み取りと書き込み:a。読み取り操作:ZSetキーが現在のノードのシャードに属している場合、ローカルで処理されます。それ以外の場合は、対応するシャードにルーティングされます。 b。書き込み操作:Zsetキーを保持しているシャードに常にルーティングされます。


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

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