Java Code Examples for net.sf.ehcache.Element#setTimeToLive()

The following examples show how to use net.sf.ehcache.Element#setTimeToLive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EHCacheSPStateManager.java    From cxf-fediz with Apache License 2.0 6 votes vote down vote up
public void setRequestState(String relayState, RequestState state) {
    if (relayState == null || "".equals(relayState)) {
        return;
    }

    int parsedTTL = (int)ttl;
    if (ttl != (long)parsedTTL) {
        // Fall back to 60 minutes if the default TTL is set incorrectly
        parsedTTL = 3600;
    }

    Element element = new Element(relayState, state);
    element.setTimeToLive(parsedTTL);
    element.setTimeToIdle(parsedTTL);
    requestCache.put(element);
}
 
Example 2
Source File: EhCacheCacheTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example 3
Source File: EHCacheSPStateManager.java    From cxf-fediz with Apache License 2.0 6 votes vote down vote up
public void setResponseState(String securityContextKey, ResponseState state) {
    if (securityContextKey == null || "".equals(securityContextKey)) {
        return;
    }

    int parsedTTL = (int)ttl;
    if (ttl != (long)parsedTTL) {
        // Fall back to 5 minutes if the default TTL is set incorrectly
        parsedTTL = 60 * 5;
    }
    Element element = new Element(securityContextKey, state);
    element.setTimeToLive(parsedTTL);
    element.setTimeToIdle(parsedTTL);

    responseCache.put(element);
}
 
Example 4
Source File: EhCacheCacheTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example 5
Source File: EhcacheLimitStore.java    From cosmic with Apache License 2.0 5 votes vote down vote up
@Override
public StoreEntry create(final Long key, final int timeToLive) {
    final StoreEntryImpl result = new StoreEntryImpl(timeToLive);
    final Element element = new Element(key, result);
    element.setTimeToLive(timeToLive);
    cache.put(element);
    return result;
}
 
Example 6
Source File: EhCacheImpl.java    From restcommander with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: EhCacheImpl.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public synchronized long decr(String key, int by) {
    Element e = cache.get(key);
    if (e == null) {
        return -1;
    }
    long newValue = ((Number) e.getValue()).longValue() - by;
    Element newE = new Element(key, newValue);
    newE.setTimeToLive(e.getTimeToLive());
    cache.put(newE);
    return newValue;
}
 
Example 8
Source File: EhCacheImpl.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public void add(String key, Object value, int expiration) {
    if (cache.get(key) != null) {
        return;
    }
    Element element = new Element(key, value);
    element.setTimeToLive(expiration);
    cache.put(element);
}
 
Example 9
Source File: EhCache.java    From dubbo-plus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: ApiEhcache.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public void put(String key, Object value, int ttl) {
	if(!isCacheAlive())
		return;

	Element element = new Element(key,value);
	element.setTimeToLive(ttl);
	cache.put(element);
}
 
Example 11
Source File: EhCacheCacheTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 12
Source File: EhcacheTokenStore.java    From azeroth with Apache License 2.0 5 votes vote down vote up
public StoreEntry create(Key key, int timeToLive) {
    StoreEntryImpl result = new StoreEntryImpl(timeToLive);
    Element element = new Element(key, result);
    element.setTimeToLive(timeToLive);
    cache.put(element);
    return result;
}
 
Example 13
Source File: JbootEhcacheImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: EhCacheService.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Adds an entry in the cache.
 *
 * @param key        Item key.
 * @param value      Item value.
 * @param expiration Expiration time.
 */
@Override
public void set(String key, Object value, Duration expiration) {
    Element element = new Element(key, value);
    if (expiration == null) {
        element.setEternal(true);
    } else {
        element.setTimeToLive((int) expiration.getStandardSeconds());
    }
    cache.put(element);
}
 
Example 15
Source File: EhcacheDriver.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void putx(String key, V value, int seconds) {
	Element el = new Element(key, value);
	if (seconds > -1) {
		el.setTimeToLive(seconds);
	}
	((Ehcache) cache().getNativeCache()).put(el);
}
 
Example 16
Source File: EhcacheDriver.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void put(String key, String value, int seconds) {
	Element el = new Element(key, value);
	if (seconds > -1) {
		el.setTimeToLive(seconds);
	}
	((Ehcache) cache().getNativeCache()).put(el);
}
 
Example 17
Source File: TxleEhCache.java    From txle with Apache License 2.0 4 votes vote down vote up
public void putIfAbsent(TxleCacheType txleCacheType, String key, Object value, int timeout) {
    Element element = new Element(key, value);
    element.setTimeToLive(timeout);
    cacheManager.getCache(txleCacheType.toString()).putIfAbsent(element);
}
 
Example 18
Source File: EhCacheImpl.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public void set(String key, Object value, int expiration) {
    Element element = new Element(key, value);
    element.setTimeToLive(expiration);
    cache.put(element);
}
 
Example 19
Source File: EhCacheImpl.java    From hermes with Apache License 2.0 4 votes vote down vote up
@Override
public void set(String key, Object value, int expiration) {
	Element element = new Element(key, value);
	element.setTimeToLive(expiration);
	cache.put(element);
}
 
Example 20
Source File: TxleEhCache.java    From txle with Apache License 2.0 4 votes vote down vote up
public void put(TxleCacheType txleCacheType, String key, Object value, int timeout) {
    Element element = new Element(key, value);
    element.setTimeToLive(timeout);
    cacheManager.getCache(txleCacheType.toString()).put(element);
}