Redisの紹介
Redisは、業界で最も広く使用されているメモリデータストレージです。 Memcachedと比較して、Redisは、ハッシュ、リスト、セットなどの豊富なデータ構造をサポートし、データの持続性もサポートします。さらに、Redisは、トランザクション、HA、マスタースレーブライブラリなどのデータベースのような機能も提供しています。 Redisにはキャッシュシステムとデータベースの特徴があるため、豊富なアプリケーションシナリオがあると言えます。
導入
ユニットテストのために、テストの安定性と客観性を確保するために、単一の環境を維持し、ネットワークリソースと通信しないようにする必要があります。スプリングブートフレームワークの場合、ユニットテストJunitを統合します。同時に、プロジェクトを設計する際に、Mongodb、Redis、MySQLなどのさまざまな組み込みストレージツールを使用できます。今日、私は主に埋め込みRedisの使用について話します。
それを使用する方法は次のとおりです。
パッケージリファレンスbuild.gradleを追加します
testcompile( 'com.github.kstyrc:埋め込みredis:0.6')
構成インジェクションを追加します
org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; Import org.springframework.data.redis.connection.redinctonnection.Rednection.connection.Rednectionfactory; org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.ListOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SetOperations;import org.springframework.data.redis.core.valueoperations; Import org.springframework.data.redis.core.zsetoperations; Import org.springframework.data.redis.serializer.jdkserializationedisserializer; Import; org.springframework.data.redis.serializer.stringredisserializer; @configurationpublic class redisconfig { / ** * redisconnectionFactory * / @autowired redisconnectionFactory redisconnectionFactory; / ** * redistemplateオブジェクトをインスタンス化 * * @return */ @bean public redistemplate <string、object> functiondomainredistemplate(){redistemplate <string、object> redistemplate = new redistemplate <>(); initdomainredistemplate(redistemplate、redisconnectionFactory); REDISTEMPLATEを返します。 } / ** *データストレージのシリアル化方法をRedisに設定します * * @param Redistemplate * @Param Factory * / private void initdomainredistemplate(redistemplate <string、object> redistemplate、redisconnectionfactory Factory){redistemplate.setkeyserializer(new StringRedisserializer(); redistemplate.sethashkeyserializer(new Stringredisserializer()); redistemplate.sethashvalueserializer(new jdkserializedredisserializer()); redistemplate.setValueSerializer(new jdkserializedredisserializer()); redistemplate.setConnectionFactory(Factory); } / ** * Instanced Hashoperationsオブジェクト、ハッシュタイプ操作を使用できます * * @param redistemplate * @return * / @bean public Hashoperations <String、String、Object> Hashoperations(Object> Redistemplate){return redistemplate.ophash(); } / ** * Instanced ValuePerationsオブジェクト、文字列操作を使用できます * * @Param Redistemplate * @return * / @Bean public ValuePerations <string、object> valueperations(redistemplate <string、object> redistemplate){return redistemplate.opsforvalue(); } / ** * listoperationsオブジェクトをインスタンス化するには、リスト操作を使用できます * * @param redistemplate * @return * / @bean public listoperations <string、object> listoperations(redistemplate <string、object> redistemplate){return redistemplate.opsforlist(); } / ** * setoperationsオブジェクトをインスタンス化すると、セット操作を使用できます * * @param redistemplate * @return * / @bean public setoperations <string、object> setoperations(redistemplate <string、object> redistemplate){return redistemplate.opsforset(); } / ** * zsetoperationsオブジェクトをインスタンス化すると、zset操作を使用できます * * @param redistemplate * @return * / @bean public zsetoperations <string、object> zsetoperations(object> redistemplate){return redistemplate.opsforzset(); }}ビジネスレイヤーでRedisを使用します
@autowired redistemplate <string、object> rediscachetemplate;
使用中、RedistemplateオブジェクトはAutowiredによって注入されました。
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。