RedissonAtomicLong, RedissonMap, RedissonList, RedissonQueue and RedissonSet now implements RExpirable.

pull/10/head
Nikita 11 years ago
parent a2f033329e
commit e28c45df5c

@ -28,14 +28,12 @@ import com.lambdaworks.redis.RedisConnection;
* @author Nikita Koksharov * @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(); private final AtomicBoolean initOnce = new AtomicBoolean();
RedissonAtomicLong(ConnectionManager connectionManager, String name) { RedissonAtomicLong(ConnectionManager connectionManager, String name) {
super(name); super(connectionManager, name);
this.connectionManager = connectionManager;
} }
public void init() { public void init() {

@ -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<Object, Object> connection = connectionManager.connectionWriteOp();
try {
return connection.expire(getName(), timeUnit.toSeconds(timeToLive));
} finally {
connectionManager.release(connection);
}
}
@Override
public boolean expireAt(long timestamp) {
RedisConnection<Object, Object> connection = connectionManager.connectionWriteOp();
try {
return connection.expireat(getName(), timestamp);
} finally {
connectionManager.release(connection);
}
}
@Override
public boolean expireAt(Date timestamp) {
RedisConnection<Object, Object> connection = connectionManager.connectionWriteOp();
try {
return connection.expireat(getName(), timestamp);
} finally {
connectionManager.release(connection);
}
}
@Override
public boolean clearExpire() {
RedisConnection<Object, Object> connection = connectionManager.connectionWriteOp();
try {
return connection.persist(getName());
} finally {
connectionManager.release(connection);
}
}
@Override
public long remainTimeToLive() {
RedisConnection<Object, Object> connection = connectionManager.connectionWriteOp();
try {
return connection.ttl(getName());
} finally {
connectionManager.release(connection);
}
}
}

@ -35,19 +35,12 @@ import com.lambdaworks.redis.RedisConnection;
* *
* @param <V> value * @param <V> value
*/ */
public class RedissonList<V> extends RedissonObject implements RList<V> { public class RedissonList<V> extends RedissonExpirable implements RList<V> {
private int batchSize = 50; private int batchSize = 50;
private final ConnectionManager connectionManager;
RedissonList(ConnectionManager connectionManager, String name) { RedissonList(ConnectionManager connectionManager, String name) {
super(name); super(connectionManager, name);
this.connectionManager = connectionManager;
}
protected ConnectionManager getConnectionManager() {
return connectionManager;
} }
@Override @Override

@ -35,13 +35,10 @@ import com.lambdaworks.redis.RedisConnection;
* @param <V> value * @param <V> value
*/ */
//TODO implement watching by keys instead of map name //TODO implement watching by keys instead of map name
public class RedissonMap<K, V> extends RedissonObject implements RMap<K, V> { public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
private final ConnectionManager connectionManager;
RedissonMap(ConnectionManager connectionManager, String name) { RedissonMap(ConnectionManager connectionManager, String name) {
super(name); super(connectionManager, name);
this.connectionManager = connectionManager;
} }
@Override @Override

@ -41,7 +41,7 @@ public class RedissonQueue<V> extends RedissonList<V> implements RQueue<V> {
} }
public V getFirst() { public V getFirst() {
RedisConnection<String, Object> connection = getConnectionManager().connectionReadOp(); RedisConnection<String, Object> connection = connectionManager.connectionReadOp();
try { try {
V value = (V) connection.lindex(getName(), 0); V value = (V) connection.lindex(getName(), 0);
if (value == null) { if (value == null) {
@ -49,12 +49,12 @@ public class RedissonQueue<V> extends RedissonList<V> implements RQueue<V> {
} }
return value; return value;
} finally { } finally {
getConnectionManager().release(connection); connectionManager.release(connection);
} }
} }
public V removeFirst() { public V removeFirst() {
RedisConnection<String, Object> connection = getConnectionManager().connectionWriteOp(); RedisConnection<String, Object> connection = connectionManager.connectionWriteOp();
try { try {
V value = (V) connection.lpop(getName()); V value = (V) connection.lpop(getName());
if (value == null) { if (value == null) {
@ -62,7 +62,7 @@ public class RedissonQueue<V> extends RedissonList<V> implements RQueue<V> {
} }
return value; return value;
} finally { } finally {
getConnectionManager().release(connection); connectionManager.release(connection);
} }
} }
@ -73,11 +73,11 @@ public class RedissonQueue<V> extends RedissonList<V> implements RQueue<V> {
@Override @Override
public V poll() { public V poll() {
RedisConnection<String, Object> connection = getConnectionManager().connectionWriteOp(); RedisConnection<String, Object> connection = connectionManager.connectionWriteOp();
try { try {
return (V) connection.lpop(getName()); return (V) connection.lpop(getName());
} finally { } finally {
getConnectionManager().release(connection); connectionManager.release(connection);
} }
} }

@ -31,13 +31,10 @@ import com.lambdaworks.redis.RedisConnection;
* *
* @param <V> value * @param <V> value
*/ */
public class RedissonSet<V> extends RedissonObject implements RSet<V> { public class RedissonSet<V> extends RedissonExpirable implements RSet<V> {
private final ConnectionManager connectionManager;
RedissonSet(ConnectionManager connectionManager, String name) { RedissonSet(ConnectionManager connectionManager, String name) {
super(name); super(connectionManager, name);
this.connectionManager = connectionManager;
} }
@Override @Override

@ -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; package org.redisson.connection;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;

@ -21,7 +21,7 @@ package org.redisson.core;
* @author Nikita Koksharov * @author Nikita Koksharov
* *
*/ */
public interface RAtomicLong extends RObject { public interface RAtomicLong extends RExpirable {
/** /**
* Atomically decrements by one the current value. * Atomically decrements by one the current value.

@ -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();
}

@ -24,6 +24,6 @@ import java.util.List;
* *
* @param <V> value * @param <V> value
*/ */
public interface RList<V> extends List<V>, RObject { public interface RList<V> extends List<V>, RExpirable {
} }

@ -26,6 +26,6 @@ import java.util.concurrent.ConcurrentMap;
* @param <K> key * @param <K> key
* @param <V> value * @param <V> value
*/ */
public interface RMap<K, V> extends ConcurrentMap<K, V>, RObject { public interface RMap<K, V> extends ConcurrentMap<K, V>, RExpirable {
} }

@ -24,6 +24,6 @@ import java.util.Queue;
* *
* @param <V> value * @param <V> value
*/ */
public interface RQueue<V> extends Queue<V>, RObject { public interface RQueue<V> extends Queue<V>, RExpirable {
} }

@ -24,6 +24,6 @@ import java.util.Set;
* *
* @param <V> value * @param <V> value
*/ */
public interface RSet<V> extends Set<V>, RObject { public interface RSet<V> extends Set<V>, RExpirable {
} }

Loading…
Cancel
Save