RScript object added. #73
parent
c88ed841ab
commit
e60084c88c
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* 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 java.util.List;
|
||||||
|
|
||||||
|
import org.redisson.async.ResultOperation;
|
||||||
|
import org.redisson.connection.ConnectionManager;
|
||||||
|
import org.redisson.core.RScript;
|
||||||
|
|
||||||
|
import com.lambdaworks.redis.RedisAsyncConnection;
|
||||||
|
import com.lambdaworks.redis.ScriptOutputType;
|
||||||
|
|
||||||
|
public class RedissonScript implements RScript {
|
||||||
|
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
public RedissonScript(ConnectionManager connectionManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R> R eval(final String luaScript, final ReturnType returnType, final List<Object> keys, final Object... values) {
|
||||||
|
return (R) connectionManager.get(evalAsync(luaScript, returnType, keys, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R> Future<R> evalAsync(final String luaScript, final ReturnType returnType, final List<Object> keys, final Object... values) {
|
||||||
|
return connectionManager.writeAsync(new ResultOperation<R, Object>() {
|
||||||
|
@Override
|
||||||
|
protected Future<R> execute(RedisAsyncConnection<Object, Object> async) {
|
||||||
|
return async.eval(luaScript, ScriptOutputType.valueOf(returnType.toString()), keys, values);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* 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.List;
|
||||||
|
|
||||||
|
public interface RScript {
|
||||||
|
|
||||||
|
enum ReturnType {BOOLEAN, INTEGER, MULTI, STATUS, VALUE};
|
||||||
|
|
||||||
|
<R> Future<R> evalAsync(String luaScript, ReturnType returnType, List<Object> keys, Object... values);
|
||||||
|
|
||||||
|
<R> R eval(String luaScript, ReturnType returnType, List<Object> keys, Object... values);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package org.redisson;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.Future;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import org.hamcrest.MatcherAssert;
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.redisson.core.RScript;
|
||||||
|
|
||||||
|
public class RedissonScriptTest extends BaseTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEval() {
|
||||||
|
RScript script = redisson.getScript();
|
||||||
|
List<Object> res = script.eval("return {1,2,3.3333,'foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
|
||||||
|
MatcherAssert.assertThat(res, Matchers.<Object>contains(1L, 2L, 3L, "foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEvalAsync() {
|
||||||
|
RScript script = redisson.getScript();
|
||||||
|
Future<List<Object>> res = script.evalAsync("return {1,2,3.3333,'foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
|
||||||
|
MatcherAssert.assertThat(res.awaitUninterruptibly().getNow(), Matchers.<Object>contains(1L, 2L, 3L, "foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue