CacheManagerなしぢゃダメなのか?

前回キャッシュを使うためにCacheManagerを使用しましたが、キャッシュのファクトリとしての役割しかしてなさそうに見えます。そう考えると、CacheManager を使わない方法を考えたくなるのが人の常。

というわけで、CacheManager なしのテストをしてみました。

/**
 * CacheManagerなしでCacheを使う試みのサンプル。(失敗)
 */
public class CacheWithoutCacheManagerTest {

  public static void main(String[] args) {
    // キャッシュ作成
    String name = "testCache";
    int maxElementsInMemory = 10000;
    boolean overflowToDisk = false;
    boolean eternal = true;
    long timeToLiveSeconds = 0;
    long timeToIdelSeconds = 0;
    Cache cache = new Cache(name, maxElementsInMemory, overflowToDisk, eternal, timeToLiveSeconds, timeToIdelSeconds);

    // キャッシュされるデータ作成
    Object key = "Cached Key";
    Object value = "Cached Value";
    Element element = new Element(key, value);
    
    // キャッシュにデータをput -> Exception!!
    cache.put(element);
    
    // キャッシュからデータをget
    Element result = cache.get(key);
    
    // 内容確認。
    System.out.println("key = [" + result.getObjectKey() + "]\nvalue = [" + result.getObjectValue() + "]");
  }
}

すると、例外が発生しました。

Exception in thread "main" java.lang.IllegalStateException: The testCache Cache is not alive.
  at net.sf.ehcache.Cache.checkStatus(Cache.java:1588)
  at net.sf.ehcache.Cache.put(Cache.java:648)
  at net.sf.ehcache.Cache.put(Cache.java:618)
  at sample.CacheWithoutCacheManagerTest.main(CacheWithoutCacheManagerTest.java:27)

キャッシュが alive じゃないと怒られているようです。

しかし、ソースを確認できるのが、オープンソースのよいところ。当該箇所を見てみます。

private void checkStatus() throws IllegalStateException {
  if (!status.equals(Status.STATUS_ALIVE)) {
    throw new IllegalStateException("The " + configuration.getName() + " Cache is not alive.");
  }
}

なるほど。status が Status.STATUS_ALIVE になってなければダメなのね。

だったら、無理やりtrueにしてやるw

と思ったが、private フィールドでした orz

ということで、Cacheクラスのソースを、正規表現 "status\s*=" にて検索。

ありました。

private void changeStatus(Status status) {
  this.status = status;
}

しかし、残念ながらプライベートメソッドです。

さらに、"changeStatus\s*(" で検索。

ありました。initialize メソッドの中で、changeStatus(Status.STATUS_ALIVE) していました。

ということで、initializeメソッドを追加して実験。

/**
 * CacheManagerなしでCacheを使う試みのサンプル。(成功)
 */
public class CacheWithoutCacheManagerTest2 {

  public static void main(String[] args) {
    // キャッシュ作成
    String name = "testCache";
    int maxElementsInMemory = 10000;
    boolean overflowToDisk = false;
    boolean eternal = true;
    long timeToLiveSeconds = 0;
    long timeToIdelSeconds = 0;
    Cache cache = new Cache(name, maxElementsInMemory, overflowToDisk, eternal, timeToLiveSeconds, timeToIdelSeconds);

    // キャッシュを初期化
    cache.initialise();
    
    // キャッシュされるデータ作成
    Object key = "Cached Key";
    Object value = "Cached Value";
    Element element = new Element(key, value);
    
    // キャッシュにデータをput
    cache.put(element);
    
    // キャッシュからデータをget
    Element result = cache.get(key);
    
    // 内容確認。
    System.out.println("key = [" + result.getObjectKey() + "]\nvalue = [" + result.getObjectValue() + "]");
  }
}

結果

key = [Cached Key]
value = [Cached Value]

成功です。

となると、net.sf.ehcache.Cache#initialise メソッドって何してるのかが気になります。