@Test void putIfAbsent_withNewValue_shouldPutTheNewValueAndReturnNull() throws Exception { MemcachedClientIF client = mock(MemcachedClientIF.class); SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test"); when(client.add("key", 0, "value")).thenReturn(new AsyncResult<>(true)); Cache.ValueWrapper valueWrapper = cache.putIfAbsent("key", "value"); assertThat(valueWrapper).isNull(); }
@Override public ValueWrapper putIfAbsent(Object key, Object value) { Assert.notNull(key, "key parameter is mandatory"); Assert.isAssignable(String.class, key.getClass()); ValueWrapper valueWrapper = get(key); if (valueWrapper == null) { try { this.memcachedClientIF.add((String) key, this.expiration, value).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalArgumentException("Error writing key" + key, e); } return null; } else { return valueWrapper; } }
SimpleSpringMemcached implements Cache { @Override public ValueWrapper putIfAbsent(Object key, Object value) { Assert.notNull(key, "key parameter is mandatory"); Assert.isAssignable(String.class, key.getClass()); ValueWrapper valueWrapper = get(key); if (valueWrapper == null) { try { this.memcachedClientIF.add((String) key, this.expiration, value).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalArgumentException("Error writing key" + key, e); } return null; } else { return valueWrapper; } } }
SimpleSpringMemcached implements Cache { @Override public ValueWrapper putIfAbsent(Object key, Object value) { Assert.notNull(key, "key parameter is mandatory"); Assert.isAssignable(String.class, key.getClass()); ValueWrapper valueWrapper = get(key); if (valueWrapper == null) { try { this.memcachedClientIF.add((String) key, this.expiration, value).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalArgumentException("Error writing key" + key, e); } return null; } else { return valueWrapper; } } SimpleSpringMemcached(MemcachedClientIF memcachedClientIF, String cacheName); }
SimpleSpringMemcached implements Cache { @Override public ValueWrapper putIfAbsent(Object key, Object value) { Assert.notNull(key, "key parameter is mandatory"); Assert.isAssignable(String.class, key.getClass()); ValueWrapper valueWrapper = get(key); if (valueWrapper == null) { try { this.memcachedClientIF.add((String) key, this.expiration, value).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalArgumentException("Error writing key" + key, e); } return null; } else { return valueWrapper; } } SimpleSpringMemcached(MemcachedClientIF memcachedClientIF, String cacheName); @Override String getName(); @Override Object getNativeCache(); @Override ValueWrapper get(Object key); @Override T get(Object key, Class<T> type); @SuppressWarnings("unchecked") @Override T get(Object key, Callable<T> valueLoader); @Override void put(Object key, Object value); @Override ValueWrapper putIfAbsent(Object key, Object value); @Override void evict(Object key); @Override void clear(); void setExpiration(int expiration); }
SimpleSpringMemcached implements Cache { @Override public ValueWrapper putIfAbsent(Object key, Object value) { Assert.notNull(key, "key parameter is mandatory"); Assert.isAssignable(String.class, key.getClass()); ValueWrapper valueWrapper = get(key); if (valueWrapper == null) { try { this.memcachedClientIF.add((String) key, this.expiration, value).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalArgumentException("Error writing key" + key, e); } return null; } else { return valueWrapper; } } SimpleSpringMemcached(MemcachedClientIF memcachedClientIF, String cacheName); @Override String getName(); @Override Object getNativeCache(); @Override ValueWrapper get(Object key); @Override T get(Object key, Class<T> type); @SuppressWarnings("unchecked") @Override T get(Object key, Callable<T> valueLoader); @Override void put(Object key, Object value); @Override ValueWrapper putIfAbsent(Object key, Object value); @Override void evict(Object key); @Override void clear(); void setExpiration(int expiration);  }