JCSAdminBean admin = new JCSAdminBean(); LinkedList linkedList = admin.buildCacheInfo(); ListIterator iterator = linkedList.listIterator(); while (iterator.hasNext()) { CacheRegionInfo info = (CacheRegionInfo)iterator.next(); CompositeCache compCache = info.getCache(); System.out.println("Cache Name: " + compCache.getCacheName()); System.out.println("Cache Type: " + compCache.getCacheType()); System.out.println("Cache Misses (not found): " + compCache.getMissCountNotFound()); System.out.println("Cache Misses (expired): " + compCache.getMissCountExpired()); System.out.println("Cache Hits (memory): " + compCache.getHitCountRam()); System.out.println("Cache value: " + compCache.get(propId));
public JcsCache(final CompositeCache cache) throws CacheException { this(cache.getCacheName(), cache.getCacheAttributes()); }
/** * Retrieves A COPY OF the default element attributes used by this region. This does not provide * a reference to the element attributes. * <p> * Each time an element is added to the cache without element attributes, the default element * attributes are cloned. * <p> * @return the default element attributes used by this region. * @throws CacheException */ @Override public IElementAttributes getDefaultElementAttributes() throws CacheException { return this.getCacheControl().getElementAttributes(); }
private void expires(final K cacheKey) { final ICacheElement<K, V> elt = delegate.get(cacheKey); delegate.remove(cacheKey); for (final JCSListener<K, V> listener : listeners.values()) { listener.onExpired(Arrays.<CacheEntryEvent<? extends K, ? extends V>> asList(new JCSCacheEntryEvent<K, V>(this, EventType.REMOVED, null, cacheKey, elt.getVal()))); } }
CompositeCache<String, Integer> cache = new CompositeCache<String, Integer>( cattr, attr ); @SuppressWarnings("unchecked") AuxiliaryCache<String, Integer>[] aux = new AuxiliaryCache[] { diskMock }; cache.setAuxCaches( aux ); cache.update( element, false ); cache.dispose(); MockMemoryCache<String, Integer> memoryCache = (MockMemoryCache<String, Integer>) cache.getMemoryCache(); assertEquals( "Wrong number freed.", numToInsert, memoryCache.lastNumberOfFreedElements );
/** * 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() ); }
/** * 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 ); }
/** * Do not try to go remote or laterally for this get. * <p> * @param key * @return ICacheElement */ public ICacheElement<K, V> localGet( K key ) { return get( key, true ); }
/** * This returns data gathered for this region and all the auxiliaries it currently uses. * <p> * @return Statistics and Info on the Region. */ public ICacheStats getStatistics() { ICacheStats stats = new CacheStats(); stats.setRegionName( this.getCacheName() ); // store the composite cache stats first ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>(); elems.add(new StatElement<Integer>( "HitCountRam", Integer.valueOf(getHitCountRam()) ) ); elems.add(new StatElement<Integer>( "HitCountAux", Integer.valueOf(getHitCountAux()) ) ); stats.setStatElements( elems ); // memory + aux, memory is not considered an auxiliary internally int total = auxCaches.length + 1; ArrayList<IStats> auxStats = new ArrayList<IStats>(total); auxStats.add(getMemoryCache().getStatistics()); for ( AuxiliaryCache<K, V> aux : auxCaches ) { auxStats.add(aux.getStatistics()); } // store the auxiliary stats stats.setAuxiliaryCacheStats( auxStats ); return stats; }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_NotExpired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // subtract two seconds now -= 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertFalse( "Item should not have expired.", result ); }
/** * 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 ); }
/** * Gets the set of keys of objects currently in the group. * <p> * @param group * @return A Set of keys. */ @Override public Set<K> getGroupKeys( String group ) { Set<K> groupKeys = new HashSet<K>(); GroupId groupId = new GroupId( this.getCacheControl().getCacheName(), group ); for (GroupAttrName<K> gan : this.getCacheControl().getKeySet()) { if (gan.groupId.equals( groupId )) { groupKeys.add( gan.attrName ); } } return groupKeys; }
/** * 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 cache.getCacheName(); }
cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); MockMemoryCache<String, String> memory = (MockMemoryCache<String, String>)cache.getMemoryCache();
/** * Gets the ICompositeCacheAttributes of the cache region. * <p> * @return ICompositeCacheAttributes, the controllers config info, defined in the top section of * a region definition. */ @Override public ICompositeCacheAttributes getCacheAttributes() { return this.getCacheControl().getCacheAttributes(); }
/** * Standard update method. * <p> * @param ce * @throws IOException */ public void localUpdate( ICacheElement<K, V> ce ) throws IOException { update( ce, true ); }
/** * Flushes all cache items from memory to auxiliary caches and close the auxiliary caches. */ @Override public void dispose() { dispose( false ); }
/** * Build a map of all the matching elements in all of the auxiliaries and memory. Do not try to * go remote or laterally for this data. * <p> * @param pattern * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache for any matching keys */ public Map<K, ICacheElement<K, V>> localGetMatching( String pattern ) { return getMatching( pattern, true ); }
/** * Processes an update request. * <p> * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote * origination. * <p> * @param item * @param requesterId * @throws IOException */ @Override public void processUpdate( ICacheElement<K, V> item, long requesterId ) throws IOException { CompositeCache<K, V> cache = getCacheManager().getCache( item.getCacheName() ); boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency(); if ( keepLocal ) { cache.localUpdate( item ); } else { cache.update( item ); } }
/** * Processes a get request. * <p> * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote * origination. * <p> * @param cacheName * @param pattern * @param requesterId * @return Map * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> processGetMatching( String cacheName, String pattern, long requesterId ) throws IOException { CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet(); if ( keepLocal ) { return cache.localGetMatching( pattern ); } else { return cache.getMatching( pattern ); } }