第33章 Spring Framework との統合
33.1. Spring Framework との統合
JBoss Data Grid では、ユーザーは Spring キャッシュプロバイダーを定義できるため、アプリケーションにキャッシュサポートを簡単に追加する方法を提供し、Spring のプログラミングモデルに精通したユーザーは JBoss Data Grid によるキャッシングを実現できます。
33.2. Spring Maven 依存関係の定義
Spring モジュールは、ライブラリーの依存関係およびリモートクライアントサーバーの依存関係とは別にバンドルされます。JBoss Data Grid の使用方法に応じて、以下の Maven 設定を使用する必要があります。
ライブラリーモードの Spring 4 用の pom.xml
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring4-embedded</artifactId>
<version>${infinispan.version}</version>
</dependency>
リモートクライアントサーバーモードの Spring 4 用の pom.xml
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring4-remote</artifactId>
<version>${infinispan.version}</version>
</dependency>
33.3. プログラミングによる Spring キャッシュサポートの有効化 (ライブラリーモード)
Spring のキャッシュサポートは、アプリケーションでプログラミングを使用して有効にできます。Spring のサポートを有効にするには、以下の手順を実行します。
-
使用している Spring 設定クラスに
@EnableCachingアノテーションを追加します。 -
@Beanアノテーションが付けられたSpringEmbeddedCacheManagerを返すメソッドを定義します。
以下のコードスニペットにはこれらの変更が反映されています。
プログラミングによる設定の例
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
[...]
@org.springframework.context.annotation.Configuration
@EnableCaching
public class Config {
[...]
@Bean
public SpringEmbeddedCacheManager cacheManager() throws Exception {
Configuration config = new ConfigurationBuilder()
.memory()
.size(150)
.build();
return SpringEmbeddedCacheManager(new DefaultCacheManager(config));
}
[...]
33.4. プログラミングによる Spring キャッシュサポートの有効化 (リモートクライアントサーバーモード)
Spring のキャッシュサポートは、以下の手順を実行し、アプリケーションでプログラミングを使用すると有効にできます。
-
使用している Spring 設定クラスに
@EnableCachingアノテーションを追加します。 -
@Beanアノテーションが付けられたSpringRemoteCacheManagerを返すメソッドを定義します。
以下のコードスニペットにはこれらの変更が反映されています。
プログラミングによる設定の例
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.spring.provider.SpringRemoteCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
[...]
@org.springframework.context.annotation.Configuration
@EnableCaching
public class Config {
[...]
@Bean
public SpringRemoteCacheManager cacheManager() throws Exception {
Configuration config = new ConfigurationBuilder()
.addServer()
.host(ADDRESS)
.port(PORT)
.build();
return new SpringRemoteCacheManager(new RemoteCacheManager(config));
}
[...]
33.5. アプリケーションコードへのキャッシングの追加
『Spring Cache Abstraction』に記載されている Spring アノテーションを使用すると、キャッシングを各アプリケーションに追加できます。
キャッシュエントリーの追加
キャッシュにエントリーを追加するには、@Cacheable アノテーションをメソッドに追加します。このアノテーションは戻り値を指定のキャッシュに追加します。たとえば、特定のキーを基に Book を返すメソッドの場合、次のように @Cacheable アノテーションを追加します。
@Cacheable(value = "books", key = "#bookId")
public Book findBook(Integer bookId) {...}
findBook(Integer bookId) から返されたすべての Book インスタンスは、bookId を値のキーとして使用し、books という名前のキャッシュに置かれます。
key 属性が指定されていない場合、Spring は提供された引数からハッシュを生成し、生成されたこの値をキャッシュキーとして使用します。アプリケーションが直接エントリーを参照する必要がある場合、エントリーを簡単に取得できるようにするため、key 属性を含めることが推奨されます。
キャッシュエントリーの削除
キャッシュからのこエントリーを削除するには、メソッドに @CacheEvict アノテーションを付けます。このアノテーションを設定すると、キャッシュのすべてのエントリーをエビクトしたり、指定のキーを持つエントリーのみを対象としたりすることができます。以下の例を見てください。
// Evict all entries in the "books" cache
@CacheEvict (value="books", key = "#bookId", allEntries = true)
public void deleteBookAllEntries() {...}
// Evict any entries in the "books" cache that match the passed in bookId
@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...]}
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.