1. Druid の統合
1.1 Druid の概要
Java プログラムの大部分はデータベースを操作する必要があります。パフォーマンスを向上させるには、データベースを操作するときに、データベース接続プール。
Druid は、Alibaba のオープンソース プラットフォーム上に実装されたデータベース接続プールであり、C3P0 や DBCP などの DB プールの利点を組み合わせ、ログ監視も追加します。
Druid は監視用に設計されたデータベース接続プールで、データベース接続プールの接続状態や SQL 実行状態を効果的に監視できます。
1.2 Druid データ ソースの依存関係を追加します。
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency>
1.3 Druid データ ソースを使用します。
server: port: 8080 spring: datasource: druid: url: jdbc:mysql://localhost:3306/eshop?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true username: xxx password: xxx driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 10 max-active: 20 min-idle: 10 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 stat-view-servlet: enabled: true login-username: admin login-password: 1234 logging: level: com.wyy.spring.Dao: debug
テストが成功するかどうかを確認します。
package com.wyy.spring; import com.wyy.spring.Dao.StudentMapper; import com.wyy.spring.service.StudentService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; @SpringBootTest class SpringBoot04ApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() { System.out.println(dataSource.getClass()); } }
結果の出力
2. redis の統合
2.1 redis 依存関係の追加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.2 yml Redis 構成情報を追加します
redis: database: 0 host: 120.0.0.0 port: 6379 password: xxxx jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 10000
2.3 redis 構成クラス
package com.wyy.spring.conf; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.util.ClassUtils; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.time.Duration; @Configuration @EnableCaching public class RedisConfiguration extends CachingConfigurerSupport { @Bean @Primary /** * 缓存管理器 */ CacheManager cacheManager(RedisConnectionFactory factory) { RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .computePrefixWith(cacheName -> cacheName + ":-cache-:") /*设置缓存过期时间*/ .entryTtl(Duration.ofHours(1)) /*禁用缓存空值,不缓存null校验*/ .disableCachingNullValues() /*设置CacheManager的值序列化方式为json序列化,可使用加入@Class属性*/ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer( new GenericJackson2JsonRedisSerializer() )); /*使用RedisCacheConfiguration创建RedisCacheManager*/ RedisCacheManager manager = RedisCacheManager.builder(factory) .cacheDefaults(cacheConfiguration) .build(); return manager; } public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(factory); RedisSerializer stringSerializer = new StringRedisSerializer(); /* key序列化 */ redisTemplate.setKeySerializer(stringSerializer); /* value序列化 */ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); /* Hash key序列化 */ redisTemplate.setHashKeySerializer(stringSerializer); /* Hash value序列化 */ redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; @Override public KeyGenerator keyGenerator() { return (Object target, Method method, Object... params) -> { final int NO_PARAM_KEY = 0; final int NULL_PARAM_KEY = 53; StringBuilder key = new StringBuilder(); /* Class.Method: */ key.append(target.getClass().getSimpleName()) .append(".") .append(method.getName()) .append(":"); if (params.length == 0) { return key.append(NO_PARAM_KEY).toString(); } int count = 0; for (Object param : params) { /* 参数之间用,进行分隔 */ if (0 != count) { key.append(','); } if (param == null) { key.append(NULL_PARAM_KEY); } else if (ClassUtils.isPrimitiveArray(param.getClass())) { int length = Array.getLength(param); for (int i = 0; i < length; i++) { key.append(Array.get(param, i)); key.append(','); } } else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) { key.append(param); } else { /*JavaBean一定要重写hashCode和equals*/ key.append(param.hashCode()); count++; return key.toString(); }; }
@CacheConfig キャッシュされたキャッシュ名、KeyGenerator、CacheManager、および CacheResolver の共有を可能にするクラスレベルのアノテーション
@Cacheable 使用メソッドがキャッシュ可能であることを宣言します。実行されたメソッドの結果をキャッシュすることで、同じパラメータを使用した後続の呼び出しで実際のメソッドを再度実行する必要がなくなります。キャッシュから値を直接取得します
@CachePut でマークされたメソッドは、実行前に以前に実行された結果がキャッシュにあるかどうかを確認しません。代わりに、メソッドは毎回実行され、実行結果は次のようになります。キー値として表現され、正しいフォームは指定されたキャッシュに保存されます。
@CacheEvict の機能は主にメソッド設定用であり、特定の条件に従ってキャッシュをクリアできます
以上がSpringBoot が Druid と Redis を統合する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

redisisbothadatabaseandaserver.1)asadatabase、itusesin memorystorage forfastaccess、理想的なforreal-timeapplicationsandcaching.2)asaserver、itupportspub/submessagingandaging andluascriptingforreal-communicationandserver-sideoperation。

Redisは、高性能と柔軟性を提供するNOSQLデータベースです。 1)大規模データと高い並行性の処理に適したキー価値ペアを介してデータを保存します。 2)メモリストレージとシングルスレッドモデルは、速い読み取りと書き込みと原子性を確保します。 3)RDBおよびAOFメカニズムを使用してデータを持続し、高可用性とスケールアウトをサポートします。

Redisは、主にデータベース、キャッシュ、メッセージブローカーとして使用されるメモリデータ構造ストレージシステムです。そのコア機能には、シングルスレッドモデル、I/O多重化、持続メカニズム、複製、クラスタリング機能が含まれます。 Redisは、キャッシュ、セッションストレージ、メッセージキューのための実際のアプリケーションで一般的に使用されます。適切なデータ構造を選択し、パイプラインとトランザクションを使用し、監視とチューニングを使用することにより、パフォーマンスを大幅に改善できます。

RedisデータベースとSQLデータベースの主な違いは、Redisが高性能および柔軟性要件に適したインメモリデータベースであることです。 SQLデータベースは、複雑なクエリとデータの一貫性要件に適したリレーショナルデータベースです。具体的には、1)Redisは高速データアクセスとキャッシュサービスを提供し、キャッシュおよびリアルタイムのデータ処理に適した複数のデータ型をサポートします。 2)SQLデータベースは、テーブル構造を介してデータを管理し、複雑なクエリとトランザクション処理をサポートし、データの一貫性を必要とするeコマースや金融システムなどのシナリオに適しています。

redisactsassassadatastoreandaservice.1)asadatastore、itusesin memorystorage for fastorations、supporting variousdatastructureSlike-key-valuepairsandsortedsets.2)asaservice、iteasruascruascriptingrupting criptingforceptingpurplecomplecomplecprexoperations

他のデータベースと比較して、Redisには次の独自の利点があります。1)非常に速い速度、および読み取り操作は通常、マイクロ秒レベルにあります。 2)豊富なデータ構造と操作をサポートします。 3)キャッシュ、カウンター、公開サブスクリプションなどの柔軟な使用シナリオ。 Redisまたはその他のデータベースを選択する場合、特定のニーズとシナリオに依存します。 Redisは、高性能および低遅延のアプリケーションでうまく機能します。

Redisは、データストレージと管理において重要な役割を果たしており、複数のデータ構造と持続性メカニズムを通じて最新のアプリケーションの中核となっています。 1)Redisは、文字列、リスト、コレクション、注文されたコレクション、ハッシュテーブルなどのデータ構造をサポートし、キャッシュや複雑なビジネスロジックに適しています。 2)RDBとAOFの2つの持続方法を通じて、Redisは信頼できるストレージとデータの迅速な回復を保証します。

Redisは、大規模なデータの効率的なストレージとアクセスに適したNOSQLデータベースです。 1.Redisは、複数のデータ構造をサポートするオープンソースメモリデータ構造ストレージシステムです。 2.キャッシュ、セッション管理などに適した、非常に速い読み取り速度と書き込み速度を提供します。 4.使用例には、基本的なキー値ペア操作と高度なコレクション重複排除関数が含まれます。 5.一般的なエラーには、接続の問題、データ型の不一致、メモリオーバーフローが含まれるため、デバッグに注意する必要があります。 6.パフォーマンス最適化の提案には、適切なデータ構造の選択とメモリ排除戦略の設定が含まれます。


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

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

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

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

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

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

ホットトピック









