/** * Constructor for the ShrinkerThread object. * <p> * @param cache The MemoryCache which the new shrinker should watch. */ public ShrinkerThread( CompositeCache<K, V> cache ) { super(); this.cache = cache; long maxMemoryIdleTimeSeconds = cache.getCacheAttributes().getMaxMemoryIdleTimeSeconds(); if ( maxMemoryIdleTimeSeconds < 0 ) { this.maxMemoryIdleTime = -1; } else { this.maxMemoryIdleTime = maxMemoryIdleTimeSeconds * 1000; } this.maxSpoolPerRun = cache.getCacheAttributes().getMaxSpoolPerRun(); if ( this.maxSpoolPerRun != -1 ) { this.spoolLimit = true; } }
/** * 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); } }
Class<?> c = Class.forName( cattr.getMemoryCacheName() ); @SuppressWarnings("unchecked") // Need cast IMemoryCache<K, V> newInstance = (IMemoryCache<K, V>) c.newInstance();
/** * @see org.apache.commons.jcs.engine.behavior.IRequireScheduler#setScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) */ @Override public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutor) { if ( cacheAttr.isUseMemoryShrinker() ) { future = scheduledExecutor.scheduleAtFixedRate( new ShrinkerThread<K, V>(this), 0, cacheAttr.getShrinkerIntervalSeconds(), TimeUnit.SECONDS); } }
cache = (CompositeCache<K, V>) caches.get( cattr.getCacheName() ); if ( cache == null ) cattr.setCacheName( cattr.getCacheName() ); cache = configurator.parseRegion( this.getConfigurationProperties(), cattr.getCacheName(), this.defaultAuxValues, cattr ); caches.put( cattr.getCacheName(), cache );
/** * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the * item is not put to disk. */ public void testSpoolNotAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount ); }
/** * Verify we try a disk aux on a getMatching call. * <p> * @throws IOException */ public void testGetMatching_NotOnDisk() throws IOException { // SETUP int maxMemorySize = 0; String cacheName = "testGetMatching_NotOnDisk"; String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache"; ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMemoryCacheName( memoryCacheClassName ); cattr.setMaxObjects( maxMemorySize ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, Integer> cache = new CompositeCache<String, Integer>( cattr, attr ); MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<String, Integer>(); diskMock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, Integer>[] aux = new AuxiliaryCache[] { diskMock }; cache.setAuxCaches( aux ); // DO WORK cache.getMatching( "junk" ); // VERIFY assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount ); }
attributes.setCacheName(cacheName); attributes.setMaxObjects( maxObjects ); attributes.setSpoolChunkSize( 1 );
/** * Verify that we can get a region using the define region method with cache attributes. * @throws Exception */ public void testRegionDefinitonWithAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); CacheAccess<String, String> access = JCS.defineRegion( "testRegionDefinitonWithAttributes", ca ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
log.debug( "lateralcache in aux list: cattr " + cacheAttr.isUseLateral() ); if ( cacheAttr.isUseLateral() && cacheElement.getElementAttributes().getIsLateral() && !localOnly ) log.debug( "diskcache in aux list: cattr " + cacheAttr.isUseDisk() ); if ( cacheAttr.isUseDisk() && cacheAttr.getDiskUsagePattern() == DiskUsagePattern.UPDATE && cacheElement.getElementAttributes().getIsSpool() )
if ( cacheAttr.getDiskUsagePattern() == DiskUsagePattern.SWAP )
/** * Gets the cache attribute of the CacheHub object * <p> * @param cacheName * @param cattr * @return CompositeCache */ public <K, V> CompositeCache<K, V> getCache( String cacheName, ICompositeCacheAttributes cattr ) { cattr.setCacheName( cacheName ); return getCache( cattr, this.defaultElementAttr ); }
/** * Returns the cache (aka "region") name. * <p> * @return The cacheName value */ public String getCacheName() { String attributeCacheName = this.cacheAttributes.getCacheName(); if(attributeCacheName != null) return attributeCacheName; return cacheName; }
/** * For post reflection creation initialization * <p> * @param hub */ @Override public void initialize( CompositeCache<K, V> hub ) { hitCnt = new AtomicLong(0); missCnt = new AtomicLong(0); putCnt = new AtomicLong(0); this.cacheAttributes = hub.getCacheAttributes(); this.chunkSize = cacheAttributes.getSpoolChunkSize(); this.cache = hub; this.map = createMap(); this.status = CacheStatus.ALIVE; }
ccAttr = ccAttr2.copy(); ccAttr.setCacheName( regName ); ccAttr.setCacheName( regName ); return ccAttr;
/** * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the * item is not put to disk. */ public void testSpoolNotAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount ); }
/** * Verify we try a disk aux on a getMatching call. * <p> * @throws IOException */ public void testGetMatching_NotOnDisk() throws IOException { // SETUP int maxMemorySize = 0; String cacheName = "testGetMatching_NotOnDisk"; String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache"; ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMemoryCacheName( memoryCacheClassName ); cattr.setMaxObjects( maxMemorySize ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, Integer> cache = new CompositeCache<String, Integer>( cattr, attr ); MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<String, Integer>(); diskMock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, Integer>[] aux = new AuxiliaryCache[] { diskMock }; cache.setAuxCaches( aux ); // DO WORK cache.getMatching( "junk" ); // VERIFY assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount ); }
attributes.setCacheName(cacheName); attributes.setMaxObjects( maxObjects ); attributes.setSpoolChunkSize( 1 );
/** * Verify that we can get a region using the define region method with cache attributes. * @throws Exception */ public void testRegionDefinitonWithAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); CacheAccess<String, String> access = JCS.getInstance( "testRegionDefinitonWithAttributes", ca ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
log.debug( "lateralcache in aux list: cattr " + cacheAttr.isUseLateral() ); if ( cacheAttr.isUseLateral() && cacheElement.getElementAttributes().getIsLateral() && !localOnly ) if ( log.isDebugEnabled() ) log.debug( "diskcache in aux list: cattr " + cacheAttr.isUseDisk() ); if ( cacheAttr.isUseDisk() && cacheAttr.getDiskUsagePattern() == DiskUsagePattern.UPDATE && cacheElement.getElementAttributes().getIsSpool() )