JCS jcs = JCSCacheFactory.getCacheInstance(regionName); IElementAttributes attributes = jcs.getDefaultElementAttributes(); attributes.addElementEventHandler(handler); jcs.setDefaultElementAttributes(attributes);
if ( !attributes.getIsEternal() ) long maxLifeSeconds = attributes.getMaxLife(); long createTime = attributes.getCreateTime(); final long timeFactorForMilliseconds = attributes.getTimeFactorForMilliseconds(); long idleTime = attributes.getIdleTime(); long lastAccessTime = attributes.getLastAccessTime();
private ICacheElement<K, V> updateElement(final K key, final V v, final Duration duration) { final ICacheElement<K, V> element = new CacheElement<K, V>(name, key, v); final IElementAttributes copy = delegate.getElementAttributes().copy(); if (duration != null) { copy.setTimeFactorForMilliseconds(1); final boolean eternal = duration.isEternal(); copy.setIsEternal(eternal); if (!eternal) { copy.setIdleTime(duration.getTimeUnit().toMillis(duration.getDurationAmount())); } // MaxLife = -1 to use IdleTime excepted if jcache.ccf asked for something else } element.setElementAttributes(copy); return element; }
private ICacheElement<K, V> updateElement(final K key, final V v, final Duration duration, final IElementAttributes attrs) { final ICacheElement<K, V> element = new CacheElement<K, V>(name, key, v); if (duration != null) { attrs.setTimeFactorForMilliseconds(1); final boolean eternal = duration.isEternal(); attrs.setIsEternal(eternal); if (!eternal) { attrs.setLastAccessTimeNow(); } // MaxLife = -1 to use IdleTime excepted if jcache.ccf asked for something else } element.setElementAttributes(attrs); return element; }
attributes.getIsEternal(), format.format(new Date(attributes.getCreateTime())), attributes.getMaxLife(), (now - attributes.getCreateTime() - attributes.getMaxLife() * 1000 ) / -1000);
/** * Verify that elements that go in the region after this call take the new attributes. * @throws Exception */ public void testSetDefaultElementAttributes() throws Exception { CacheAccess<String, String> access = JCS.getInstance( "test" ); assertNotNull( "We should have an access class", access ); long maxLife = 9876; IElementAttributes attr = new ElementAttributes(); attr.setMaxLife(maxLife); access.setDefaultElementAttributes( attr ); assertEquals( "Wrong element attributes.", attr.getMaxLife(), access.getDefaultElementAttributes() .getMaxLife() ); String key = "mykey"; String value = "myvalue"; access.put( key, value ); ICacheElement<String, String> element = access.getCacheElement( key ); assertEquals( "Wrong max life. Should have the new value.", maxLife, element.getElementAttributes() .getMaxLife() ); }
attr.setMaxLife(maxLife);
final ICacheElement<K, V> element = updateElement( // reuse it to create basic structure jcsKey, value, created ? null : duration, oldElt != null ? oldElt.getElementAttributes() : delegate.getElementAttributes().clone()); if (created && duration != null) { // set maxLife final IElementAttributes copy = element.getElementAttributes(); copy.setTimeFactorForMilliseconds(1); final boolean eternal = duration.isEternal(); copy.setIsEternal(eternal); if (ElementAttributes.class.isInstance(copy)) { ElementAttributes.class.cast(copy).setCreateTime(); copy.setIsEternal(false); if (duration == expiryPolicy.getExpiryForAccess()) element.getElementAttributes().setIdleTime(duration.getTimeUnit().toMillis(duration.getDurationAmount())); element.getElementAttributes().setMaxLife(duration.getTimeUnit().toMillis(duration.getDurationAmount()));
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_IdleTooLong() 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(100); element.getElementAttributes().setIdleTime( 1 ); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }
psUpdate.setBytes( 1, element ); Timestamp createTime = new Timestamp( ce.getElementAttributes().getCreateTime() ); psUpdate.setTimestamp( 2, createTime ); psUpdate.setLong( 3, now ); long expireTime = now + ce.getElementAttributes().getMaxLife(); psUpdate.setLong( 4, expireTime );
if (created && duration != null) { // set maxLife final IElementAttributes copy = element.getElementAttributes(); copy.setTimeFactorForMilliseconds(1); final boolean eternal = duration.isEternal(); copy.setIsEternal(eternal); if (!eternal) copy.setIsEternal(false); element.getElementAttributes().setMaxLife(duration.getTimeUnit().toMillis(duration.getDurationAmount()));
attrp.addElementEventHandler( hand ); cache_control.put( "key" + n, "data" + n + " put from ta = junk", attrp ); for ( int n = 0; n < num; n++ ) attrp.copy();
attrp.addElementEventHandler( hand ); cache_control.put( "key" + n, "data" + n + " put from ta = junk", attrp ); for ( int n = 0; n < num; n++ ) attrp.clone();
if ( !cacheElement.getElementAttributes().getIsEternal() ) final long lastAccessTime = attributes.getLastAccessTime();
if (expiryForAccess != null && (!elt.getElementAttributes().getIsEternal() || !expiryForAccess.isEternal()))
/** * Puts an item to the cache. * <p> * @param ce Description of the Parameter * @throws IOException Description of the Exception */ @Override public void update(ICacheElement<K, V> ce) throws IOException { putCnt.incrementAndGet(); ce.getElementAttributes().setLastAccessTimeNow(); lock.lock(); try { map.put(ce.getKey(), new SoftReferenceElementDescriptor<K, V>(ce)); strongReferences.add(ce); trimStrongReferences(); } finally { lock.unlock(); } }
/** * Test that cloned ElementAttributes have different creation times. * @throws Exception */ public void testElementAttributesCreationTime() throws Exception { ElementAttributes elem1 = new ElementAttributes(); long ctime1 = elem1.getCreateTime(); Thread.sleep(10); IElementAttributes elem2 = elem1.clone(); long ctime2 = elem2.getCreateTime(); assertFalse("Creation times should be different", ctime1 == ctime2); }
/** * Gets the defaultElementAttributes attribute of the CacheHub object * <p> * @return The defaultElementAttributes value */ public IElementAttributes getDefaultElementAttributes() { return this.defaultElementAttr.clone(); }
/** * Gets the defaultElementAttributes attribute of the CacheHub object * <p> * @return The defaultElementAttributes value */ public IElementAttributes getDefaultElementAttributes() { return this.defaultElementAttr.copy(); }
ArrayList<IElementEventHandler> eventHandlers = element.getElementAttributes().getElementEventHandlers(); if ( eventHandlers != null )