public Object run() { return cache.putIfAbsent(key, value); } };
@Override public V putIfAbsent(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) { return this.cache.putIfAbsent(key, value, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit); }
@Override public boolean markAsDelivered(NotificationId notificationId, SubscriptionId subscriptionId) { return this.cache.putIfAbsent(notificationId, subscriptionId) == null; }
@Override public void execute() { V existing = cache.putIfAbsent(key, value); if (existing != null) { throw new IllegalStateException("There is already existing value in cache for key " + key); } }
@Override public Void invoke(Cache<SessionKey, Map<FullyQualifiedSessionId, Void>> cache) { cache.putIfAbsent(key, null).put(sessionId, null); return null; } };
@Override public boolean execute(Cache cache, String sharedKey, Object existing, String targetValue) { try { Object o = cache.putIfAbsent(SHARED_KEY, targetValue); return o == null; } catch (CacheException e) { return false; } }
@Override public <K, V> void execute(Cache<K, V> cache, K key, V value1, V value2) { cache.putIfAbsent(key, value2); }
/** * Put a new session in the cache * @param ssoId session id * @param fullyQualifiedSessionId fully qualified session id * @throws Exception */ private void putSessionInCache(String ssoId, FullyQualifiedSessionId fullyQualifiedSessionId) throws Exception { SessionKey key = new SessionKey(ssoId); key.cast(this.cache).putIfAbsent(key, null).put(fullyQualifiedSessionId, null); }
public void testStoresPutIfAbsent() throws Exception { assertStores(0); cache.putIfAbsent("voooo", "doooo"); assertStores(1); cache.putIfAbsent("voooo", "no-doooo"); assertStores(1); }
public void testValuesForCacheLoader() { cache.putIfAbsent("k1", "v1"); List<String> copy1 = copyValues(cache); assertEquals(1, copy1.size()); assertEquals("v1", copy1.get(0)); cache.putIfAbsent("k2", "v2"); List<String> copy2 = copyValues(cache); assertEquals(2, copy2.size()); assertEquals(Arrays.asList("v1", "v2"), copy2); }
@Override public Void invoke(Cache<String, Map<Object, Object>> cache) { Map<Object, Object> map = cache.putIfAbsent(sessionId, null); SessionMapEntry.VERSION.put(map, Integer.valueOf(sessionData.getVersion())); SessionMapEntry.METADATA.put(map, sessionData.getMetadata()); SessionMapEntry.TIMESTAMP.put(map, sessionData.getTimestamp()); DistributedCacheManager.this.attributeStorage.store(map, sessionData); return null; } };
@SuppressWarnings("ConstantConditions") public void testPutNullParameters() { expectException(NullPointerException.class, "Null keys are not supported!", () -> cache.put(null, null)); expectException(NullPointerException.class, "Null values are not supported!", () -> cache.put("k", null)); expectException(NullPointerException.class, "Null keys are not supported!", () -> cache.put(null, "v")); //put if absent since it shares the same command as put expectException(NullPointerException.class, "Null keys are not supported!", () -> cache.putIfAbsent(null, null)); expectException(NullPointerException.class, "Null values are not supported!", () -> cache.putIfAbsent("k", null)); expectException(NullPointerException.class, "Null keys are not supported!", () -> cache.putIfAbsent(null, "v")); }
public void testIdleExpiryInPutIfAbsent() throws InterruptedException { Cache<String, String> cache = getCache(); long idleTime = EXPIRATION_TIMEOUT; assertNull(cache.putIfAbsent("k", "v", -1, MILLISECONDS, idleTime, MILLISECONDS)); assertEquals("v", cache.get("k")); timeService.advance(idleTime + 100); assertNull(cache.get("k")); cache.put("k", "v"); assertNotNull(cache.putIfAbsent("k", "v", -1, MILLISECONDS, idleTime, MILLISECONDS)); }
public void testPutIfAbsent() throws Throwable { Object k1 = getKeyForCache(0); tm(0).begin(); assertNull(cache(0).putIfAbsent(k1, "v1")); Transaction suspendedTx = tm(0).suspend(); cache(0).put(k1, "v2"); assertEquals(cache(0).get(k1), "v2"); assertEquals(cache(1).get(k1), "v2"); suspendedTx.commit(); assertEquals("v1", cache(0).get(k1)); assertEquals("v1", cache(1).get(k1)); }
public void testEmbeddedPutIfAbsentHotRodGet() { final Integer key = 3; Cache<Integer, String> embedded = getEmbeddedCache(); RemoteCache<Integer, String> remote = cacheFactory.getHotRodCache(); assertEquals(null, embedded.putIfAbsent(key, "v1")); assertEquals("v1", remote.get(key)); assertEquals("v1", embedded.putIfAbsent(key, "v2")); assertEquals("v1", remote.get(key)); assertEquals("v1", embedded.remove(key)); }
public void testPutIfAbsentFromNonOwner() { initAndTest(); Object retval = getFirstNonOwner("k1").putIfAbsent("k1", "value2"); assert "value".equals(retval); assertOnAllCachesAndOwnership("k1", "value"); c1.clear(); assertFalse(c1.getAdvancedCache().getLockManager().isLocked("k1")); assertFalse(c2.getAdvancedCache().getLockManager().isLocked("k1")); retval = getFirstNonOwner("k1").putIfAbsent("k1", "value2"); assert null == retval; assertOnAllCachesAndOwnership("k1", "value2"); } }
public void testPutIfAbsentFromMainOwner() { Object k = getKeyForCache(0); cache(0).put(k, "0"); cache(0).putIfAbsent(k, "1"); assertEquals(advancedCache(0).getDataContainer().get(k).getValue(), "0"); assertEquals(advancedCache(1).getDataContainer().get(k).getValue(), "0"); cache(0).remove(k); cache(0).putIfAbsent(k, "1"); assertEquals(advancedCache(0).getDataContainer().get(k).getValue(), "1"); assertEquals(advancedCache(1).getDataContainer().get(k).getValue(), "1"); } }
public void testPutIfAbsentFromMainOwner() { Object k = getKeyForCache(0); cache(0).put(k, "0"); cache(0).putIfAbsent(k, "1"); assertEquals(advancedCache(0).getDataContainer().get(k).getValue(), "0"); assertEquals(advancedCache(1).getDataContainer().get(k).getValue(), "0"); cache(0).remove(k); cache(0).putIfAbsent(k, "1"); assertEquals(advancedCache(0).getDataContainer().get(k).getValue(), "1"); assertEquals(advancedCache(1).getDataContainer().get(k).getValue(), "1"); } }
public void testDefaultLifespanPutIfAbsent() { cache().putIfAbsent(1, "v1"); expectCachedThenExpired(1, "v1"); cache().getAdvancedCache().putIfAbsent(2, "v2", new EmbeddedMetadata.Builder().build()); expectCachedThenExpired(2, "v2"); }
public void testAtomicPutIfAbsentFromNonOwnerWithFlag(Method m) throws Exception { String key = k(m), value = "value"; String replaced = getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).putIfAbsent(key, value); assertNull(replaced); //interesting case: fails to put as value exists, put actually missing in Store replaced = getFirstNonOwner(key).putIfAbsent(key, value); assertEquals(replaced, value); for (Cache<Object, String> c : caches) { assertEquals(replaced, c.get(key)); CacheLoader store = TestingUtil.getFirstLoader(c); assertFalse(store.contains(key)); } }