/** * Create an instance of the BlockDiskCache. * <p> * @param iaca the cache attributes for this cache * @param cacheMgr This allows auxiliaries to reference the manager without assuming that it is * a singleton. This will allow JCS to be a non-singleton. Also, it makes it easier * to test. * @param cacheEventLogger * @param elementSerializer * @return BlockDiskCache */ @Override public <K, V> BlockDiskCache<K, V> createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) { BlockDiskCacheAttributes idca = (BlockDiskCacheAttributes) iaca; if ( log.isDebugEnabled() ) { log.debug( "Creating DiskCache for attributes = " + idca ); } BlockDiskCache<K, V> cache = new BlockDiskCache<K, V>( idca ); cache.setCacheEventLogger( cacheEventLogger ); cache.setElementSerializer( elementSerializer ); return cache; } }
Set<K> matchingKeys = getKeyMatcher().getMatchingKeysFromArray( pattern, keyArray ); ICacheElement<K, V> element = processGet( key ); if ( element != null )
setElementSerializer( elementSerializer ); this.logCacheName = "Region [" + getCacheName() + "] "; this.fileName = getCacheName().replaceAll("[^a-zA-Z0-9-_\\.]", "_"); this.rootDirectory = cacheAttributes.getDiskPath(); getElementSerializer() ); getElementSerializer() ); boolean alright = verifyDisk(); this.reset(); setAlive(true); if ( log.isInfoEnabled() )
/** * Create an instance of the BlockDiskCache. * <p> * @param iaca the cache attributes for this cache * @param cacheMgr This allows auxiliaries to reference the manager without assuming that it is * a singleton. This will allow JCS to be a non-singleton. Also, it makes it easier * to test. * @param cacheEventLogger * @param elementSerializer * @return BlockDiskCache */ @Override public <K, V> BlockDiskCache<K, V> createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) { BlockDiskCacheAttributes idca = (BlockDiskCacheAttributes) iaca; if ( log.isDebugEnabled() ) { log.debug( "Creating DiskCache for attributes = " + idca ); } BlockDiskCache<K, V> cache = new BlockDiskCache<K, V>( idca, elementSerializer ); cache.setCacheEventLogger( cacheEventLogger ); return cache; } }
protected boolean processRemove( K key ) if ( !isAlive() ) removed = performPartialKeyRemoval((String) key); removed = performGroupRemoval(((GroupAttrName<?>) key).groupId); removed = performSingleKeyRemoval(key); reset();
cattr.setBlockSizeBytes(200); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, byte[]> diskCache = new BlockDiskCache<String, byte[]>(cattr); diskCache.update(new CacheElement<String, byte[]>(cacheName, "x", bytes)); assertNotNull(diskCache.get("x")); Thread.sleep(1000); ICacheElement<String, byte[]> afterElement = diskCache.get("x"); assertNotNull(afterElement);
/** * Add some items to the disk cache and then remove them one by one. * * @throws IOException */ public void testRemoveItems() throws IOException { BlockDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName("testRemoveItems"); cattr.setMaxKeySize(100); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, String> disk = new BlockDiskCache<String, String>(cattr); disk.processRemoveAll(); int cnt = 25; for (int i = 0; i < cnt; i++) { IElementAttributes eAttr = new ElementAttributes(); eAttr.setIsSpool(true); ICacheElement<String, String> element = new CacheElement<String, String>("testRemoveItems", "key:" + i, "data:" + i); element.setElementAttributes(eAttr); disk.processUpdate(element); } // remove each for (int i = 0; i < cnt; i++) { disk.remove("key:" + i); ICacheElement<String, String> element = disk.processGet("key:" + i); assertNull("Should not have received an element.", element); } }
public void testPutGetMatching_SmallWait() throws Exception { // SETUP int items = 200; String cacheName = "testPutGetMatching_SmallWait"; BlockDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMaxKeySize(100); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>(cattr); // DO WORK for (int i = 0; i <= items; i++) { diskCache.update(new CacheElement<String, String>(cacheName, i + ":key", cacheName + " data " + i)); } Thread.sleep(500); Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching("1.8.+"); // VERIFY assertEquals("Wrong number returned", 10, matchingResults.size()); // System.out.println( "matchingResults.keySet() " + matchingResults.keySet() ); // System.out.println( "\nAFTER TEST \n" + diskCache.getStats() ); }
public void testAppendToDisk() throws Exception { String cacheName = "testAppendToDisk"; BlockDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMaxKeySize(100); cattr.setBlockSizeBytes(500); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, X> diskCache = new BlockDiskCache<String, X>(cattr); diskCache.removeAll(); X value1 = new X(); value1.string = "1234567890"; X value2 = new X(); value2.string = "0987654321"; diskCache.update(new CacheElement<String, X>(cacheName, "1", value1)); diskCache.dispose(); diskCache = new BlockDiskCache<String, X>(cattr); diskCache.update(new CacheElement<String, X>(cacheName, "2", value2)); diskCache.dispose(); diskCache = new BlockDiskCache<String, X>(cattr); assertTrue(diskCache.verifyDisk()); assertEquals(2, diskCache.getKeySet().size()); assertEquals(value1.string, diskCache.get("1").getVal().string); assertEquals(value2.string, diskCache.get("2").getVal().string); }
cattr.setBlockSizeBytes(500); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, X> diskCache = new BlockDiskCache<String, X>(cattr); diskCache.update(new CacheElement<String, X>(cacheName, "x" + i, before)); diskCache.dispose(); diskCache = new BlockDiskCache<String, X>(cattr); ICacheElement<String, X> afterElement = diskCache.get("x" + i); assertNotNull("Missing element from cache. Cache size: " + diskCache.getSize() + " element: x" + i, afterElement); X after = (afterElement.getVal()); diskCache.dispose();
private void innerTestPutKeys(BlockDiskCacheAttributes attributes) { BlockDiskCache<String, String> blockDiskCache = new BlockDiskCache<String, String>( attributes ); BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>( attributes, blockDiskCache ); // DO WORK int numElements = 100; for ( int i = 0; i < numElements; i++ ) { keyStore.put( String.valueOf( i ), new int[i] ); } // System.out.println( "testPutKeys " + keyStore ); // VERIFY assertEquals( "Wrong number of keys", numElements, keyStore.size() ); for ( int i = 0; i < numElements; i++ ) { int[] result = keyStore.get( String.valueOf( i ) ); assertEquals( "Wrong array returned.", i, result.length ); } }
/** * Resets the keyfile, the disk file, and the memory key map. * <p> * @see org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache#removeAll() */ @Override protected void processRemoveAll() { reset(); }
@Override public void run() { try { disposeInternal(); } catch ( InterruptedException e ) { log.warn( "Interrupted while diposing." ); } } };
cattr.setBlockSizeBytes( 200 ); cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" ); BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr ); diskCache.update( new CacheElement<String, String>( cacheName, "x", string ) ); assertNotNull( diskCache.get( "x" ) ); Thread.sleep( 1000 ); ICacheElement<String, String> afterElement = diskCache.get( "x" ); assertNotNull( afterElement );
cattr.setMaxKeySize(100); cattr.setDiskPath("target/test-sandbox/BlockDiskCacheUnitTest"); BlockDiskCache<String, String> disk = new BlockDiskCache<String, String>(cattr); disk.processRemoveAll(); + i); element.setElementAttributes(eAttr); disk.processUpdate(element); ICacheElement<String, String> element = disk.processGet(i + ":key"); assertNotNull("Shoulds have received an element.", element); disk.remove(i + ":"); ICacheElement<String, String> element = disk.processGet(i + ":key"); assertNull("Should not have received an element.", element);
public void testPutGetMatching_SmallWait() throws Exception { // SETUP int items = 200; String cacheName = "testPutGetMatching_SmallWait"; BlockDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName( cacheName ); cattr.setMaxKeySize( 100 ); cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" ); BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr ); // DO WORK for ( int i = 0; i <= items; i++ ) { diskCache.update( new CacheElement<String, String>( cacheName, i + ":key", cacheName + " data " + i ) ); } Thread.sleep( 500 ); Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching( "1.8.+" ); // VERIFY assertEquals( "Wrong number returned", 10, matchingResults.size() ); //System.out.println( "matchingResults.keySet() " + matchingResults.keySet() ); //System.out.println( "\nAFTER TEST \n" + diskCache.getStats() ); }
private void innerTestPutKeys(BlockDiskCacheAttributes attributes) { BlockDiskCache<String, String> blockDiskCache = new BlockDiskCache<String, String>(attributes); BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, blockDiskCache); // DO WORK int numElements = 100; for (int i = 0; i < numElements; i++) { keyStore.put(String.valueOf(i), new int[i]); } // System.out.println( "testPutKeys " + keyStore ); // VERIFY assertEquals("Wrong number of keys", numElements, keyStore.size()); for (int i = 0; i < numElements; i++) { int[] result = keyStore.get(String.valueOf(i)); assertEquals("Wrong array returned.", i, result.length); } }
/** * Resets the keyfile, the disk file, and the memory key map. * <p> * @see org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache#doRemoveAll() */ @Override protected void processRemoveAll() { reset(); }
@Override public void run() { try { disposeInternal(); } catch ( InterruptedException e ) { log.warn( "Interrupted while diposing." ); } } };