jsr 107 caching standard
DESCRIPTION
This presentation explain the new feature of JSR 107 Caching Standards.TRANSCRIPT
1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
The New JSR-107 Caching Standard
レッドハット株式会社 木村 貴由 日本オラクル株式会社 寺田 佳央
3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。
OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。
4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
Program Agenda
• はじめに • JSR-107 Caching の概要 • JSR-107 Caching の詳細
5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 5
はじめに
6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 5
JSR-107 の概要
14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
仕様に関する情報入手先
http://jcp.org/en/jsr/detail?id=107
15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
http://tinyurl.com/7b5tenh
https://github.com/jsr107
仕様に関する情報入手先
16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
ConcurrentMapに類似
putIfAbsent() remove() replace() など
17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
どの位のサイズ?
18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
http://tinyurl.com/7nkqzxc
Java SE/EE 何れの環境でも利用可能
※ Java SE 6/EE 6 以降
19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
<dependency>! <groupId> javax.cache </groupId>! <artifactId> cache-api </artifactId>! <version> 0.5 </version> ← (2012/03/13 リリース)! </dependency>
スタート・ポイント
20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
Map に対する Cache の利点
• Atomic オペレーション (java.util.ConcurrentMapと類似) • Read-through キャッシュ • Write-through キャッシュ • ライフサイクル • キャッシュ・イベントリスナー • 統計情報
21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
オプションの機能
• トランザクション処理 (isolationレベルの指定) • アノテーション (Spring/CDI) • storeByReference (デフォルト storeByValue)
22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
主要なAPI
• CacheManager – キャッシュの検索、ライフサイクル管理など – すべてのキャッシュは CacheManager に含まれる – クラスタ環境では CacheMnagerがクラスタ化される
• CacheBuilder – 該当する CacheManager から Cacheを生成 – Cache に関する各種追加設定が可能
• 有効期限設定、統計情報の有効・無効、トランザクションの有効・無効
23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
主要なAPI
• Cache – キャッシュ操作用のメソッドを提供 – containsKey, get, getAll, getAndPut, getAndRemove, getAndReplace, getCacheManager, getConfiguration, getMBean, getName, getStatistics, invokeEntryProcess, iterator, load, loadAll, put, putAll, putifAbsent, registerCacheEntryListener, remove, removeAll, replace, unregisterCacheEntryListener, unwrap
24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
キャッシュのサンプル・コード String cacheName = "sampleCache";! CacheManager cacheManager = Caching.getCacheManager();! Cache<Integer, Date> cache = cacheManager.getCache(cacheName);!! if (cache == null) {! cache = cacheManager.! <Integer,Date>createCacheBuilder(cacheName).build();! }! Date value1 = new Date();! Integer key = 1;! cache.put(key, value1);! Date value2 = cache.get(key);!
25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 5
JSR-107 の詳細
26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
キャッシュのイベント機構
• CacheEntryCreatedListener • CacheEntryRemovedListener • CacheEntryReadListener • CacheEntryUpdatedListener • CacheEntryExpiredListener
27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
キャッシュのイベント機構
• CacheEntryEvent – Cache getSource() – K getKey() – V getValue()
28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
アノテーション
• @CacheResult • @CachePut • @CacheRemoveEntry • @CacheRemoveAll
29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
アノテーションを使用しない場合
private Cache<Long, User> cache; !! public User findUser(long id){! User user = cache.get(id);! if ( user == null ) {! user = …; // DB から指定された ID を元に検索! cache.put(id, user); //キャッシュに ID と User を登録! }! return user;! }!
30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
アノテーションを使用する場合
• キャッシュに存在しない場合 – メソッド内部処理を実行し実行結果をキャッシュ
• キャッシュに存在した場合 – メソッドの内部は実行せずキャッシュの内容を返す
@CacheResult(cacheName=”foo-cache")! public User findUser(long id) {! User user = …; //DB から指定された ID を元に検索! return user;! }!
31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
ビジネスロジックへの適用例 @CacheDefaults(cacheName="blogManager")!
public class BlogManager {!
@CacheResult!
public Blog getBlogEntry(String title) {...}!
@CachePut!
public void createEntry(@CacheKeyParam String title,!
@CacheValue Blog blog) {...}!
@CacheRemoveEntry!
public void removeBlogEntry(String title) {...}!
@CacheRemoveAll!
public void removeAllBlogs() {...}!
}
32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
トランザクション
• トランザクショナルリソースのキャッシュ • データベース更新のシナリオ
– データベース更新 – キャッシュ更新 – データベースコミット失敗!
• キャッシュとキャッシュ対象の同期が必要
33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 5
デモ
34 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
@Named!
@RequestScoped!
public class Eetter {!
@Inject!
private TwitterService twitter;!
private String query;!
private List<Tweet> tweets;!
private long searchTimeTaken;!
……続く!
Twitter検索結果のキャッシュ(CDI)
35 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
@Named!
@RequestScoped!
public class Eetter {!
……(続き)!
public void search() throws Exception {!
long begin = System.currentTimeMillis();!
tweets = twitter.search(query);!
searchTimeTaken = !
System.currentTimeMillis() - begin;!
}}!
}
Twitter検索結果のキャッシュ(CDI)
36 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
@CacheDefaults(cacheName="twitter")!
public class TwitterService {!
@CacheResult!
public List<Tweet> search(String queryString) !
throws TwitterException {!
List<Tweet> tweets = ...; // Twitter検索を実行 return tweets;!
}!
……続く
キャッシュ用のアノテーション実装例
37 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
……続き!
@CacheRemoveEntry!
public void removeCache(String queryString) { }!
!
@CacheRemoveAll!
public void clearCache() { }!
}
Twitter検索結果のキャッシュ(アノテーション)
38 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 5
まとめ
39 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
想定する各社の実装製品 • Terracotta - Ehcache • JBoss ‒ Infinispan • Oracle - Coherence • IBM ‒ WebSphere eXtreme Scale • SpringSource - Gemfire • GridGain • Google App Engine - memcache • その他
40 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
まとめ
• JSR 107 : JCACHE - Java Temporary Caching API – Java オブジェクトのキャッシュ – Java アプリケーションの高速化を実現 – 高いスケーラビリティ – DB キャッシュ以外の様々な場面で利用可能 – ConcurrentMap のように簡単な操作性
41 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
参考情報
• JSR 107 – http://jcp.org/en/jsr/detail?id=107
• Early Draft 仕様 – http://tinyurl.com/7b5tenh
• ソースコード – https://github.com/jsr107
• スペックリード:Greg Luck のブログ – http://gregluck.com/blog
42 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
Russia 17–18 April 2012
India 3–4 May 2012
San Francisco September 30–October 4, 2012
43 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8
44 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8