RedissonConcurrentMap added

pull/6/head
Nikita 11 years ago
parent fc99fbf5a0
commit 5e7bd1bff9

@ -0,0 +1,80 @@
package org.redisson;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import com.lambdaworks.redis.RedisConnection;
public class RedissonConcurrentMap<K, V> extends RedissonMap<K, V> implements ConcurrentMap<K, V> {
private final Redisson redisson;
RedissonConcurrentMap(Redisson redisson, RedisConnection<Object, Object> connection, String name) {
super(connection, name);
this.redisson = redisson;
}
@Override
public V putIfAbsent(K key, V value) {
while (true) {
Boolean res = getConnection().hsetnx(getName(), key, value);
if (!res) {
V result = get(key);
if (result != null) {
return result;
}
} else {
return null;
}
}
}
@Override
public boolean remove(Object key, Object value) {
// TODO use murmur-hashing as lock key table
Lock lock = redisson.getLock(getName() + "__" + key);
lock.lock();
try {
if (containsKey(key) && get(key).equals(value)) {
remove(key);
return true;
}
return false;
} finally {
lock.unlock();
}
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
// TODO user murmur-hashing as lock key table
Lock lock = redisson.getLock(getName() + "__" + key);
lock.lock();
try {
if (containsKey(key) && get(key).equals(oldValue)) {
getConnection().hset(getName(), key, newValue);
return true;
} else {
return false;
}
} finally {
lock.unlock();
}
}
@Override
public V replace(K key, V value) {
// TODO use murmur-hashing as lock key table
Lock lock = redisson.getLock(getName() + "__" + key);
lock.lock();
try {
if (containsKey(key)) {
return put(key, value);
}
return null;
} finally {
lock.unlock();
}
}
}

@ -18,6 +18,14 @@ public class RedissonMap<K, V> implements Map<K, V> {
this.name = name;
}
protected String getName() {
return name;
}
protected RedisConnection<Object, Object> getConnection() {
return connection;
}
@Override
public int size() {
return connection.hlen(name).intValue();

Loading…
Cancel
Save