Bean キャッシュ

Bean キャッシュを有効にするには、@Cache アノテーションをエンティティ Bean タイプに追加します。

@Cache
@Entity
public class Customer {
 ...
}

ナチュラルキー

naturalKey 属性を使用して、使用できるナチュラル一意キーを定義します。

Ebean はナチュラルキー参照キャッシュを自動的に維持し、where 式のナチュラルキープロパティを使用するクエリで使用できます。

@Cache(naturalKey = "email")
// Multiple properties make up the unique natural key
@Cache(naturalKey = {"store","sku"})

暗黙に使用

Bean に @Cache が定義されている場合、Ebean は L2 Bean キャッシュを自動的に使用しようとします。

@Cache
@Entity
public class Country
// automatically use the cache
Country country = DB.find(Country.class,"NZ");

// references automatically use the cache too
Country countryRef = DB.reference(Country.class,"NZ");

// hit the country cache automatically via navigation
// ... and lazy loading
Customer customer = DB.find(Customer.class, 1);
Address billingAddress = customer.getBillingAddress();
Country c2 = billingAddress.getCountry();

明示的

query.setUseCache() を使用して Bean キャッシュを使用することを明示的に指定できますが、Bean キャッシュについてはキャッシュを使用しない場合を指定できます。

// explicitly state we do not want to use the bean cache
Customer customer = DB.find(Customer.class)
  .setUseCache(false)
  .setId(7)
  .findOne();

ニアキャッシュ

ebean-redis を使用するときはニアキャッシュを有効にするには nearCache=true を設定します。Hazelcast または Ignite でニアキャッシュを有効にするには代わりにそれらの Hazelcast | Ignite 独自の構成を使用します。

@Cache(nearCache=true)
@Entity
public class Country

上記の場合、ebean-redis を使用すると Bean キャッシュに当たるクエリは最初に処理内のニアキャッシュを試してみて、データがローカルニアキャッシュにない場合にのみ Redis に当たります。