From e28c45df5c6ef17ecfaaaaa45431a1574c3cc5c7 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 22 Apr 2014 20:53:31 +0400 Subject: [PATCH] RedissonAtomicLong, RedissonMap, RedissonList, RedissonQueue and RedissonSet now implements RExpirable. --- .../java/org/redisson/RedissonAtomicLong.java | 6 +- .../java/org/redisson/RedissonExpirable.java | 85 +++++++++++++++++++ src/main/java/org/redisson/RedissonList.java | 11 +-- src/main/java/org/redisson/RedissonMap.java | 7 +- src/main/java/org/redisson/RedissonQueue.java | 12 +-- src/main/java/org/redisson/RedissonSet.java | 7 +- .../connection/PubSubConnectionEntry.java | 15 ++++ .../java/org/redisson/core/RAtomicLong.java | 2 +- .../java/org/redisson/core/RExpirable.java | 33 +++++++ src/main/java/org/redisson/core/RList.java | 2 +- src/main/java/org/redisson/core/RMap.java | 2 +- src/main/java/org/redisson/core/RQueue.java | 2 +- src/main/java/org/redisson/core/RSet.java | 2 +- 13 files changed, 152 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/redisson/RedissonExpirable.java create mode 100644 src/main/java/org/redisson/core/RExpirable.java diff --git a/src/main/java/org/redisson/RedissonAtomicLong.java b/src/main/java/org/redisson/RedissonAtomicLong.java index 501033532..8c5e0812e 100644 --- a/src/main/java/org/redisson/RedissonAtomicLong.java +++ b/src/main/java/org/redisson/RedissonAtomicLong.java @@ -28,14 +28,12 @@ import com.lambdaworks.redis.RedisConnection; * @author Nikita Koksharov * */ -public class RedissonAtomicLong extends RedissonObject implements RAtomicLong { +public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong { - private final ConnectionManager connectionManager; private final AtomicBoolean initOnce = new AtomicBoolean(); RedissonAtomicLong(ConnectionManager connectionManager, String name) { - super(name); - this.connectionManager = connectionManager; + super(connectionManager, name); } public void init() { diff --git a/src/main/java/org/redisson/RedissonExpirable.java b/src/main/java/org/redisson/RedissonExpirable.java new file mode 100644 index 000000000..ed5fc61df --- /dev/null +++ b/src/main/java/org/redisson/RedissonExpirable.java @@ -0,0 +1,85 @@ +/** + * 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 java.util.Date; +import java.util.concurrent.TimeUnit; + +import org.redisson.connection.ConnectionManager; +import org.redisson.core.RExpirable; + +import com.lambdaworks.redis.RedisConnection; + +abstract class RedissonExpirable extends RedissonObject implements RExpirable { + + final ConnectionManager connectionManager; + + RedissonExpirable(ConnectionManager connectionManager, String name) { + super(name); + this.connectionManager = connectionManager; + } + + @Override + public boolean expire(long timeToLive, TimeUnit timeUnit) { + RedisConnection connection = connectionManager.connectionWriteOp(); + try { + return connection.expire(getName(), timeUnit.toSeconds(timeToLive)); + } finally { + connectionManager.release(connection); + } + } + + @Override + public boolean expireAt(long timestamp) { + RedisConnection connection = connectionManager.connectionWriteOp(); + try { + return connection.expireat(getName(), timestamp); + } finally { + connectionManager.release(connection); + } + } + + @Override + public boolean expireAt(Date timestamp) { + RedisConnection connection = connectionManager.connectionWriteOp(); + try { + return connection.expireat(getName(), timestamp); + } finally { + connectionManager.release(connection); + } + } + + @Override + public boolean clearExpire() { + RedisConnection connection = connectionManager.connectionWriteOp(); + try { + return connection.persist(getName()); + } finally { + connectionManager.release(connection); + } + } + + @Override + public long remainTimeToLive() { + RedisConnection connection = connectionManager.connectionWriteOp(); + try { + return connection.ttl(getName()); + } finally { + connectionManager.release(connection); + } + } + +} diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index d115b9b54..6df10bfdd 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -35,19 +35,12 @@ import com.lambdaworks.redis.RedisConnection; * * @param value */ -public class RedissonList extends RedissonObject implements RList { +public class RedissonList extends RedissonExpirable implements RList { private int batchSize = 50; - private final ConnectionManager connectionManager; - RedissonList(ConnectionManager connectionManager, String name) { - super(name); - this.connectionManager = connectionManager; - } - - protected ConnectionManager getConnectionManager() { - return connectionManager; + super(connectionManager, name); } @Override diff --git a/src/main/java/org/redisson/RedissonMap.java b/src/main/java/org/redisson/RedissonMap.java index 14ddc8866..719394dc4 100644 --- a/src/main/java/org/redisson/RedissonMap.java +++ b/src/main/java/org/redisson/RedissonMap.java @@ -35,13 +35,10 @@ import com.lambdaworks.redis.RedisConnection; * @param value */ //TODO implement watching by keys instead of map name -public class RedissonMap extends RedissonObject implements RMap { - - private final ConnectionManager connectionManager; +public class RedissonMap extends RedissonExpirable implements RMap { RedissonMap(ConnectionManager connectionManager, String name) { - super(name); - this.connectionManager = connectionManager; + super(connectionManager, name); } @Override diff --git a/src/main/java/org/redisson/RedissonQueue.java b/src/main/java/org/redisson/RedissonQueue.java index 918fa3786..ab9631923 100644 --- a/src/main/java/org/redisson/RedissonQueue.java +++ b/src/main/java/org/redisson/RedissonQueue.java @@ -41,7 +41,7 @@ public class RedissonQueue extends RedissonList implements RQueue { } public V getFirst() { - RedisConnection connection = getConnectionManager().connectionReadOp(); + RedisConnection connection = connectionManager.connectionReadOp(); try { V value = (V) connection.lindex(getName(), 0); if (value == null) { @@ -49,12 +49,12 @@ public class RedissonQueue extends RedissonList implements RQueue { } return value; } finally { - getConnectionManager().release(connection); + connectionManager.release(connection); } } public V removeFirst() { - RedisConnection connection = getConnectionManager().connectionWriteOp(); + RedisConnection connection = connectionManager.connectionWriteOp(); try { V value = (V) connection.lpop(getName()); if (value == null) { @@ -62,7 +62,7 @@ public class RedissonQueue extends RedissonList implements RQueue { } return value; } finally { - getConnectionManager().release(connection); + connectionManager.release(connection); } } @@ -73,11 +73,11 @@ public class RedissonQueue extends RedissonList implements RQueue { @Override public V poll() { - RedisConnection connection = getConnectionManager().connectionWriteOp(); + RedisConnection connection = connectionManager.connectionWriteOp(); try { return (V) connection.lpop(getName()); } finally { - getConnectionManager().release(connection); + connectionManager.release(connection); } } diff --git a/src/main/java/org/redisson/RedissonSet.java b/src/main/java/org/redisson/RedissonSet.java index d55613351..37e1e5828 100644 --- a/src/main/java/org/redisson/RedissonSet.java +++ b/src/main/java/org/redisson/RedissonSet.java @@ -31,13 +31,10 @@ import com.lambdaworks.redis.RedisConnection; * * @param value */ -public class RedissonSet extends RedissonObject implements RSet { - - private final ConnectionManager connectionManager; +public class RedissonSet extends RedissonExpirable implements RSet { RedissonSet(ConnectionManager connectionManager, String name) { - super(name); - this.connectionManager = connectionManager; + super(connectionManager, name); } @Override diff --git a/src/main/java/org/redisson/connection/PubSubConnectionEntry.java b/src/main/java/org/redisson/connection/PubSubConnectionEntry.java index 6f11be3d2..58b4833b5 100644 --- a/src/main/java/org/redisson/connection/PubSubConnectionEntry.java +++ b/src/main/java/org/redisson/connection/PubSubConnectionEntry.java @@ -1,3 +1,18 @@ +/** + * 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.connection; import java.util.concurrent.Semaphore; diff --git a/src/main/java/org/redisson/core/RAtomicLong.java b/src/main/java/org/redisson/core/RAtomicLong.java index d91a8d4bb..15d193fd4 100644 --- a/src/main/java/org/redisson/core/RAtomicLong.java +++ b/src/main/java/org/redisson/core/RAtomicLong.java @@ -21,7 +21,7 @@ package org.redisson.core; * @author Nikita Koksharov * */ -public interface RAtomicLong extends RObject { +public interface RAtomicLong extends RExpirable { /** * Atomically decrements by one the current value. diff --git a/src/main/java/org/redisson/core/RExpirable.java b/src/main/java/org/redisson/core/RExpirable.java new file mode 100644 index 000000000..93514b3c8 --- /dev/null +++ b/src/main/java/org/redisson/core/RExpirable.java @@ -0,0 +1,33 @@ +/** + * 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 java.util.Date; +import java.util.concurrent.TimeUnit; + +public interface RExpirable extends RObject { + + boolean expire(long timeToLive, TimeUnit timeUnit); + + boolean expireAt(long timestamp); + + boolean expireAt(Date timestamp); + + boolean clearExpire(); + + long remainTimeToLive(); + +} diff --git a/src/main/java/org/redisson/core/RList.java b/src/main/java/org/redisson/core/RList.java index edcb542a4..66c510569 100644 --- a/src/main/java/org/redisson/core/RList.java +++ b/src/main/java/org/redisson/core/RList.java @@ -24,6 +24,6 @@ import java.util.List; * * @param value */ -public interface RList extends List, RObject { +public interface RList extends List, RExpirable { } diff --git a/src/main/java/org/redisson/core/RMap.java b/src/main/java/org/redisson/core/RMap.java index 92a4f1c7e..7722f72e4 100644 --- a/src/main/java/org/redisson/core/RMap.java +++ b/src/main/java/org/redisson/core/RMap.java @@ -26,6 +26,6 @@ import java.util.concurrent.ConcurrentMap; * @param key * @param value */ -public interface RMap extends ConcurrentMap, RObject { +public interface RMap extends ConcurrentMap, RExpirable { } diff --git a/src/main/java/org/redisson/core/RQueue.java b/src/main/java/org/redisson/core/RQueue.java index f355eed5b..2d3bfe9f5 100644 --- a/src/main/java/org/redisson/core/RQueue.java +++ b/src/main/java/org/redisson/core/RQueue.java @@ -24,6 +24,6 @@ import java.util.Queue; * * @param value */ -public interface RQueue extends Queue, RObject { +public interface RQueue extends Queue, RExpirable { } diff --git a/src/main/java/org/redisson/core/RSet.java b/src/main/java/org/redisson/core/RSet.java index d15a55bca..0987c1657 100644 --- a/src/main/java/org/redisson/core/RSet.java +++ b/src/main/java/org/redisson/core/RSet.java @@ -24,6 +24,6 @@ import java.util.Set; * * @param value */ -public interface RSet extends Set, RObject { +public interface RSet extends Set, RExpirable { }