Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=1.2.3
version=1.2.4
springVersion=5.1.8.RELEASE
springBootVersion=2.1.6.RELEASE
jerseyVersion=2.28
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions micro-elasticache/README.md → micro-memcached/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Elasticache plugin for BASE microservices

[micro-elasticache example apps](https://github.com/aol/micro-server/tree/master/micro-elasticache/src/test/java/app)
[micro-memcached example apps](https://github.com/aol/micro-server/tree/master/micro-memcached/src/test/java/app)

Basically Available Soft statE

Expand All @@ -24,11 +24,11 @@ elasticache.max.retries is the maximum number of retries before client throws er
```xml
<dependency>
<groupId>com.oath.microservices</groupId>
<artifactId>micro-elasticache</artifactId>
<artifactId>micro-memcached</artifactId>
<version>x.yz</version>
</dependency>
```
### Gradle
```groovy
compile 'com.oath.microservices:micro-elasticache:x.yz'
compile 'com.oath.microservices:micro-memcached:x.yz'
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description = 'micro-elasticache'
description = 'micro-memcached'

apply plugin: 'groovy'
apply plugin: 'java'
Expand Down Expand Up @@ -42,7 +42,7 @@ modifyPom {
inceptionYear '2015'

groupId 'com.oath.microservices'
artifactId 'micro-elasticache'
artifactId 'micro-memcached'
version "$version"


Expand Down Expand Up @@ -71,6 +71,11 @@ modifyPom {
name 'Gordon Morrow'
email '[email protected]'
}
developer {
id 'davidartplus'
name 'David Guzman'
email '[email protected]'
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.oath.micro.server.memcached;
import java.util.Optional;

public interface DistributedCache<K, V> {
void setConnectionTested(boolean result);
boolean isAvailable();
boolean add(K key, int exp, V value);
Optional<V> get(K key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.oath.micro.server.memcached;

import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetSocketAddress;

@Component
@Slf4j
public class DistributedCacheFactory {

private final ElasticacheConfig config;

@Autowired
public DistributedCacheFactory(ElasticacheConfig config) {
this.config = config;
}

public <K, V> DistributedCache<K, V> create() {
try {
log.info("Creating Memcached Data connection for elasticache cluster: {}", config.getHostname());
return new MemcachedCacheImpl(createMemcachedClient(), config.getRetryAfterSecs(), config.getMaxRetries());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess

return new MemcachedCacheImpl<K, V>(createMemcachedClient(), ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the compiler can deduce the types <K, V> from the return type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides, the IDE suggests it's not relevant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, thanks.

}
catch (Exception e) {
log.error("Failed to create transient data connection", e);
return null;
}
}

private MemcachedClient createMemcachedClient() {
try {
log.info("Starting an instance of memcache client towards elasticache cluster");
return new MemcachedClient(new InetSocketAddress(config.getHostname(), config.getPort()));
} catch (IOException e) {
log.error("Could not initialize connection to elasticache cluster", e);
return null;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.oath.micro.server.memcached;



import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;


import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

@Slf4j
@Getter
@Configuration
public class ElasticacheConfig {
private final String hostname;
private final int port;
private final int retryAfterSecs;
private final int maxRetries;

@Autowired
public ElasticacheConfig(@Value("${elasticache.hostname:null}") String hostname,
@Value("${elasticache.port:6379}") int port,
@Value("${elasticache.retry.after.seconds:1}") int retryAfterSecs,
@Value("${elasticache.max.retries:3}") int maxRetries) {
this.hostname = hostname;
this.port = port;
this.retryAfterSecs = retryAfterSecs;
this.maxRetries = maxRetries;
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.oath.micro.server.elasticache;
package com.oath.micro.server.memcached;

import com.oath.micro.server.Plugin;
import cyclops.reactive.collections.mutable.SetX;
Expand All @@ -9,6 +9,6 @@ public class ElasticachePlugin implements Plugin {

@Override
public Set<Class> springClasses() {
return SetX.of(ConfigureElasticache.class);
return SetX.of(ElasticacheConfig.class, DistributedCacheFactory.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.oath.micro.server.elasticache;
package com.oath.micro.server.memcached;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -7,21 +7,21 @@


@Slf4j
public class TransientElasticacheDataConnection<V> implements DistributedCacheManager<V> {
public class MemcachedCacheImpl<K, V> implements DistributedCache<K, V> {

private volatile boolean available = false;
private final MemcachedClient memcachedClient;
private final int retryAfterSec;
private final int maxTry;

public TransientElasticacheDataConnection(MemcachedClient memcachedClient,int retryAfterSec, int maxTry) {
public MemcachedCacheImpl(MemcachedClient memcachedClient, int retryAfterSec, int maxTry) {
this.memcachedClient = memcachedClient;
this.retryAfterSec = retryAfterSec;
this.maxTry = maxTry;
}

@Override
public boolean add(final String key, int exp, final Object value) {
public boolean add(final K key, int exp, final V value) {

log.trace("Memcached add operation on key '{}', with value:{}", key, value);
boolean success = false;
Expand All @@ -31,13 +31,13 @@ public boolean add(final String key, int exp, final Object value) {
try {
if (tryCount > 0) {
Thread.sleep(retryAfterSec * 1000);
log.warn("retrying operation #{}", tryCount);
log.warn("Retrying operation #{}", tryCount);
}
tryCount++;
success = memcachedClient.add(key, exp, value)
success = memcachedClient.add(asString(key), exp, value)
.get();
} catch (final Exception e) {
log.warn("memcache set: {}", e.getMessage());
log.warn("Memcache set: {}", e.getMessage());
}
} while (!success && tryCount < maxTry);

Expand All @@ -53,8 +53,8 @@ public boolean add(final String key, int exp, final Object value) {
}

@Override
public Optional<V> get(String key) {
return (Optional<V>) Optional.ofNullable(memcachedClient.get(key));
public Optional<V> get(K key) {
return (Optional<V>) Optional.ofNullable(memcachedClient.get(asString(key)));
}

@Override
Expand All @@ -66,4 +66,8 @@ public boolean isAvailable() {
public final void setConnectionTested(final boolean available) {
this.available = available;
}
}

private String asString(K key){
return key.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.oath.micro.server.memcached.ElasticachePlugin
Loading