org.springframework.data.redis.connection.ReturnType Java Examples
The following examples show how to use
org.springframework.data.redis.connection.ReturnType.
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: RedisDistributedLock.java From microservices-platform with Apache License 2.0 | 6 votes |
/** * 释放锁 * @param key 锁的key * @return 成功/失败 */ public boolean releaseLock(String key) { // 释放锁的时候,有可能因为持锁之后方法执行时间大于锁的有效期,此时有可能已经被另外一个线程持有锁,所以不能直接删除 try { // 使用lua脚本删除redis中匹配value的key,可以避免由于方法执行时间过长而redis锁自动过期失效的时候误删其他线程的锁 // spring自带的执行脚本方法中,集群模式直接抛出不支持执行脚本的异常,所以只能拿到原redis的connection来执行脚本 Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> { byte[] scriptByte = redisTemplate.getStringSerializer().serialize(UNLOCK_LUA); return connection.eval(scriptByte, ReturnType.BOOLEAN, 1 , redisTemplate.getStringSerializer().serialize(key) , redisTemplate.getStringSerializer().serialize(lockFlag.get())); }); return result; } catch (Exception e) { log.error("release redisDistributeLock occured an exception", e); } finally { lockFlag.remove(); } return false; }
Example #2
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #3
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #4
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVAL"); List<Object> params = new ArrayList<Object>(); params.add(script); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, StringCodec.INSTANCE, c, params.toArray()); }
Example #5
Source File: RedissonReactiveScriptingCommands.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #6
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #7
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #8
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVAL"); List<Object> params = new ArrayList<Object>(); params.add(script); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, StringCodec.INSTANCE, c, params.toArray()); }
Example #9
Source File: RedissonReactiveScriptingCommands.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #10
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #11
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #12
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 6 votes |
@Override public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { if (isQueueing()) { throw new UnsupportedOperationException(); } if (isPipelined()) { throw new UnsupportedOperationException(); } RedisCommand<?> c = toCommand(returnType, "EVAL"); List<Object> params = new ArrayList<Object>(); params.add(script); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, StringCodec.INSTANCE, c, params.toArray()); }
Example #13
Source File: RedissonReactiveScriptingCommands.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #14
Source File: RedissonReactiveScriptingCommands.java From redisson with Apache License 2.0 | 6 votes |
protected RedisCommand<?> toCommand(ReturnType returnType, String name) { RedisCommand<?> c = null; if (returnType == ReturnType.BOOLEAN) { c = org.redisson.api.RScript.ReturnType.BOOLEAN.getCommand(); } else if (returnType == ReturnType.INTEGER) { c = org.redisson.api.RScript.ReturnType.INTEGER.getCommand(); } else if (returnType == ReturnType.MULTI) { c = org.redisson.api.RScript.ReturnType.MULTI.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } else if (returnType == ReturnType.STATUS) { c = org.redisson.api.RScript.ReturnType.STATUS.getCommand(); } else if (returnType == ReturnType.VALUE) { c = org.redisson.api.RScript.ReturnType.VALUE.getCommand(); return new RedisCommand(c, name, new BinaryConvertor()); } return new RedisCommand(c, name); }
Example #15
Source File: RedisUtils.java From rqueue with Apache License 2.0 | 6 votes |
public static int updateAndGetVersion( RedisConnectionFactory redisConnectionFactory, String versionDbKey, int defaultVersion) { RedisConnection connection = RedisConnectionUtils.getConnection(redisConnectionFactory); byte[] versionKey = versionDbKey.getBytes(); byte[] versionFromDb = connection.get(versionKey); if (SerializationUtils.isEmpty(versionFromDb)) { Long count = connection.eval( "return #redis.pcall('keys', 'rqueue-*')".getBytes(), ReturnType.INTEGER, 0); if (count != null && count > 0L) { int version = 1; connection.set(versionKey, String.valueOf(version).getBytes()); return version; } connection.set(versionKey, String.valueOf(defaultVersion).getBytes()); return defaultVersion; } return Integer.parseInt(new String(versionFromDb)); }
Example #16
Source File: LockUtil.java From jframework with Apache License 2.0 | 6 votes |
/** * 加锁 * * @param lockKey key * @param requestId 随机请求id * @param expireSecond 超时秒 * @param loopTimes 循环次数 * @param sleepInterval 等待间隔(毫秒) * @return */ public static boolean tryGetDistributedLock(String lockKey, String requestId, Integer expireSecond, Long loopTimes, Long sleepInterval) { DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(LOCK_SCRIPT_STR, Long.class); while (loopTimes-- >= 0) { Object result = redisTemplate.execute( (RedisConnection connection) -> connection.eval( redisScript.getScriptAsString().getBytes(), ReturnType.INTEGER, 1, lockKey.getBytes(), requestId.getBytes(), String.valueOf(expireSecond).getBytes() ) ); if (SUCCESS.equals(result)) { return true; } try { TimeUnit.MILLISECONDS.sleep(sleepInterval); } catch (InterruptedException e) { log.error("e message: {}", e.getMessage()); Thread.currentThread().interrupt(); } } return false; }
Example #17
Source File: RedisDistributedLock.java From SpringBoot2.0 with Apache License 2.0 | 6 votes |
@Override public boolean releaseLock(String key) { // 释放锁的时候,有可能因为持锁之后方法执行时间大于锁的有效期,此时有可能已经被另外一个线程持有锁,所以不能直接删除 try { // 使用lua脚本删除redis中匹配value的key,可以避免由于方法执行时间过长而redis锁自动过期失效的时候误删其他线程的锁 // spring自带的执行脚本方法中,集群模式直接抛出不支持执行脚本的异常,所以只能拿到原redis的connection来执行脚本 Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> { byte[] scriptByte = redisTemplate.getStringSerializer().serialize(UNLOCK_LUA); return connection.eval(scriptByte, ReturnType.BOOLEAN, 1 , redisTemplate.getStringSerializer().serialize(key) , redisTemplate.getStringSerializer().serialize(lockFlag.get())); }); return result; } catch (Exception e) { log.error("release redisDistributeLock occured an exception", e); } finally { lockFlag.remove(); } return false; }
Example #18
Source File: RedisService.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * unRedisLock(释放分布式锁) * * @param key 锁 * @param requestId 请求唯一标识,解锁需要带入 * @return 是否释放成功 */ @RequestMapping("unRedisLock") public int unRedisLock(String key, String requestId) { int result = -1; if (StringUtil.isBlank(key)) { return result; } if (StringUtil.isBlank(requestId)) { return result; } RedisConnection conn = getRedisConnection(); if (conn == null) { return result; } // 释放锁的时候,有可能因为持锁之后方法执行时间大于锁的有效期,此时有可能已经被另外一个线程持有锁,所以不能直接删除 try { boolean ret = conn.eval(UNLOCK_LUA.getBytes(), ReturnType.BOOLEAN, 1, key.getBytes(Charset.forName("UTF-8")), requestId.getBytes(Charset.forName("UTF-8"))); if (ret) { result = 1; } } catch (Exception e) { logger.error("unRedisLock(释放分布式锁)异常={}", StringUtil.getExceptionMsg(e)); } return result; }
Example #19
Source File: TracingRedisConnectionTest.java From java-redis-client with Apache License 2.0 | 5 votes |
@Test public void invokingEvalShaCreatesNewSpan() { commandCreatesNewSpan(RedisCommand.EVALSHA, () -> getConnection() .evalSha("scriptSha", ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .evalSha("scriptSha", ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes()); commandCreatesNewSpan(RedisCommand.EVALSHA, () -> getConnection().evalSha("scriptSha".getBytes(), ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .evalSha("scriptSha".getBytes(), ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes()); }
Example #20
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #21
Source File: LockUtil.java From jframework with Apache License 2.0 | 5 votes |
/** * 释放锁 * * @param lockKey key * @param requestId 加锁的请求id * @return */ public static boolean releaseDistributedLock(String lockKey, String requestId) { DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(UNLOCK_SCRIPT_STR, Long.class); Object result = redisTemplate.execute( (RedisConnection connection) -> connection.eval( redisScript.getScriptAsString().getBytes(), ReturnType.INTEGER, 1, lockKey.getBytes(), requestId.getBytes() ) ); return SUCCESS.equals(result); }
Example #22
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #23
Source File: RedissonConnection.java From redisson with Apache License 2.0 | 5 votes |
@Override public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { RedisCommand<?> c = toCommand(returnType, "EVALSHA"); List<Object> params = new ArrayList<Object>(); params.add(scriptSha); params.add(numKeys); params.addAll(Arrays.asList(keysAndArgs)); return write(null, ByteArrayCodec.INSTANCE, c, params.toArray()); }
Example #24
Source File: TracingRedisConnectionTest.java From java-redis-client with Apache License 2.0 | 5 votes |
@Test public void invokingEvalCreatesNewSpan() { commandCreatesNewSpan(RedisCommand.EVAL, () -> getConnection().eval("script".getBytes(), ReturnType.MULTI, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .eval("script".getBytes(), ReturnType.MULTI, 1, "keysAndArgs".getBytes()); }
Example #25
Source File: TracingRedisConnectionTest.java From java-redis-client with Apache License 2.0 | 5 votes |
@Test public void invokingEvalShaCreatesNewSpan() { commandCreatesNewSpan(RedisCommand.EVALSHA, () -> getConnection() .evalSha("scriptSha", ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .evalSha("scriptSha", ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes()); commandCreatesNewSpan(RedisCommand.EVALSHA, () -> getConnection().evalSha("scriptSha".getBytes(), ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .evalSha("scriptSha".getBytes(), ReturnType.BOOLEAN, 1, "keysAndArgs".getBytes()); }
Example #26
Source File: TracingRedisConnectionTest.java From java-redis-client with Apache License 2.0 | 5 votes |
@Test public void invokingEvalCreatesNewSpan() { commandCreatesNewSpan(RedisCommand.EVAL, () -> getConnection().eval("script".getBytes(), ReturnType.MULTI, 1, "keysAndArgs".getBytes())); verify(mockRedisConnection()) .eval("script".getBytes(), ReturnType.MULTI, 1, "keysAndArgs".getBytes()); }
Example #27
Source File: TracingRedisConnection.java From java-redis-client with Apache License 2.0 | 4 votes |
@Override public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { return helper.doInScope(RedisCommand.EVALSHA, () -> connection.evalSha(scriptSha, returnType, numKeys, keysAndArgs)); }
Example #28
Source File: TracingRedisConnection.java From java-redis-client with Apache License 2.0 | 4 votes |
@Override public <T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { return helper.doInScope(RedisCommand.EVALSHA, () -> connection.evalSha(scriptSha, returnType, numKeys, keysAndArgs)); }
Example #29
Source File: TracingRedisConnection.java From java-redis-client with Apache License 2.0 | 4 votes |
@Override public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { return helper.doInScope(RedisCommand.EVAL, () -> connection.eval(script, returnType, numKeys, keysAndArgs)); }
Example #30
Source File: TracingRedisConnection.java From java-redis-client with Apache License 2.0 | 4 votes |
@Override public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { return helper.doInScope(RedisCommand.EVAL, () -> connection.eval(script, returnType, numKeys, keysAndArgs)); }