public void add(String key, Object value, int expiration) { if (ehCache.get(key) != null) { return; } Element element = new Element(key, value); element.setTimeToLive(expiration); ehCache.put(element); }
public void replace(String key, Object value, int expiration) { if (ehCache.get(key) == null) { return; } Element element = new Element(key, value); element.setTimeToLive(expiration); ehCache.put(element); }
public void set(String key, Object value, int expiration) { Element element = new Element(key, value); element.setTimeToLive(expiration); ehCache.put(element); }
public synchronized long decr(String key, int by) { Element e = ehCache.get(key); if (e == null) { return -1; } long newValue = ((Number) e.getObjectValue()).longValue() - by; Element newE = new Element(key, newValue); newE.setTimeToLive(e.getTimeToLive()); ehCache.put(newE); return newValue; }
public synchronized long incr(String key, int by) { Element e = ehCache.get(key); if (e == null) { return -1; } long newValue = ((Number) e.getObjectValue()).longValue() + by; Element newE = new Element(key, newValue); newE.setTimeToLive(e.getTimeToLive()); ehCache.put(newE); return newValue; }
@Test public void testExpiredElements() throws Exception { Assume.group(TestGroup.LONG_RUNNING); String key = "brancusi"; String value = "constantin"; Element brancusi = new Element(key, value); // ttl = 10s brancusi.setTimeToLive(3); nativeCache.put(brancusi); assertEquals(value, cache.get(key).get()); // wait for the entry to expire Thread.sleep(5 * 1000); assertNull(cache.get(key)); }
/** * Constructor * * @param key any non null value * @param value any value, including nulls * @param timeToIdleSeconds seconds to idle * @param timeToLiveSeconds seconds to live * @since 2.7.1 */ public Element(final Object key, final Object value, final int timeToIdleSeconds, final int timeToLiveSeconds) { this(key, value); setTimeToIdle(timeToIdleSeconds); setTimeToLive(timeToLiveSeconds); }
@Override public StoreEntry create(Long key, int timeToLive) { StoreEntryImpl result = new StoreEntryImpl(timeToLive); Element element = new Element(key, result); element.setTimeToLive(timeToLive); cache.put(element); return result; }
/** * Constructor used by ehcache-server * * timeToIdleSeconds and timeToLiveSeconds will have precedence over eternal. Which means that what ever eternal says, non-null * timeToIdleSeconds or timeToLiveSeconds will result in the element not being eternal * * @param key any non null value * @param value any value, including nulls * @param eternal specify as non-null to override cache configuration * @param timeToIdleSeconds specify as non-null to override cache configuration * @param timeToLiveSeconds specify as non-null to override cache configuration * * @deprecated */ @Deprecated public Element(final Object key, final Object value, final Boolean eternal, final Integer timeToIdleSeconds, final Integer timeToLiveSeconds) { this.key = key; this.value = value; if (eternal != null) { setEternal(eternal.booleanValue()); } if (timeToIdleSeconds != null) { setTimeToIdle(timeToIdleSeconds.intValue()); } if (timeToLiveSeconds != null) { setTimeToLive(timeToLiveSeconds.intValue()); } this.creationTime = getCurrentTime(); }
@Override public void replace(String key, Object value, int expiration) { if (cache.get(key) == null) { return; } Element element = new Element(key, value); element.setTimeToLive(expiration); cache.put(element); }
@Override public void set(String key, Object value, int expiration) { Element element = new Element(key, value); element.setTimeToLive(expiration); cache.put(element); }
@Override public void put(String key, Object value, int ttl) { Element element = new Element(key, value); if (0 >= ttl) ttl = defaultTTL; element.setTimeToLive(ttl); cache.put(element); }
@Override public void put(Object key, Object value) { if(key==null||value==null){ return ; } Element element = new Element(key,value); element.setTimeToLive(getExpireSecond(cachedUrl)); originCache.put(element); }
public void doPut( final String key, final Object element, final int expiration ) { Element el = new Element( key, element ); if ( expiration > -1 ) { el.setTimeToLive( expiration ); } getEHCache().put( el ); }
@Override public void put(Object key, Object value) { if(key==null||value==null){ return ; } Element element = new Element(key,value); element.setTimeToLive(getExpireSecond(cachedUrl)); originCache.put(element); }
private void expireElement(Element element, int adjustTTL) { long lastAccessTime = element.getLastAccessTime(); long creationTime = element.getCreationTime(); // set element eviction to ('now' + expiredTTL) seconds in the future int timeToLive = lastAccessTime == 0 ? expiredTTL : (int) (lastAccessTime - creationTime) / 1000 + expiredTTL; timeToLive += adjustTTL; element.setTimeToLive(timeToLive); }
private void updateTTL(Element dependentElement) { if (dependentElement == null) { return; } long lastAccessTime = dependentElement.getLastAccessTime(); long creationTime = dependentElement.getCreationTime(); int timeToLive = lastAccessTime == 0 ? cacheDependencyTTL : (int) (lastAccessTime - creationTime) / 1000 + cacheDependencyTTL; timeToLive += ADJUST_TTL; dependentElement.setTimeToLive(timeToLive); } }
@Override public void setTtl(String cacheName, Object key, int seconds) { Element element = getOrAddCache(cacheName).get(key); if (element == null) { return; } element.setTimeToLive(seconds); getOrAddCache(cacheName).put(element); }
@Override public void put(String cacheName, Object key, Object value, int liveSeconds) { if (liveSeconds <= 0) { put(cacheName, key, value); return; } Element element = new Element(key, value); element.setTimeToLive(liveSeconds); getOrAddCache(cacheName).put(element); }
private void applyDefaultsToElementWithoutLifespanSet(Element element) { if (!element.isLifespanSet()) { // Setting with Cache defaults // Eternal is always false element.setTimeToLive((int) timeToLiveSeconds); element.setTimeToIdle((int) timeToIdleSeconds); element.setEternal(false); } }