@Override public void putIfAbsent(final K key, final V value) { cache.putIfAbsent(key, value); }
@Override public <K, V> boolean putIfAbsent(K key, V value) { return jCache.putIfAbsent(key, value); }
@Override @Nullable public ValueWrapper putIfAbsent(Object key, @Nullable Object value) { boolean set = this.cache.putIfAbsent(key, toStoreValue(value)); return (set ? null : get(key)); }
@Override @Nullable public ValueWrapper putIfAbsent(Object key, @Nullable Object value) { boolean set = this.cache.putIfAbsent(key, toStoreValue(value)); return (set ? null : get(key)); }
@Override public V put(final K key, final V value) throws CacheException { V previousValue; while (true) { previousValue = cache.get(key); if (previousValue == null) { if (cache.putIfAbsent(key, value)) { break; } } else { if (cache.replace(key, value)) { break; } } } return previousValue; }
@Test public void putIfAbsent() { expect(cache.putIfAbsent(1, "a")).isTrue(); changesOf(0, 1, 1, 0); expect(cache.putIfAbsent(1, "b")).isFalse(); changesOf(1, 0, 0, 0); }
@Test public void testExpiryConfiguration() { final AtomicBoolean expiryCreated = new AtomicBoolean(false); MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); configuration.setExpiryPolicyFactory(() -> { expiryCreated.set(true); return new CreatedExpiryPolicy(Duration.FIVE_MINUTES); }); Cache<String, String> cache = cacheManager.createCache("cache", configuration); cache.putIfAbsent("42", "The Answer"); cache.putIfAbsent("42", "Or not!?"); assertThat(expiryCreated.get(), is(true)); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheWriter); assertThat(testCache.putIfAbsent(1, "one"), is(false)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_existsInSor() throws Exception { assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verifyZeroInteractions(cacheLoader); verify(cacheWriter, times(1)).write(eq(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one"))); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception { assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verifyZeroInteractions(cacheLoader); verify(cacheWriter, times(1)).write(eq(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one"))); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception { assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoader).load(1); verify(cacheWriter, times(1)).write(eq(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one"))); }
/** * Tries to set the global cleanup node id to current node. * * @param grid Grid. * @param metaCache Meta cache. * * @return True if successfully set the flag indicating that current node performs the cleanup; otherwise false. */ private boolean trySetGlobalCleanupFlag(Ignite grid, final Cache<CleanupNodeId, UUID> metaCache) { final UUID localNodeId = grid.cluster().localNode().id(); while (true) { // Get the node performing cleanup. UUID nodeId = metaCache.get(CLEANUP_NODE_ID); if (nodeId == null) { if (metaCache.putIfAbsent(CLEANUP_NODE_ID, localNodeId)) return true; // Successfully reserved cleanup to local node. // Failed putIfAbsent: someone else may have started cleanup. Retry the check. continue; } if (nodeId.equals(localNodeId)) return false; // Current node already performs cleanup. if (grid.cluster().node(nodeId) != null) return false; // Another node already performs cleanup and is alive. // Node that performs cleanup has disconnected. if (metaCache.replace(CLEANUP_NODE_ID, nodeId, localNodeId)) return true; // Successfully replaced disconnected node id with our id. // Replace failed: someone else started cleanup. return false; } }
@Test public void testLoaderConfiguration() throws Exception { final AtomicBoolean loaderCreated = new AtomicBoolean(false); MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class).setReadThrough(true); configuration.setCacheLoaderFactory(() -> { loaderCreated.set(true); return new TestCacheLoader(); }); CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Cache<String, String> cache = cacheManager.createCache("cache", configuration); assertThat(loaderCreated.get(), is(true)); cache.putIfAbsent("42", "The Answer"); TestCacheLoader.seen.clear(); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(Collections.singleton("42"), true, future); future.get(); assertThat(TestCacheLoader.seen, contains("42")); }
@Override public <K, V> boolean putIfAbsent(K key, V value) { return jCache.putIfAbsent(key, value); }
@Override void execute(Cache<Object, Object> cache, Exchange exchange) { exchange.getIn().setHeader( JCacheConstants.RESULT, cache.putIfAbsent( exchange.getIn().getHeader(JCacheConstants.KEY), exchange.getIn().getBody()) ); } },
@Override @ManagedOperation(description = "Adds the key to the store") public boolean add(Object key) { return cache.putIfAbsent(key, true); }
@Override public boolean putIfAbsent(K key, V value) { return cache.putIfAbsent(keyTransformer.compact(key), valueTransformer.compact(value)); }
@Test public void putIfAbsent_Missing() { long now = System.currentTimeMillis(); Date key = new Date(now); Date value = new Date(now); assertTrue(cache.putIfAbsent(key, value)); assertSame(value, cache.get(key)); }
@Test public void putIfAbsent_Missing() { Long key = System.currentTimeMillis(); String value = "value" + key; assertTrue(cache.putIfAbsent(key, value)); assertEquals(value, cache.get(key)); }
private <K> void doPutIfAbsent(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, readCache.get(key)); assertTrue(writeCache.putIfAbsent(key, "one")); assertEquals("one", readCache.get(key)); assertFalse(writeCache.putIfAbsent(key, "uno")); assertEquals("one", readCache.get(key)); assertTrue(writeCache.remove(key)); assertEquals(null, readCache.get(key)); }