/** * Trim the number of strong references to equal or below the number given * by the maxObjects parameter. */ private void trimStrongReferences() { int max = getCacheAttributes().getMaxObjects(); int startsize = strongReferences.size(); for (int cursize = startsize; cursize > max; cursize--) { ICacheElement<K, V> ce = strongReferences.poll(); waterfal(ce); } }
/** * Copies the item to memory if the memory size is greater than 0. Only spool if the memory * cache size is greater than 0, else the item will immediately get put into purgatory. * <p> * @param element * @throws IOException */ private void copyAuxiliaryRetrievedItemToMemory( ICacheElement<K, V> element ) throws IOException { if ( memCache.getCacheAttributes().getMaxObjects() > 0 ) { memCache.update( element ); } else { if ( log.isDebugEnabled() ) { log.debug( "Skipping memory update since no items are allowed in memory" ); } } }
/** * Copies the item to memory if the memory size is greater than 0. Only spool if the memory * cache size is greater than 0, else the item will immediately get put into purgatory. * <p> * @param element * @throws IOException */ private void copyAuxiliaryRetrievedItemToMemory( ICacheElement<K, V> element ) throws IOException { if ( memCache.getCacheAttributes().getMaxObjects() > 0 ) { memCache.update( element ); } else { if ( log.isDebugEnabled() ) { log.debug( "Skipping memory update since no items are allowed in memory" ); } } }
/** * Verify that we use a system property for a ${FOO} string in a value. * * @throws Exception * */ public void test1SystemPropertyInValueDelimiter() throws Exception { int maxMemory = 1234; System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_DISK_DIR", "system_set" ); System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_MAX_SIZE", String.valueOf( maxMemory ) ); JCS.setConfigFilename( "/TestSystemProperties.ccf" ); CacheAccess<String, String> cache = JCS.getInstance( "test1" ); assertEquals( "We should have used the system property for the memory size", maxMemory, cache .getCacheAttributes().getMaxObjects() ); System.clearProperty("MY_SYSTEM_PROPERTY_DISK_DIR"); System.clearProperty("MY_SYSTEM_PROPERTY_MAX_SIZE"); }
/** * Verify that we use a system property for a ${FOO} string in a value. * * @throws Exception * */ public void test1SystemPropertyInValueDelimiter() throws Exception { int maxMemory = 1234; System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_DISK_DIR", "system_set" ); System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_MAX_SIZE", String.valueOf( maxMemory ) ); JCS.setConfigFilename( "/TestSystemProperties.ccf" ); CacheAccess<String, String> cache = JCS.getInstance( "test1" ); assertEquals( "We should have used the system property for the memory size", maxMemory, cache .getCacheAttributes().getMaxObjects() ); System.clearProperty("MY_SYSTEM_PROPERTY_DISK_DIR"); System.clearProperty("MY_SYSTEM_PROPERTY_MAX_SIZE"); }
/** * Verify that the system properties are used. * @throws Exception * */ public void testSystemPropertyUsage() throws Exception { System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) ); JCS.setConfigFilename( "/TestSystemPropertyUsage.ccf" ); CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" ); manager = CompositeCacheManager.getInstance(); assertEquals( "System property value is not reflected", testValue, jcs.getCacheAttributes().getMaxObjects()); }
/** * Verify that the system properties are not used is specified. * * @throws Exception * */ public void testSystemPropertyUsage_inactive() throws Exception { System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) ); manager = CompositeCacheManager.getUnconfiguredInstance(); Properties props = PropertyLoader.loadProperties( "TestSystemPropertyUsage.ccf" ); manager.configure( props, false ); CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" ); assertEquals( "System property value should not be reflected", Integer.parseInt( props.getProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS ) ), jcs.getCacheAttributes().getMaxObjects()); } }
/** * Verify that the system properties are not used is specified. * * @throws Exception * */ public void testSystemPropertyUsage_inactive() throws Exception { System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) ); manager = CompositeCacheManager.getUnconfiguredInstance(); Properties props = PropertyLoader.loadProperties( "TestSystemPropertyUsage.ccf" ); manager.configure( props, false ); CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" ); assertEquals( "System property value should not be reflected", Integer.parseInt( props.getProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS ) ), jcs.getCacheAttributes().getMaxObjects()); } }
/** * Verify that we use a system property for a ${FOO} string in a value. We * define a propety in the cache.ccf file, but we do not have it as a system * property. The default value should be used, if one exists. * * @throws Exception * */ public void test2SystemPropertyMissingInValueDelimeter() throws Exception { System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_DISK_DIR", "system_set" ); CompositeCacheManager mgr = CompositeCacheManager.getUnconfiguredInstance(); mgr.configure( "/TestSystemProperties.ccf" ); CacheAccess<String, String> cache = JCS.getInstance( "missing" ); // TODO check against the actual default def assertEquals( "We should have used the default property for the memory size", 100, cache.getCacheAttributes() .getMaxObjects() ); System.clearProperty("MY_SYSTEM_PROPERTY_DISK_DIR"); }
/** * Verify that the system properties are used. * @throws Exception * */ public void testSystemPropertyUsage() throws Exception { System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) ); JCS.setConfigFilename( "/TestSystemPropertyUsage.ccf" ); CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" ); manager = CompositeCacheManager.getInstance(); assertEquals( "System property value is not reflected.", testValue, jcs.getCacheAttributes().getMaxObjects()); }
/** * Verify that we use a system property for a ${FOO} string in a value. We * define a propety in the cache.ccf file, but we do not have it as a system * property. The default value should be used, if one exists. * * @throws Exception * */ public void test2SystemPropertyMissingInValueDelimeter() throws Exception { System.getProperties().setProperty( "MY_SYSTEM_PROPERTY_DISK_DIR", "system_set" ); CompositeCacheManager mgr = CompositeCacheManager.getUnconfiguredInstance(); mgr.configure( "/TestSystemProperties.ccf" ); CacheAccess<String, String> cache = JCS.getInstance( "missing" ); // TODO check against the actual default def assertEquals( "We should have used the default property for the memory size", 100, cache.getCacheAttributes() .getMaxObjects() ); System.clearProperty("MY_SYSTEM_PROPERTY_DISK_DIR"); }
/** * Get stats. * <p> * @throws CacheException */ public void testGetStatsThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } String stats = cache.getStats(); // System.out.println( stats ); // TODO improve stats check assertTrue( "Should have 200 puts", stats.indexOf( "2000" ) != -1 ); }
/** * Get stats. * <p> * @throws CacheException */ public void testGetStatsThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } String stats = cache.getStats(); // System.out.println( stats ); // TODO improve stats check assertTrue( "Should have 200 puts", stats.indexOf( "2000" ) != -1 ); }
/** * Get stats. * <p> * @throws CacheException */ public void testGetStatsThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } String stats = cache.getStats(); //System.out.println( stats ); // TODO improve stats check assertTrue( "Should have 200 puts" + stats, stats.indexOf( "200" ) != -1 ); }
/** * Get stats. * <p> * @throws CacheException */ public void testGetStatsThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } String stats = cache.getStats(); //System.out.println( stats ); // TODO improve stats check assertTrue( "Should have 200 puts" + stats, stats.indexOf( "200" ) != -1 ); }
/** * put the max and clear. verify that no elements remain. * <p> * @throws CacheException */ public void testClearThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } cache.clear(); // Test that first items are not in the cache for ( int i = max; i >= 0; i-- ) { String value = cache.get( i + ":key" ); assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value ); } }
/** * put the max and clear. verify that no elements remain. * <p> * @throws CacheException */ public void testClearThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } cache.clear(); // Test that first items are not in the cache for ( int i = max; i >= 0; i-- ) { String value = cache.get( i + ":key" ); assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value ); } }
/** * put the max and clear. verify that no elements remain. * <p> * @throws CacheException */ public void testClearThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testClearThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } cache.clear(); // Test that first items are not in the cache for ( int i = max; i >= 0; i-- ) { String value = cache.get( i + ":key" ); assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value ); } }
/** * put the max and remove each. verify that they are all null. * <p> * @throws CacheException */ public void testPutRemoveThroughHub() throws CacheException { CacheAccess<String, String> cache = JCS.getInstance( "testPutRemoveThroughHub" ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max * 2; for ( int i = 0; i < items; i++ ) { cache.put( i + ":key", "myregion" + " data " + i ); } for ( int i = 0; i < items; i++ ) { cache.remove( i + ":key" ); } // Test that first items are not in the cache for ( int i = max; i >= 0; i-- ) { String value = cache.get( i + ":key" ); assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value ); } }
/** * Put half the max and clear. get the key array and verify that it has the correct number of * items. * <p> * @throws Exception */ public void testGetKeyArray() throws Exception { CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance(); cacheMgr.configure( "/TestSoftReferenceCache.ccf" ); CompositeCache<String, String> cache = cacheMgr.getCache( "testGetKeyArray" ); SoftReferenceMemoryCache<String, String> srmc = new SoftReferenceMemoryCache<String, String>(); srmc.initialize( cache ); int max = cache.getCacheAttributes().getMaxObjects(); int items = max / 2; for ( int i = 0; i < items; i++ ) { ICacheElement<String, String> ice = new CacheElement<String, String>( cache.getCacheName(), i + ":key", cache.getCacheName() + " data " + i ); ice.setElementAttributes( cache.getElementAttributes() ); srmc.update( ice ); } Set<String> keys = srmc.getKeySet(); assertEquals( "Wrong number of keys.", items, keys.size() ); }