net.spy.memcached.MemcachedClientIF Java Examples

The following examples show how to use net.spy.memcached.MemcachedClientIF. 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: MemcachedCacheManager.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    Cache cache = getCache(CacheConstants.QUERY_CACHE);
    MemcachedClientIF cacheClient = (MemcachedClientIF) cache.getNativeCache();
    Collection<SocketAddress> liveServers = cacheClient.getAvailableServers();
    Collection<SocketAddress> deadServers = cacheClient.getUnavailableServers();
    if (liveServers.isEmpty()) {
        clusterHealth.set(false);
        logger.error("All the servers in MemcachedCluster is down, UnavailableServers: " + deadServers);
    } else {
        clusterHealth.set(true);
        if (deadServers.size() > liveServers.size()) {
            logger.warn("Half of the servers in MemcachedCluster is down, LiveServers: " + liveServers
                    + ", UnavailableServers: " + deadServers);
        }
    }
}
 
Example #2
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void get_witValueLoaderAndNonExistingValue_createsValueFromValueLoaderAndStoresItInCache()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	cache.setExpiration(42);

	when(client.set("myKey", 42, "createdValue")).thenReturn(new AsyncResult<>(true));

	// Act
	String value = cache.get("myKey", () -> "createdValue");

	// Assert
	assertThat(value).isEqualTo("createdValue");
}
 
Example #3
Source File: MemcachedCacheManager.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    Cache cache = getCache(CacheConstants.QUERY_CACHE);
    MemcachedClientIF cacheClient = (MemcachedClientIF) cache.getNativeCache();
    Collection<SocketAddress> liveServers = cacheClient.getAvailableServers();
    Collection<SocketAddress> deadServers = cacheClient.getUnavailableServers();
    if (liveServers.isEmpty()) {
        clusterHealth.set(false);
        logger.error("All the servers in MemcachedCluster is down, UnavailableServers: " + deadServers);
    } else {
        clusterHealth.set(true);
        if (deadServers.size() > liveServers.size()) {
            logger.warn("Half of the servers in MemcachedCluster is down, LiveServers: " + liveServers
                    + ", UnavailableServers: " + deadServers);
        }
    }
}
 
Example #4
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void get_witValueLoaderAndExistingValue_doesNotCallValueLoader() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	when(client.get("myKey")).thenReturn("existingValue");

	// Act
	String value = cache.get("myKey", () -> {
		throw new UnsupportedOperationException("Should not be called");
	});

	// Assert
	assertThat(value).isEqualTo("existingValue");
}
 
Example #5
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withTypeParameterAndNonCompatibleInstance_reportsIllegalArgumentException()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	when(client.get("test")).thenReturn(23L);

	// Assert
	assertThatThrownBy(() -> cache.get("test", String.class))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining(
					"java.lang.Long is not assignable to class java.lang.String");
}
 
Example #6
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void putIfAbsent_withExistingValue_shouldNotPutTheValueAndReturnTheExistingOne()
		throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	when(client.get("key")).thenReturn("value");

	// Act
	Cache.ValueWrapper valueWrapper = cache.putIfAbsent("key", "value");

	// Assert
	assertThat(valueWrapper.get()).isEqualTo("value");
}
 
Example #7
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withoutTypeParameterAndNonCompatibleCacheKey_reportsIllegalArgumentException()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.get(23L))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining(
					"java.lang.Long is not assignable to class java.lang.String");
}
 
Example #8
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withTypeParameterAndNonCompatibleCacheKey_reportsIllegalArgumentException()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.get(23L, Object.class))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining(
					"java.lang.Long is not assignable to class java.lang.String");
}
 
Example #9
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withoutTypeParameterAndNullCacheKey_reportsIllegalArgumentException()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.get(null))
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessage("key parameter is mandatory");
}
 
Example #10
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withTypeParameterAndNullCacheKey_reportsIllegalArgumentException()
		throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.get(null, Object.class))
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessage("key parameter is mandatory");
}
 
Example #11
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void put_nullCacheKey_reportsIllegalArgumentException() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.put(null, "test"))
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessage("key parameter is mandatory");
}
 
Example #12
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void put_longCacheKey_reportsIllegalArgumentException() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.put(23L, "test"))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining(
					"java.lang.Long is not assignable to class java.lang.String");
}
 
Example #13
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void put_nullCacheValueWithDefaultExpiration_keyStoredInCache() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	when(client.set("test", 0, null)).thenReturn(new AsyncResult<>(true));

	// Act
	cache.put("test", null);

	// Assert
	verify(client, times(1)).set("test", 0, null);
}
 
Example #14
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void put_withDefaultExpiration_keyStoredInCache() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	when(client.set("test", 0, "cachedElement")).thenReturn(new AsyncResult<>(true));

	// Act
	cache.put("test", "cachedElement");

	// Assert
	verify(client, times(1)).set("test", 0, "cachedElement");
}
 
Example #15
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void put_withCustomExpiration_keyStoredInCache() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	cache.setExpiration(42);
	when(client.set("test", 42, "cachedElement")).thenReturn(new AsyncResult<>(true));

	// Act
	cache.put("test", "cachedElement");

	// Assert
	verify(client, times(1)).set("test", 42, "cachedElement");
}
 
Example #16
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void evict_nullCacheKey_reportsIllegalArgumentException() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.evict(null))
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessage("key parameter is mandatory");
}
 
Example #17
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void evict_longCacheKey_reportsIllegalArgumentException() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Assert
	assertThatThrownBy(() -> cache.evict(23L))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining(
					"java.lang.Long is not assignable to class java.lang.String");

}
 
Example #18
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void evict_withCacheKey_deletedObjectInCache() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	when(client.delete("test")).thenReturn(new AsyncResult<>(true));

	// Act
	cache.evict("test");

	// Assert
	verify(client, times(1)).delete("test");
}
 
Example #19
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void clear_withDefaultSettings_flushesCache() throws Exception {

	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Act
	cache.clear();

	// Assert
	verify(client, times(1)).flush();
}
 
Example #20
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void putIfAbsent_withNewValue_shouldPutTheNewValueAndReturnNull() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");
	when(client.add("key", 0, "value")).thenReturn(new AsyncResult<>(true));

	// Act
	Cache.ValueWrapper valueWrapper = cache.putIfAbsent("key", "value");

	// Assert
	assertThat(valueWrapper).isNull();
}
 
Example #21
Source File: MemcachedCache.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public MemcachedCache(final MemcachedClientIF client, final MemcachedCacheConfig config,
        final String memcachedPrefix, int timeToLiveSeconds) {
    Preconditions.checkArgument(memcachedPrefix.length() <= MAX_PREFIX_LENGTH,
            "memcachedPrefix length [%d] exceeds maximum length [%d]", memcachedPrefix.length(), MAX_PREFIX_LENGTH);
    this.memcachedPrefix = memcachedPrefix;
    this.client = client;
    this.config = config;
    this.compressThreshold = config.getMaxObjectSize() / 2;
    this.timeToLiveSeconds = timeToLiveSeconds;
}
 
Example #22
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withTypeParameterAndFoundInstance_returnsConvertedValue() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	when(client.get("test")).thenReturn("cachedValue");

	// Act
	String cachedElement = cache.get("test", String.class);

	// Assert
	assertThat(cachedElement).isEqualTo("cachedValue");
}
 
Example #23
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withoutTypeParameterAndNonFoundInstance_returnsValue() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Act
	Cache.ValueWrapper valueWrapper = cache.get("test");

	// Assert
	assertThat(valueWrapper).isNull();
}
 
Example #24
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withoutTypeParameterAndFoundInstance_returnsValueWrapperWithInstance()
		throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	when(client.get("test")).thenReturn("cachedValue");

	// Act
	Cache.ValueWrapper valueWrapper = cache.get("test");

	// Assert
	assertThat(valueWrapper.get()).isSameAs("cachedValue");
}
 
Example #25
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getNativeCache_withConfiguredMemcachedClient_returnsConfiguredMemcachedClient()
		throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Act
	Object nativeCache = cache.getNativeCache();

	// Assert
	assertThat(nativeCache).isSameAs(client);
}
 
Example #26
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void simpleSpringMemcached_withoutName_reportsError() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);

	// Assert
	assertThatThrownBy(() -> new SimpleSpringMemcached(client, null))
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessage("cacheName is mandatory");

}
 
Example #27
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getName_configuredName_configuredNameReturned() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Act
	String cacheName = cache.getName();

	// Assert
	assertThat(cacheName).isEqualTo("test");
}
 
Example #28
Source File: ClientMemcachedProtocolSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @return Memcache client.
 * @throws Exception If start failed.
 */
private MemcachedClientIF startClient() throws Exception {
    int port = customPort != null ? customPort : IgniteConfiguration.DFLT_TCP_PORT;

    return new MemcachedClient(new BinaryConnectionFactory(),
        F.asList(new InetSocketAddress(LOC_HOST, port)));
}
 
Example #29
Source File: MemcachedCache.java    From kylin with Apache License 2.0 5 votes vote down vote up
public MemcachedCache(final MemcachedClientIF client, final MemcachedCacheConfig config,
        final String memcachedPrefix, int timeToLiveSeconds) {
    Preconditions.checkArgument(memcachedPrefix.length() <= MAX_PREFIX_LENGTH,
            "memcachedPrefix length [%d] exceeds maximum length [%d]", memcachedPrefix.length(), MAX_PREFIX_LENGTH);
    this.memcachedPrefix = memcachedPrefix;
    this.client = client;
    this.config = config;
    this.compressThreshold = config.getMaxObjectSize() / 2;
    this.timeToLiveSeconds = timeToLiveSeconds;
}
 
Example #30
Source File: SimpleSpringMemcachedTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void get_withTypeParameterAndNonFoundInstance_returnsValue() throws Exception {
	// Arrange
	MemcachedClientIF client = mock(MemcachedClientIF.class);
	SimpleSpringMemcached cache = new SimpleSpringMemcached(client, "test");

	// Act
	String cachedElement = cache.get("test", String.class);

	// Assert
	assertThat(cachedElement).isNull();
}