NCacheは、メモリ内の非常に高速でスケーラブルな分散キャッシュとして、Spring との統合を提供し、高価なデータ トリップを削減することでアプリケーションのパフォーマンスを向上させます。
Spring は、Java 用の軽量の依存関係注入およびアスペクト指向開発コンテナーおよびフレームワークです。 これにより、アプリケーション開発を簡素化し、次のような再利用可能な部分をアプリケーションにプラグインできます。 NCache.
Spring 自体は凝集性と疎結合を提供することで Java 開発全体の複雑さを軽減しますが、これらのアプリケーションでは大量のトラフィックが発生します。 これらの高トラフィック Spring アプリケーションは、重大なスケーラビリティの問題に直面しています。 したがって、スケーラビリティ要件を満たすメモリ内分散キャッシュが重要です。
これらのアプリケーションは、サーバー ファームにサーバーを追加することでスケールアップできますが、残念ながら、データベースはそのような高負荷を処理できるようにスケールアップすることはできません。 このようなシナリオでは、分散キャッシュがデータベースの処理に最適です。 スケーラビリティ 問題。
したがって、最良のオプションは NCache。 それはスピーディで、インメモリで、 キーバリューストア Spring キャッシュ モジュールを実装した分散キャッシュ。これにより、Spring アプリケーションがスケーラブルで分散されます。 スケーラビリティの問題を引き起こす高価なデータベースの移動を削減することでデータベースの負荷を軽減し、より高速なパフォーマンスを提供します。
NCache 次のXNUMXつの方法でSpringキャッシングサポートを提供します。
Spring アプリケーションを構成するには、まず、Spring とアプリケーションに必要なすべての Maven 依存関係を追加する必要があります。 NCache 提供する。
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-spring</artifactId>
<version>x.x.x</version>
</dependency>
これらの依存関係を追加した後、Java ベースまたは XML ベースの Bean 定義を通じて、Spring アプリケーションで Bean を定義します。
Java ベースの Bean 定義:
Java ベースの定義を使用して Bean を定義するには、 @豆 CachingConfiguration クラスのアノテーション。ここで、setConfigFile メソッドは、 ncache-spring.xmlファイル。 詳細については、を参照してください。 JavaベースのBean 定義。
@Configuration
class CachingConfiguration {
@Bean
public CacheManager cacheManager() {
String resource = Path.of(System.getenv("NCHOME"), "config/ncache-spring.xml").toString();
SpringConfigurationManager springConfigurationManager = new SpringConfigurationManager();
springConfigurationManager.setConfigFile(resource);
NCacheCacheManager cacheManager = new NCacheCacheManager();
cacheManager.setSpringConfigurationManager(springConfigurationManager);
return cacheManager;
}
}
XML ベースの Bean 定義:
XML ベースの定義を使用して Bean を定義するには、キャッシュを有効にして指定する XML ファイルを追加する必要があります。 NCache キャッシュマネージャーとして。 BeanタグはcacheManagerとcacheManagerの両方に定義する必要があります。 NCache構成マネージャー。 さらに、cacheManager に次のプロパティを指定する必要があります。
<bean id="cacheManager" class="com.alachisoft.integrations.spring.NCacheCacheManager">
<property name="springConfigurationManager" ref="NCacheConfigurationManager"/>
<property name="logFilePath" value=" C:\Program Files\NCache\log-files\CustomerDBSpringLogs"/>
</bean>
<bean id="NCacheConfigurationManager" class="com.alachisoft.integrations.spring.configuration.SpringConfigurationManager">
<property name="configFile" value="ncache-spring.xml">
</bean>
詳しくは、 XMLベースのBean定義.
さらに、キャッシュを設定する必要があります。 ncache-spring.xml ファイル NCache 管理センター。 各キャッシュには、このファイル内の独自のプロパティ セットを使用した独自の定義が必要です。 例えば:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application-config default-cache-name="books">
<caches>
<cache name="books" ncacheid-instance="democache" priority="normal" expiration-type="absolute" expiration-period="300"/>
</caches>
</application-config>
Spring は JCache 準拠のキャッシュ プロバイダーもサポートしており、Spring アプリケーションで JCache を使用できます。 NCache。 まず、Spring の Maven 依存関係を追加します。 NCache、JCache。 依存関係を追加した後、キャッシュを構成します。
詳細については、 JCache Spring ドキュメント.
上記で定義した両方の構成でキャッシュを有効にしたら、次のステップは、これらのキャッシュ動作をそれぞれのメソッドにバインドして使用することです。 NCache 春のキャッシングプロバイダーとして。
XNUMX つの方法でキャッシュ動作をそのメソッドにバインドできます。 一つは終わった キャッシングアノテーション.
// BookController.java
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
// Get a Spring Service to save objects in the DB. This also caches them with 5min absolute (TTL) expiration.
BookService service = context.getBean(BookService.class);
service.save(new Book(18001, "The Second Machine Age", "Erik Brynjolfsson, Andrew McAfee", new Date(2014, 0, 20)));
// Print all saved books details
printBooks(service.listAll());
// Use Spring Service to get a book from the DB. It actually comes from the cache
Book foundBook = service.findBookByIsbn(18001);
printBookDetails(foundBook);
foundBook.setAuthor("New Author");
Book UpdatedBook = service.update(foundBook);
printBookDetails(UpdatedBook);
context.close();
System.exit(0);
}
// BookService.java
@Service
class BookService {
@Autowired
private BookRepository repo;
public List<Book> listAll() {
return repo.findAll();
}
@CachePut(value = "demoCache", key = "#book.getISBN()")
public Book save(Book book) { return repo.save(book); }
@CachePut(value = "demoCache", key = "#book.getISBN()")
public Book update(Book book) { return repo.save(book); }
@Cacheable(value = "demoCache", key = "#id")
public Book get(int id) {
return repo.findById(id);
}
@Cacheable(value = "demoCache", key = "#isbn")
public Book findBookByIsbn(long isbn) {
return repo.findBookByIsbn(isbn);
}
@CacheEvict(value = "demoCache", allEntries = true)
public void delete(int id) {
repo.deleteById(id);
}
}
注釈を使用しない場合は、 宣言型 XML ベースのキャッシュ ターゲットメソッドとキャッシュディレクティブを外部で指定できる場合に使用できます。
<!-- the service we want to make cacheable -->
<bean id="bookService" class="x.y.service.BookService"/>
<!-- cache definitions -->
<cache:advice id="booksCacheAdvice" cache-manager="cacheManager">
<cache:caching cache="demoCache">
<cache:cacheable method="getBookNameByIsbn" key="#isbn"/>
<cache:cache-evict method="loadBooks" all-entries="true"/>
</cache:caching>
</cache:advice>