org.redisson.api.RScript Java Examples

The following examples show how to use org.redisson.api.RScript. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public long nextTimestamp() {
    long time = System.currentTimeMillis() << 12;
    try {
        return redisson.getScript(LongCodec.INSTANCE).eval(RScript.Mode.READ_WRITE,
                "local currentTime = redis.call('get', KEYS[1]);"
                        + "if currentTime == false then "
                        + "redis.call('set', KEYS[1], ARGV[1]); "
                        + "return ARGV[1]; "
                        + "end;"
                        + "local nextValue = math.max(tonumber(ARGV[1]), tonumber(currentTime) + 1); "
                        + "redis.call('set', KEYS[1], nextValue); "
                        + "return nextValue;",
                RScript.ReturnType.INTEGER, Arrays.<Object>asList("redisson-hibernate-timestamp"), time);
    } catch (Exception e) {
        if (fallback) {
            return super.nextTimestamp();
        }
        throw e;
    }
}
 
Example #2
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptExists() {
    RScript s = redisson.getScript();
    String r = s.scriptLoad("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);

    List<Boolean> r1 = s.scriptExists(r);
    Assert.assertEquals(1, r1.size());
    Assert.assertTrue(r1.get(0));

    s.scriptFlush();

    List<Boolean> r2 = s.scriptExists(r);
    Assert.assertEquals(1, r2.size());
    Assert.assertFalse(r2.get(0));
}
 
Example #3
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalSha() {
    RScript s = redisson.getScript();
    String res = s.scriptLoad("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res);

    redisson.getBucket("foo").set("bar");
    String r1 = s.evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    Assert.assertEquals("bar", r1);
}
 
Example #4
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public long nextTimestamp() {
    long time = System.currentTimeMillis() << 12;
    return redisson.getScript(LongCodec.INSTANCE).eval(RScript.Mode.READ_WRITE,
              "local currentTime = redis.call('get', KEYS[1]);"
            + "if currentTime == false then "
                + "redis.call('set', KEYS[1], ARGV[1]); "
                + "return ARGV[1]; "
            + "end;"
            + "local nextValue = math.max(tonumber(ARGV[1]), tonumber(currentTime) + 1); "
            + "redis.call('set', KEYS[1], nextValue); "
            + "return nextValue;",
            RScript.ReturnType.INTEGER, Arrays.<Object>asList("redisson-hibernate-timestamp"), time);
}
 
Example #5
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public long nextTimestamp() {
    long time = System.currentTimeMillis() << 12;
    return redisson.getScript(LongCodec.INSTANCE).eval(RScript.Mode.READ_WRITE,
              "local currentTime = redis.call('get', KEYS[1]);"
            + "if currentTime == false then "
                + "redis.call('set', KEYS[1], ARGV[1]); "
                + "return ARGV[1]; "
            + "end;"
            + "local nextValue = math.max(tonumber(ARGV[1]), tonumber(currentTime) + 1); "
            + "redis.call('set', KEYS[1], nextValue); "
            + "return nextValue;",
            RScript.ReturnType.INTEGER, Arrays.<Object>asList("redisson-hibernate-timestamp"), time);
}
 
Example #6
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public long nextTimestamp() {
    long time = System.currentTimeMillis() << 12;
    return redisson.getScript(LongCodec.INSTANCE).eval(RScript.Mode.READ_WRITE,
              "local currentTime = redis.call('get', KEYS[1]);"
            + "if currentTime == false then "
                + "redis.call('set', KEYS[1], ARGV[1]); "
                + "return ARGV[1]; "
            + "end;"
            + "local nextValue = math.max(tonumber(ARGV[1]), tonumber(currentTime) + 1); "
            + "redis.call('set', KEYS[1], nextValue); "
            + "return nextValue;",
            RScript.ReturnType.INTEGER, Arrays.<Object>asList("redisson-hibernate-timestamp"), time);
}
 
Example #7
Source File: RedissonScriptReactiveTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalSha() {
    RScriptReactive s = redisson.getScript();
    String res = sync(s.scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res);

    sync(redisson.getBucket("foo").set("bar"));
    String r1 = sync(s.<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
}
 
Example #8
Source File: RedissonScriptReactiveTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptLoad() {
    sync(redisson.getBucket("foo").set("bar"));
    String r = sync(redisson.getScript().scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = sync(redisson.getScript().<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
}
 
Example #9
Source File: RedissonScriptReactiveTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptFlush() {
    sync(redisson.getBucket("foo").set("bar"));
    String r = sync(redisson.getScript().scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = sync(redisson.getScript().<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
    sync(redisson.getScript().scriptFlush());

    try {
        sync(redisson.getScript().evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    } catch (Exception e) {
        Assert.assertEquals(RedisException.class, e.getClass());
    }
}
 
Example #10
Source File: RedissonBatchRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test(expected=RedisException.class)
public void testExceptionHandling() {
    RBatchRx batch = redisson.createBatch(batchOptions);
    batch.getMap("test").put("1", "2");
    batch.getScript().eval(Mode.READ_WRITE, "wrong_code", RScript.ReturnType.VALUE);
    sync(batch.execute());
}
 
Example #11
Source File: RedissonScriptRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalSha() {
    RScriptRx s = redisson.getScript();
    String res = sync(s.scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res);

    sync(redisson.getBucket("foo").set("bar"));
    String r1 = sync(s.<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
}
 
Example #12
Source File: RedissonScriptRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptLoad() {
    sync(redisson.getBucket("foo").set("bar"));
    String r = sync(redisson.getScript().scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = sync(redisson.getScript().<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
}
 
Example #13
Source File: RedissonScriptRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptFlush() {
    sync(redisson.getBucket("foo").set("bar"));
    String r = sync(redisson.getScript().scriptLoad("return redis.call('get', 'foo')"));
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = sync(redisson.getScript().<String>evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    Assert.assertEquals("bar", r1);
    sync(redisson.getScript().scriptFlush());

    try {
        sync(redisson.getScript().evalSha(RScript.Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()));
    } catch (Exception e) {
        Assert.assertEquals(RedisException.class, e.getClass());
    }
}
 
Example #14
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalshaAsync() {
    RScript s = redisson.getScript();
    String res = s.scriptLoad("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res);

    redisson.getBucket("foo").set("bar");
    String r = redisson.getScript().eval(Mode.READ_ONLY, "return redis.call('get', 'foo')", RScript.ReturnType.VALUE);
    Assert.assertEquals("bar", r);
    RFuture<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    Assert.assertEquals("bar", r1.awaitUninterruptibly().getNow());
}
 
Example #15
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #16
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptLoadAsync() {
    redisson.getBucket("foo").set("bar");
    RFuture<String> r = redisson.getScript().scriptLoadAsync("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r.awaitUninterruptibly().getNow());
    String r1 = redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    Assert.assertEquals("bar", r1);
}
 
Example #17
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptLoad() {
    redisson.getBucket("foo").set("bar");
    String r = redisson.getScript().scriptLoad("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    Assert.assertEquals("bar", r1);
}
 
Example #18
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptFlush() {
    redisson.getBucket("foo").set("bar");
    String r = redisson.getScript().scriptLoad("return redis.call('get', 'foo')");
    Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r);
    String r1 = redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    Assert.assertEquals("bar", r1);
    redisson.getScript().scriptFlush();

    try {
        redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList());
    } catch (Exception e) {
        Assert.assertEquals(RedisException.class, e.getClass());
    }
}
 
Example #19
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptEncoding() {
    RScript script = redisson.getScript();
    String value = "test";
    script.eval(RScript.Mode.READ_WRITE, "redis.call('set', KEYS[1], ARGV[1])", RScript.ReturnType.VALUE, Arrays.asList("foo"), value);

    String val = script.eval(RScript.Mode.READ_WRITE, "return redis.call('get', KEYS[1])", RScript.ReturnType.VALUE, Arrays.asList("foo"));
    Assert.assertEquals(value, val);
}
 
Example #20
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEval() {
    RScript script = redisson.getScript(StringCodec.INSTANCE);
    List<Object> res = script.eval(RScript.Mode.READ_ONLY, "return {'1','2','3.3333','foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
    assertThat(res).containsExactly("1", "2", "3.3333", "foo");
}
 
Example #21
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testMulti() throws InterruptedException, ExecutionException {
    RLexSortedSet idx2 = redisson.getLexSortedSet("ABCD17436");
    
    Long l = new Long("1506524856000");
    for (int i = 0; i < 100; i++) {
        String s = "DENY" + "\t" + "TESTREDISSON" + "\t"
                + Long.valueOf(l) + "\t" + "helloworld_hongqin";
        idx2.add(s);
        l = l + 1;
    }

    String max = "'[DENY" + "\t" + "TESTREDISSON" + "\t" + "1506524856099'";
    String min = "'[DENY" + "\t" + "TESTREDISSON" + "\t" + "1506524856000'";
     String luaScript1= "local d = {}; d[1] = redis.call('zrevrangebylex','ABCD17436'," +max+","+min+",'LIMIT',0,5); ";
     luaScript1=  luaScript1 + " d[2] = redis.call('zrevrangebylex','ABCD17436'," +max+","+min+",'LIMIT',0,15); ";
     luaScript1=  luaScript1 + " d[3] = redis.call('zrevrangebylex','ABCD17436'," +max+","+min+",'LIMIT',0,25); ";
     luaScript1 = luaScript1 + " return d;";
 
     List<List<Object>> objs = redisson.getScript(StringCodec.INSTANCE).eval(RScript.Mode.READ_ONLY,
            luaScript1,
            RScript.ReturnType.MULTI, Collections.emptyList());            
    
    assertThat(objs).hasSize(3);
    assertThat(objs.get(0)).hasSize(5);
    assertThat(objs.get(1)).hasSize(15);
    assertThat(objs.get(2)).hasSize(25);
}
 
Example #22
Source File: RedisTransactionManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public RedisTransactionManager(RScript script) {
    this.script = script;
    List<Boolean> hasScripts = this.script.scriptExists(lockSignature, unlockSignature);
    if (!hasScripts.get(0)) {
        this.script.scriptLoad(lockScript);
    }
    if (!hasScripts.get(1)) {
        this.script.scriptLoad(unlockScript);
    }
}
 
Example #23
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #24
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #25
Source File: RedissonScriptRxTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testEval() {
    RScriptRx script = redisson.getScript(StringCodec.INSTANCE);
    List<Object> res = sync(script.eval(RScript.Mode.READ_ONLY, "return {'1','2','3.3333','foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList()));
    assertThat(res).containsExactly("1", "2", "3.3333", "foo");
}
 
Example #26
Source File: RedissonScriptTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testEvalAsync() {
    RScript script = redisson.getScript(StringCodec.INSTANCE);
    RFuture<List<Object>> res = script.evalAsync(RScript.Mode.READ_ONLY, "return {'1','2','3.3333','foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
    assertThat(res.awaitUninterruptibly().getNow()).containsExactly("1", "2", "3.3333", "foo");
}
 
Example #27
Source File: RedissonScriptReactiveTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testEval() {
    RScriptReactive script = redisson.getScript(StringCodec.INSTANCE);
    List<Object> res = sync(script.eval(RScript.Mode.READ_ONLY, "return {'1','2','3.3333','foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList()));
    assertThat(res).containsExactly("1", "2", "3.3333", "foo");
}
 
Example #28
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RScript getScript(Codec codec) {
  return redissonClient.getScript(codec);
}
 
Example #29
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RScript getScript() {
  return redissonClient.getScript();
}
 
Example #30
Source File: ScriptExamples.java    From redisson-examples with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RBucket<String> bucket = redisson.getBucket("foo");
    bucket.set("bar");

    RScript script = redisson.getScript(StringCodec.INSTANCE);

    // execute script in read only mode
    String result = script.eval(RScript.Mode.READ_ONLY,
                           "return redis.call('get', 'foo')", 
                           RScript.ReturnType.VALUE);


    
    // execute the same script stored in Redis lua script cache

    // load lua script into Redis cache to all redis master instances
    String sha1 = script.scriptLoad("return redis.call('get', 'foo')");

    // call lua script by sha digest
    result = redisson.getScript().evalSha(RScript.Mode.READ_ONLY,
                                sha1, RScript.ReturnType.VALUE, Collections.emptyList());
    
    
    redisson.shutdown();
}