사용자 도구

사이트 도구


java:jpa:cache

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:jpa:cache [2014/06/08 00:03]
kwon37xi
java:jpa:cache [2015/10/17 12:07] (현재)
kwon37xi
줄 1: 줄 1:
 ====== JPA Second Level Cache ====== ====== JPA Second Level Cache ======
 +  * [[java:hibernate:cache|Hibernate Cache]]
   * Hibernate 4 기반으로 살펴보는 JPA2 Second Level Cache.   * Hibernate 4 기반으로 살펴보는 JPA2 Second Level Cache.
   * 아직까지 살펴본바로는, JPA 2의 [[http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html|@Cacheable]]애노테이션보다는 그냥 [[https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/annotations/Cache.html|@org.hibernate.annotations.Cache]]를 사용하고 쿼리 캐시 힌트로 hibernate 기반으로 사용하는게 나은 듯 하다.   * 아직까지 살펴본바로는, JPA 2의 [[http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html|@Cacheable]]애노테이션보다는 그냥 [[https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/annotations/Cache.html|@org.hibernate.annotations.Cache]]를 사용하고 쿼리 캐시 힌트로 hibernate 기반으로 사용하는게 나은 듯 하다.
   * 아래 나오는 Property Key의 대부분은 [[https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/cfg/Environment.html|Environment]] 클래스에 상수로 정의 돼 있다.   * 아래 나오는 Property Key의 대부분은 [[https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/cfg/Environment.html|Environment]] 클래스에 상수로 정의 돼 있다.
  
-===== Hibernate Cache 활성화 ===== +===== Evict All ===== 
-  * [[java:hibernate:cache|Hibernate Cache]] 도 함께 참조 +현재 Hibernate 구현체(4.3)은 JPA의 Cache.evictAll() 호출시에 모든 Region을 evict하지 않고 오로지 Entity 관련 region에 대해서만 evict를 수행한다.
-<code sh> +
-Map<String,Object> props = new HashMap<String,Object>(); +
-props.put(Environment.USE_SECOND_LEVEL_CACHE, true); // hibernate.cache.use_second_level_cache +
-props.put(Environment.USE_QUERY_CACHE, true); // hibernate.cache.use_query_cache +
-props.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName()); // hibernate.cache.region.factory_class - 캐시 구현체 지정 +
-props.put(Environment.CACHE_REGION_PREFIX, "cachetest"); // hibernate.cache.region_prefix +
-props.put(Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, CacheConcurrencyStrategy.READ_WRITE); // hibernate.cache.default_cache_concurrency_strategy +
-// ... +
-EntityManagerFactory emf = Persistence.createEntityManagerFactory("cachetest", props); +
-</code>+
  
 +전체 리젼을 다 evict하려면 Hibernate의 ''SessionFactory''의 ''Cache'' 구현체로 수행해야 한다.
 +관련 구현은 [[https://github.com/hibernate/hibernate-orm/blob/master/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java
 +|EntityManagerFactoryImpl.java]]에 있다.
  
-===== Entity Cache ===== 
 <code java> <code java>
-@Entity +// JPA 2.0사용시 
-@Table(name = "books"+HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory)entityManagerFactory; 
-@org.hibernate.annotations.Cache(usage CacheConcurrencyStrategy.READ_WRITE, region = "books") +org.hibernate.Cache cache hemf.getSessionFactory().getCache();
-public class Book implements Serializable { +
-.... +
-+
-</code>+
  
 +// JPA 2.1 사용시
 +SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class)
 +org.hibernate.Cache cache = sessionFactory.getCache();
  
-===== Query Cache ===== +// 전체 eviction 
-  * Query Cache는 기본적으로 region을 지정하지 않으면 ''org.hibernate.cache.internal.StandardQueryCache'' region에 캐시 결과를 저장한다. +cache.evictEntityRegions(); 
-  * ''setHint''를 통해 캐시하도록 지정<code java> +cache.evictQueryRegions(); 
-TypedQuery<Book> query = em.createNamedQuery("Book.byEdition", Book.class); +cache.evictCollectionRegions();
-query.setParameter("edition", 3); +
- +
-query.setHint("org.hibernate.cacheable", true); +
-query.setHint("org.hibernate.cacheRegion", "book-by-edition"); // region 지정+
 </code> </code>
  
-  * ''NamedQuery에 지정''<code java> +===== 참고 ===== 
-@Entity +  * [[http://www.developer.com/java/using-second-level-caching-in-a-jpa-application.html|Using Second Level Caching in a JPA Application - Developer.com]] 
-@Table(name "books"+
-@org.hibernate.annotations.Cache(usage CacheConcurrencyStrategy.READ_WRITE, region "books"+
-@NamedQuery(name "Book.byEdition", +
-        query "from Book where edition=:edition", +
-        hints +
-                @QueryHint(name "org.hibernate.cacheable", value "true"), +
-                @QueryHint(name = "org.hibernate.cacheRegion", value = "book-by-edition"+
-        } +
-+
-public class Book implements Serializable { +
-.... +
-+
-</code>+
java/jpa/cache.1402153398.txt.gz · 마지막으로 수정됨: 2014/06/08 00:03 저자 kwon37xi