diff --git a/src/main/java/org/redisson/Redisson.java b/src/main/java/org/redisson/Redisson.java index b7bd38f33..df67ce97c 100644 --- a/src/main/java/org/redisson/Redisson.java +++ b/src/main/java/org/redisson/Redisson.java @@ -20,6 +20,7 @@ import java.util.concurrent.ConcurrentMap; import org.redisson.connection.ConnectionManager; import org.redisson.core.RAtomicLong; +import org.redisson.core.RBucket; import org.redisson.core.RCountDownLatch; import org.redisson.core.RDeque; import org.redisson.core.RHyperLogLog; @@ -57,7 +58,7 @@ public class Redisson { }; private final ConcurrentMap latchesMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT, listener); - private final ConcurrentMap topicsMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT, listener); + private final ConcurrentMap topicsMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); private final ConcurrentMap locksMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT, listener); private final ConcurrentMap atomicLongsMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); @@ -67,6 +68,7 @@ public class Redisson { private final ConcurrentMap sortedSetMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); private final ConcurrentMap listsMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); private final ConcurrentMap hyperLogLogMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); + private final ConcurrentMap bucketMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); private final ConcurrentMap mapsMap = new ReferenceMap(ReferenceType.STRONG, ReferenceType.SOFT); private final ConnectionManager connectionManager; @@ -101,6 +103,19 @@ public class Redisson { return new Redisson(config); } + public RBucket getBucket(String name) { + RedissonBucket bucket = bucketMap.get(name); + if (bucket == null) { + bucket = new RedissonBucket(connectionManager, name); + RedissonBucket oldBucket = bucketMap.putIfAbsent(name, bucket); + if (oldBucket != null) { + bucket = oldBucket; + } + } + + return bucket; + } + public RHyperLogLog getHyperLogLog(String name) { RedissonHyperLogLog logLog = hyperLogLogMap.get(name); if (logLog == null) { diff --git a/src/main/java/org/redisson/RedissonBucket.java b/src/main/java/org/redisson/RedissonBucket.java new file mode 100644 index 000000000..a4e35fd0e --- /dev/null +++ b/src/main/java/org/redisson/RedissonBucket.java @@ -0,0 +1,107 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson; + +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.FutureListener; +import io.netty.util.concurrent.Promise; + +import java.util.concurrent.TimeUnit; + +import org.redisson.connection.ConnectionManager; +import org.redisson.core.RBucket; + +import com.lambdaworks.redis.RedisConnection; + +public class RedissonBucket extends RedissonExpirable implements RBucket { + + RedissonBucket(ConnectionManager connectionManager, String name) { + super(connectionManager, name); + } + + @Override + public V get() { + RedisConnection conn = connectionManager.connectionReadOp(); + try { + return conn.get(getName()); + } finally { + connectionManager.release(conn); + } + } + + @Override + public Future getAsync() { + RedisConnection conn = connectionManager.connectionReadOp(); + return conn.getAsync().get(getName()).addListener(connectionManager.createReleaseListener(conn)); + } + + @Override + public void set(V value) { + RedisConnection conn = connectionManager.connectionWriteOp(); + try { + conn.set(getName(), value); + } finally { + connectionManager.release(conn); + } + } + + @Override + public Future setAsync(V value) { + RedisConnection connection = connectionManager.connectionWriteOp(); + Promise promise = connectionManager.getGroup().next().newPromise(); + Future f = connection.getAsync().set(getName(), value); + addListener(f, promise); + promise.addListener(connectionManager.createReleaseListener(connection)); + return promise; + } + + @Override + public void set(V value, long timeToLive, TimeUnit timeUnit) { + RedisConnection conn = connectionManager.connectionWriteOp(); + try { + conn.setex(getName(), timeUnit.toSeconds(timeToLive), value); + } finally { + connectionManager.release(conn); + } + } + + private void addListener(Future future, final Promise promise) { + future.addListener(new FutureListener() { + @Override + public void operationComplete(Future future) throws Exception { + if (promise.isCancelled()) { + return; + } + if (future.isSuccess()) { + promise.setSuccess(null); + } else { + promise.setFailure(promise.cause()); + } + } + }); + } + + @Override + public Future setAsync(V value, long timeToLive, TimeUnit timeUnit) { + RedisConnection connection = connectionManager.connectionWriteOp(); + Promise promise = connectionManager.getGroup().next().newPromise(); + Future f = connection.getAsync().setex(getName(), timeUnit.toSeconds(timeToLive), value); + addListener(f, promise); + promise.addListener(connectionManager.createReleaseListener(connection)); + return promise; + } + +} diff --git a/src/main/java/org/redisson/RedissonMap.java b/src/main/java/org/redisson/RedissonMap.java index a710965c5..1ab4f5abb 100644 --- a/src/main/java/org/redisson/RedissonMap.java +++ b/src/main/java/org/redisson/RedissonMap.java @@ -286,7 +286,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public Future putAsync(K key, V value) { - RedisConnection connection = connectionManager.connectionReadOp(); + RedisConnection connection = connectionManager.connectionWriteOp(); Promise promise = connectionManager.getGroup().next().newPromise(); RedisAsyncConnection async = connection.getAsync(); putAsync(key, value, promise, async); @@ -372,7 +372,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public Future removeAsync(K key) { - RedisConnection connection = connectionManager.connectionReadOp(); + RedisConnection connection = connectionManager.connectionWriteOp(); Promise promise = connectionManager.getGroup().next().newPromise(); RedisAsyncConnection async = connection.getAsync(); removeAsync(key, promise, async); diff --git a/src/main/java/org/redisson/core/RBucket.java b/src/main/java/org/redisson/core/RBucket.java new file mode 100644 index 000000000..22e4b5dd3 --- /dev/null +++ b/src/main/java/org/redisson/core/RBucket.java @@ -0,0 +1,36 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.core; + +import io.netty.util.concurrent.Future; + +import java.util.concurrent.TimeUnit; + +public interface RBucket extends RObject { + + V get(); + + Future getAsync(); + + void set(V value); + + Future setAsync(V value); + + void set(V value, long timeToLive, TimeUnit timeUnit); + + Future setAsync(V value, long timeToLive, TimeUnit timeUnit); + +}