use of org.hibernate.stat.CacheRegionStatistics in project hibernate-orm by hibernate.
the class SecondLevelCacheTest method testCache.
@Test
public void testCache() {
doInJPA(this::entityManagerFactory, entityManager -> {
entityManager.persist(new Person());
Person aPerson = new Person();
aPerson.setName("John Doe");
aPerson.setCode("unique-code");
entityManager.persist(aPerson);
return aPerson;
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa load by id");
// tag::caching-entity-jpa-example[]
Person person = entityManager.find(Person.class, 1L);
// end::caching-entity-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native load by id");
Session session = entityManager.unwrap(Session.class);
// tag::caching-entity-native-example[]
Person person = session.get(Person.class, 1L);
// end::caching-entity-native-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native load by natural-id");
Session session = entityManager.unwrap(Session.class);
// tag::caching-entity-natural-id-example[]
Person person = session.byNaturalId(Person.class).using("code", "unique-code").load();
// end::caching-entity-natural-id-example[]
assertNotNull(person);
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache");
// tag::caching-query-jpa-example[]
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.name = :name", Person.class).setParameter("name", "John Doe").setHint("org.hibernate.cacheable", "true").getResultList();
// end::caching-query-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache");
Session session = entityManager.unwrap(Session.class);
// tag::caching-query-native-example[]
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name = :name").setParameter("name", "John Doe").setCacheable(true).list();
// end::caching-query-native-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache region");
// tag::caching-query-region-jpa-example[]
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").getResultList();
// end::caching-query-region-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache");
Session session = entityManager.unwrap(Session.class);
// tag::caching-query-region-native-example[]
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").list();
// end::caching-query-region-native-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache store mode ");
// tag::caching-query-region-store-mode-jpa-example[]
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
// end::caching-query-region-store-mode-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache store mode");
Session session = entityManager.unwrap(Session.class);
// tag::caching-query-region-store-mode-native-example[]
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").setCacheMode(CacheMode.REFRESH).list();
// end::caching-query-region-store-mode-native-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
// tag::caching-statistics-example[]
Statistics statistics = session.getSessionFactory().getStatistics();
CacheRegionStatistics secondLevelCacheStatistics = statistics.getDomainDataRegionStatistics("query.cache.person");
long hitCount = secondLevelCacheStatistics.getHitCount();
long missCount = secondLevelCacheStatistics.getMissCount();
double hitRatio = (double) hitCount / (hitCount + missCount);
// end::caching-statistics-example[]
return hitRatio;
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache store mode");
Session session = entityManager.unwrap(Session.class);
// tag::caching-query-region-native-evict-example[]
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
// end::caching-query-region-native-evict-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
// tag::caching-management-cache-mode-entity-jpa-example[]
Map<String, Object> hints = new HashMap<>();
hints.put("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE);
hints.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
Person person = entityManager.find(Person.class, 1L, hints);
// end::caching-management-cache-mode-entity-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
// tag::caching-management-cache-mode-entity-native-example[]
session.setCacheMode(CacheMode.REFRESH);
Person person = session.get(Person.class, 1L);
// end::caching-management-cache-mode-entity-native-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
// tag::caching-management-cache-mode-query-jpa-example[]
List<Person> persons = entityManager.createQuery("select p from Person p", Person.class).setHint(QueryHints.HINT_CACHEABLE, "true").setHint("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE).setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
// end::caching-management-cache-mode-query-jpa-example[]
// tag::caching-management-evict-jpa-example[]
entityManager.getEntityManagerFactory().getCache().evict(Person.class);
// end::caching-management-evict-jpa-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
// tag::caching-management-cache-mode-query-native-example[]
List<Person> persons = session.createQuery("select p from Person p").setCacheable(true).setCacheMode(CacheMode.REFRESH).list();
// end::caching-management-cache-mode-query-native-example[]
// tag::caching-management-evict-native-example[]
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
// end::caching-management-evict-native-example[]
});
}
use of org.hibernate.stat.CacheRegionStatistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() throws Exception {
Session s = sessionFactory().openSession();
Transaction t = s.beginTransaction();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.persist(i);
t.commit();
s.close();
CacheRegionStatistics slcs = sessionFactory().getStatistics().getDomainDataRegionStatistics(Item.class.getName());
assertThat(slcs.getPutCount(), equalTo(1L));
assertTrue(sessionFactory().getCache().containsEntity(Item.class, i.getId()));
s = sessionFactory().openSession();
t = s.beginTransaction();
i = (Item) s.get(Item.class, i.getId());
assertThat(slcs.getHitCount(), equalTo(1L));
assertThat(slcs.getMissCount(), equalTo(0L));
i.setDescription("A bog standard item");
t.commit();
s.close();
assertThat(slcs.getPutCount(), equalTo(2L));
assertTrue(sessionFactory().getCache().containsEntity(Item.class, i.getId()));
final DomainDataRegionTemplate region = (DomainDataRegionTemplate) sessionFactory().getMetamodel().entityPersister(Item.class).getCacheAccessStrategy().getRegion();
final Object fromCache = region.getCacheStorageAccess().getFromCache(region.getEffectiveKeysFactory().createEntityKey(i.getId(), sessionFactory().getMetamodel().entityPersister(Item.class), sessionFactory(), null), (SharedSessionContractImplementor) s);
assertNotNull(fromCache);
ExtraAssertions.assertTyping(AbstractReadWriteAccess.Item.class, fromCache);
// assertThat( (String) map.get( "description" ), equalTo( "A bog standard item" ) );
// assertThat( (String) map.get( "name" ), equalTo( "widget" ) );
// cleanup
s = sessionFactory().openSession();
t = s.beginTransaction();
s.delete(i);
t.commit();
s.close();
}
Aggregations