/** * Second level cache statistics per region * * @param regionName region name * * @return SecondLevelCacheStatistics */ public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) { ConcurrentSecondLevelCacheStatisticsImpl slcs = (ConcurrentSecondLevelCacheStatisticsImpl) secondLevelCacheStatistics.get( regionName ); if ( slcs == null ) { if ( sessionFactory == null ) { return null; } Region region = sessionFactory.getSecondLevelCacheRegion( regionName ); if ( region == null ) { return null; } slcs = new ConcurrentSecondLevelCacheStatisticsImpl( region ); ConcurrentSecondLevelCacheStatisticsImpl previous; if ( ( previous = (ConcurrentSecondLevelCacheStatisticsImpl) secondLevelCacheStatistics.putIfAbsent( regionName, slcs ) ) != null ) { slcs = previous; } } return slcs; }
@Test public void testUpdateEventuallyInvalidatesObject() { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties updateDummyEntityName(sf, 2, "updated"); assertSizeEventually(9, dummyEntityCacheStats.getEntries()); }
@Test public void testGetUpdateRemoveGet() throws Exception { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties deleteDummyEntity(sf, 1); assertEquals(4, dummyPropertyCacheStats.getHitCount()); assertEquals(0, dummyPropertyCacheStats.getMissCount()); assertEquals(1, dummyEntityCacheStats.getHitCount()); assertEquals(10, dummyEntityCacheStats.getMissCount()); }
@Test public void testUpdateShouldNotInvalidateEntryInCache() { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities, 10 entities are cached getDummyEntities(sf, 10); //updates cache entity updateDummyEntityName(sf, 2, "updated"); assertEquals(10, dummyEntityCacheStats.getElementCountInMemory()); }
@Test public void test() { SessionFactory factory = SessionFactoryProvider .getInstance().provide(Context.class); Statistics statistics = factory.getStatistics(); SecondLevelCacheStatistics secondStat = factory.getStatistics().getSecondLevelCacheStatistics(GameUser.class.getName()); deleteAll(); logger.info("==========>>>deleteAll: second level cache hits: " + statistics.getSecondLevelCacheHitCount()); logger.info("==========>>>deleteAll: query execution hits: " + statistics.getQueryExecutionCount()); logger.info("==========>>>deleteAll: element count in mem: " + secondStat.getElementCountInMemory()); intsert5(); logger.info("==========>>>intsert5: second level cache hits: " + statistics.getSecondLevelCacheHitCount()); logger.info("==========>>>intsert5: query execution hits: " + statistics.getQueryExecutionCount()); logger.info("==========>>>intsert5: element count in mem: " + secondStat.getElementCountInMemory()); doSomeThing(); logger.info("==========>>>doSomeThing: second level cache hits: " + statistics.getSecondLevelCacheHitCount()); logger.info("==========>>>doSomeThing: query execution hits: " + statistics.getQueryExecutionCount()); logger.info("==========>>>doSomeThing: element count in mem: " + secondStat.getElementCountInMemory()); deleteAll(); logger.info("==========>>>deleteAll: second level cache hits: " + statistics.getSecondLevelCacheHitCount()); logger.info("==========>>>deleteAll: query execution hits: " + statistics.getQueryExecutionCount()); logger.info("==========>>>deleteAll: element count in mem: " + secondStat.getElementCountInMemory()); }
@Test public void testUpdateEventuallyInvalidatesObject() { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached final SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties updateDummyEntityName(sf, 2, "updated"); assertSizeEventually(9, dummyEntityCacheStats.getEntries()); }
/** * @param sesFactory Session factory. * @param idToChildCnt Number of children per entity. * @param expHit Expected cache hits. * @param expMiss Expected cache misses. */ @SuppressWarnings("unchecked") private void assertCollectionCache(SessionFactory sesFactory, Map<Integer, Integer> idToChildCnt, int expHit, int expMiss) { sesFactory.getStatistics().clear(); Session ses = sesFactory.openSession(); try { for(Map.Entry<Integer, Integer> e : idToChildCnt.entrySet()) { Entity entity = (Entity)ses.load(Entity.class, e.getKey()); assertEquals((int)e.getValue(), entity.getChildren().size()); } } finally { ses.close(); } SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION); assertEquals(expHit, stats.getHitCount()); assertEquals(expMiss, stats.getMissCount()); }
/** * Prints Hibernate L2 cache statistics to standard output. * * @param sesFactory Hibernate {@link SessionFactory}, for which to print * statistics. */ private static void printStats(SessionFactory sesFactory) { System.out.println("=== Hibernate L2 cache statistics ==="); for (String entityName : ENTITY_NAMES) { System.out.println("\tEntity: " + entityName); SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName); System.out.println("\t\tPuts: " + stats.getPutCount()); System.out.println("\t\tHits: " + stats.getHitCount()); System.out.println("\t\tMisses: " + stats.getMissCount()); } System.out.println("====================================="); }
@Test public void testGetUpdateRemoveGet() throws Exception { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties updateDummyEntityName(sf, 2, "updated"); //entity 2 and its properties are invalidated //miss updated entity, hit 4 properties(they are still the same) getPropertiesOfEntity(sf, 2); //hit 1 entity and 4 properties deleteDummyEntity(sf, 1); assertEquals(12, dummyPropertyCacheStats.getHitCount()); assertEquals(0, dummyPropertyCacheStats.getMissCount()); assertEquals(2, dummyEntityCacheStats.getHitCount()); assertEquals(11, dummyEntityCacheStats.getMissCount()); }
@Test public void testUpdateQueryCausesInvalidationOfEntireRegion() { insertDummyEntities(10); executeUpdateQuery(sf, "UPDATE DummyEntity set name = 'manually-updated' where id=2"); sf.getStatistics().clear(); getDummyEntities(sf, 10); SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); assertEquals(10, dummyEntityCacheStats.getMissCount()); assertEquals(0, dummyEntityCacheStats.getHitCount()); }
@Test public void testUpdateQueryCausesInvalidationOfEntireCollectionRegion() { insertDummyEntities(1, 10); //properties reference in DummyEntity is evicted because of custom SQL query on Collection region executeUpdateQuery(sf, "update DummyProperty ent set ent.key='manually-updated'"); sf.getStatistics().clear(); //property reference missed in cache. getPropertiesOfEntity(sf, 0); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY + ".properties"); assertEquals(0, dummyPropertyCacheStats.getHitCount()); assertEquals(1, dummyPropertyCacheStats.getMissCount()); }
@Test public void testGetUpdateRemoveGet() throws Exception { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties updateDummyEntityName(sf, 2, "updated"); //hit 1 entity, hit 4 properties getPropertiesOfEntity(sf, 2); //hit 1 entity and 4 properties deleteDummyEntity(sf, 1); assertEquals(12, dummyPropertyCacheStats.getHitCount()); assertEquals(0, dummyPropertyCacheStats.getMissCount()); assertEquals(3, dummyEntityCacheStats.getHitCount()); assertEquals(10, dummyEntityCacheStats.getMissCount()); }
@Test public void testGetUpdateRemoveGet() throws Exception { insertDummyEntities(10, 4); //all 10 entities and 40 properties are cached SecondLevelCacheStatistics dummyEntityCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_ENTITY); SecondLevelCacheStatistics dummyPropertyCacheStats = sf.getStatistics().getSecondLevelCacheStatistics(CACHE_PROPERTY); sf.getCache().evictEntityRegions(); sf.getCache().evictCollectionRegions(); //miss 10 entities getDummyEntities(sf, 10); //hit 1 entity and 4 properties updateDummyEntityName(sf, 2, "updated"); //invalidation is not synchronized, so we have to wait //entity 2 and its properties are invalidated //miss updated entity, hit 4 properties(they are still the same) getPropertiesOfEntity(sf, 2); //hit 1 entity and 4 properties deleteDummyEntity(sf, 1); assertEquals(12, dummyPropertyCacheStats.getHitCount()); assertEquals(0, dummyPropertyCacheStats.getMissCount()); assertEquals(2, dummyEntityCacheStats.getHitCount()); assertEquals(11, dummyEntityCacheStats.getMissCount()); }
public void testEmptySecondLevelCacheEntry() throws Exception { getSessions().evictEntity( Item.class.getName() ); Statistics stats = getSessions().getStatistics(); stats.clear(); SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics( Item.class.getName() ); Map cacheEntries = statistics.getEntries(); assertEquals( 0, cacheEntries.size() ); }
protected void printEntityCacheStats(String region, boolean printEntries) { SecondLevelCacheStatistics stats = getCacheStats(region); LOGGER.info(region + " Stats: \n\n\t" + stats + "\n"); if (printEntries) { @SuppressWarnings("rawtypes") Map cacheEntries = stats.getEntries(); LOGGER.info(Arrays.toString(cacheEntries.entrySet().toArray())); } }
protected SecondLevelCacheStatistics getCacheStats(String region) { SecondLevelCacheStatistics stats = getSessionFactory().getStatistics().getSecondLevelCacheStatistics(region); if (stats == null){ LOGGER.warn("No such cache: " + region); } return stats; }
@Test public void shouldCacheUserOnFind() { User first = new User("first"); first.addNotificationFilter(new NotificationFilter("pipline", "stage1", StageEvent.Fails, true)); first.addNotificationFilter(new NotificationFilter("pipline", "stage2", StageEvent.Fails, true)); int originalUserCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size(); int originalNotificationsCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters").getEntries().size(); userDao.saveOrUpdate(first); long userId = userDao.findUser("first").getId(); assertThat(sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size(), is(originalUserCacheSize + 1)); SecondLevelCacheStatistics notificationFilterCollectionCache = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters"); assertThat(notificationFilterCollectionCache.getEntries().size(), is(originalNotificationsCacheSize + 1)); assertThat(notificationFilterCollectionCache.getEntries().get(userId), is(Matchers.notNullValue())); }
public SecondLevelCacheStatistics getSecondLevelCacheStatistics( String regionName) { return null; }
private static SecondLevelCacheStatistics getCacheStatistics(String regionName) { return emf.unwrap(SessionFactory.class).getStatistics() .getSecondLevelCacheStatistics(regionName); }
/** * @see StatisticsServiceMBean#getSecondLevelCacheStatistics(java.lang.String) */ public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) { return stats.getSecondLevelCacheStatistics(regionName); }
public void testQueryCacheInvalidation() { Session s = 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(); SecondLevelCacheStatistics slcs = s.getSessionFactory().getStatistics() .getSecondLevelCacheStatistics( Item.class.getName() ); assertEquals( slcs.getPutCount(), 1 ); assertEquals( slcs.getElementCountInMemory(), 1 ); assertEquals( slcs.getEntries().size(), 1 ); s = openSession(); t = s.beginTransaction(); i = (Item) s.get( Item.class, i.getId() ); assertEquals( slcs.getHitCount(), 1 ); assertEquals( slcs.getMissCount(), 0 ); i.setDescription("A bog standard item"); t.commit(); s.close(); assertEquals( slcs.getPutCount(), 2 ); Object entry = slcs.getEntries().get( i.getId() ); Map map; if ( entry instanceof ReadWriteCache.Item ) { map = new HashMap(); map = (Map) ( (ReadWriteCache.Item) entry ).getValue(); } else { map = (Map) entry; } assertTrue( map.get("description").equals("A bog standard item") ); assertTrue( map.get("name").equals("widget") ); // cleanup s = openSession(); t = s.beginTransaction(); s.delete( i ); t.commit(); s.close(); }
protected void printCacheRegionStatistics(String region) { SecondLevelCacheStatistics statistics = sessionFactory().getStatistics().getSecondLevelCacheStatistics(region); LOGGER.debug("\nRegion: {},\nStatistics: {},\nEntries: {}", region, statistics, statistics.getEntries()); }
protected void printQueryCacheRegionStatistics() { SecondLevelCacheStatistics statistics = sessionFactory() .getStatistics() .getSecondLevelCacheStatistics(StandardQueryCache.class.getName()); StringBuilder queryCache = new StringBuilder(); for ( Object queryCacheEntry: statistics.getEntries().entrySet()) { if(queryCache.length() > 0) { queryCache.append( ", " ); } if(queryCacheEntry instanceof Map.Entry) { Map.Entry queryCacheEntryMap = (Map.Entry) queryCacheEntry; queryCache.append( "{" ); queryCache.append( queryCacheEntryMap.getKey() ); queryCache.append( "=" ); List values = (List) queryCacheEntryMap.getValue(); boolean first = true; queryCache.append( "[" ); for ( Object value: values) { if(first) { first = false; } else { queryCache.append( ", " ); } queryCache.append( Object[].class.isAssignableFrom( value.getClass() ) ? Arrays.toString( (Object[]) value ) : value ); } queryCache.append( "]" ); queryCache.append( "}" ); } } statistics.getEntries(); LOGGER.debug("\nRegion: {},\nStatistics: {},\nEntries: {}", StandardQueryCache.class.getName(), statistics, queryCache ); }
public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String arg0) { return delegate.getSecondLevelCacheStatistics( arg0 ); }
protected void printQueryCacheStats(String region) { SecondLevelCacheStatistics stats = getCacheStats(region); LOGGER.info(region + " Stats: \n\n\t" + stats + "\n"); }