2.2. CacheManagers の作成

2.2.1. 新しい RemoteCacheManager の作成

手順2.1 新しい RemoteCacheManager の設定

import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;

Configuration conf = new 
ConfigurationBuilder().addServer().host("localhost").port(11222).build();
RemoteCacheManager manager = new RemoteCacheManager(conf);
RemoteCache defaultCache = manager.getCache();
  1. ConfigurationBuilder() コンストラクターを使用して新しい設定ビルダーを作成します。.addServer() メソッドは、.host(<hostname|ip>) プロパティーと .port(<port>) プロパティーで設定されたリモートサーバーを追加します。
  2. 指定された設定を使用して新しい RemoteCacheManager を作成します。
  3. リモートサーバーからデフォルトキャッシュを取得します。

2.2.2. 新しい組み込みキャッシュマネージャーの作成

CDI を使用せずに新規の EmbeddedCacheManager を作成するには、以下の手順を実行します。

手順2.2 新しい組み込みキャッシュマネージャーの作成

  1. 設定 XML ファイルを作成します。たとえば、クラスパス上 ( resources/ フォルダー内) に my-config-file.xml ファイルを作成し、このファイルに設定情報を追加します。
  2. 設定ファイルを使用してキャッシュマネージャーを作成するには、以下のプログラムを使用した設定を使用します。
    EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml");
    Cache defaultCache = manager.getCache();
上記の手順を完了すると、my-config-file.xml で指定された設定を使用して新規の EmbeddedCacheManager が作成されます。

2.2.3. CDI の使用による新しい組み込みキャッシュマネージャーの作成

CDI を使用して新規の EmbeddedCacheManager インスタンスを作成するには、以下の手順を実行します。

手順2.3 CDI を使用した新規 EmbeddedCacheManager の作成

  1. 次のようにデフォルト設定を指定します。
    public class Config
       @Produces
       public EmbeddedCacheManager defaultCacheManager() {
          ConfigurationBuilder builder = new ConfigurationBuilder();          
          Configuration configuration = builder.eviction().strategy(EvictionStrategy.LRU).maxEntries(100).build();
          return new DefaultCacheManager(configuration);
       }
    }
  2. デフォルトのキャッシュマネージャーを挿入します。
    <!-- Additional configuration information here -->
    @Inject
    EmbeddedCacheManager cacheManager;
    <!-- Additional configuration information here -->