use of net.spy.memcached.MemcachedClient in project qi4j-sdk by Qi4j.
the class MemcachePoolMixin method activateService.
@Override
public void activateService() throws Exception {
if (configuration != null) {
MemcacheConfiguration config = configuration.get();
expiration = (config.expiration().get() == null) ? 3600 : config.expiration().get();
String addresses = (config.addresses().get() == null) ? "localhost:11211" : config.addresses().get();
Protocol protocol = (config.protocol().get() == null) ? Protocol.TEXT : Protocol.valueOf(config.protocol().get().toUpperCase());
String username = config.username().get();
String password = config.password().get();
String authMech = config.authMechanism().get() == null ? "PLAIN" : config.authMechanism().get();
ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
builder.setProtocol(protocol);
if (username != null && !username.isEmpty()) {
builder.setAuthDescriptor(new AuthDescriptor(new String[] { authMech }, new PlainCallbackHandler(username, password)));
}
client = new MemcachedClient(builder.build(), AddrUtil.getAddresses(addresses));
}
}
use of net.spy.memcached.MemcachedClient in project oxAuth by GluuFederation.
the class MemcacheManual method main.
public static void main(String[] args) throws IOException {
final MemcachedClient client = createClients();
final ExecutorService executorService = Executors.newFixedThreadPool(1000, daemonThreadFactory());
int count = 10000;
final long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
final int key = i;
executorService.execute(new Runnable() {
@Override
public void run() {
// MemcachedClient client = clients.get(random);
Object toPut = testGrant();
// Object toPut = UUID.randomUUID().toString();
OperationFuture<Boolean> set = null;
for (int j = 0; j < 3; j++) {
set = client.set(Integer.toString(key), 60, toPut);
}
// block
OperationStatus status = set.getStatus();
System.out.println(" key: " + key + ", time: " + (new Date().getTime() - start) + "ms, set: " + status);
executorService.execute(new Runnable() {
@Override
public void run() {
int random = random();
if (random % 3 == 0) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object get = client.get(Integer.toString(key));
System.out.println("GET key: " + key + " result: " + get);
}
});
}
});
}
sleep(40);
// System.out.println(client.get("myKey"));
//
// client.set("myKey", 30, testState());
//
// sleep(12);
// System.out.println(client.get("myKey"));
//
// sleep(12);
// System.out.println(client.get("myKey"));
client.shutdown();
}
use of net.spy.memcached.MemcachedClient in project geode by apache.
the class DomainObjectsAsValuesJUnitTest method testGetPutDomainObject.
@Test
public void testGetPutDomainObject() throws Exception {
MemcachedClient client = new MemcachedClient(new InetSocketAddress(InetAddress.getLocalHost(), PORT));
Customer c = new Customer("name0", "addr0");
Customer c1 = new Customer("name1", "addr1");
Future<Boolean> f = client.add("keyObj", 10, c);
assertTrue(f.get());
Future<Boolean> f1 = client.add("key1", 10, c1);
assertTrue(f1.get());
assertEquals(c, client.get("keyObj"));
assertEquals(c1, client.get("key1"));
assertNull(client.get("nonExistentkey"));
}
use of net.spy.memcached.MemcachedClient in project geode by apache.
the class GemcachedBinaryClientJUnitTest method testCacheWriterException.
@SuppressWarnings("unchecked")
public void testCacheWriterException() throws Exception {
MemcachedClient client = createMemcachedClient();
assertTrue(client.set("key", 0, "value".getBytes()).get());
client.set("exceptionkey", 0, "exceptionvalue").get();
GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
Region region = cache.getRegion(GemFireMemcachedServer.REGION_NAME);
region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter() {
@Override
public void beforeCreate(EntryEvent event) throws CacheWriterException {
if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
throw new RuntimeException("ExpectedStrings: Cache writer exception");
}
}
@Override
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
throw new RuntimeException("ExpectedStrings: Cache writer exception");
}
}
});
long start = System.nanoTime();
try {
client.set("exceptionkey", 0, "exceptionvalue").get();
throw new RuntimeException("expected exception not thrown");
} catch (ExecutionException e) {
// expected
}
assertTrue(client.set("key2", 0, "value2".getBytes()).get());
}
use of net.spy.memcached.MemcachedClient in project geode by apache.
the class GemcachedBinaryClientJUnitTest method createMemcachedClient.
@Override
protected MemcachedClient createMemcachedClient() throws IOException, UnknownHostException {
List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
addrs.add(new InetSocketAddress(InetAddress.getLocalHost(), PORT));
MemcachedClient client = new MemcachedClient(new BinaryConnectionFactory(), addrs);
return client;
}
Aggregations