public String set(String key, String value) { String retVal = dynoClient.set(key, value); return retVal; }
@SneakyThrows private Integer tryReuseWorkerId() { val pidWorkerIds = jedis.lrange(PREFIX_PID, 0, -1); if (pidWorkerIds.size() == 0) { return null; } boolean locked = jedis.setnx(PREFIX_LOK, Os.PID_STRING) == 1; if (!locked) { return null; } @Cleanup val i = new Closeable() { @Override public void close() throws IOException { jedis.del(PREFIX_LOK); } }; val workerId = findUsableWorkerId(pidWorkerIds); if (workerId == null) { return null; } jedis.set(PREFIX_USE + workerId, Os.PID_STRING + "x" + Os.IP_STRING + "x" + Os.HOSTNAME); return Integer.parseInt(workerId); }
/** * @param key * @param value * @param time seconds */ public void set(String key, String value, int time) { call(jedis -> { jedis.set(key, value); if (time > 0) { jedis.expire(key, time); } }); }
jedisCommand.set(key, value); break;
public String set(String key, String value) { String retVal = dynoClient.set(key, value); return retVal; }
@Override public boolean tryAcquireLock(@Nonnull String notificationType, long lockTimeoutSeconds) { String key = "lock:" + notificationType; return redisClientDelegate.withCommandsClient(client -> { return "OK".equals(client.set(key, "\uD83D\uDD12", "NX", "EX", lockTimeoutSeconds)); }); } }
private Boolean acquireMessageLock(String messageKey, String identifier, Integer ackDeadlineSeconds) { String response = redisClientDelegate.withCommandsClient(c -> { return c.set(messageKey, identifier, SET_IF_NOT_EXIST, SET_EXPIRE_TIME_SECONDS, ackDeadlineSeconds); }); return SUCCESS.equals(response); }
private boolean setRedis(final String key, final long expire) { try { String result = redisTemplate.execute((RedisCallback<String>) connection -> { JedisCommands commands = (JedisCommands) connection.getNativeConnection(); String uuid = UUID.randomUUID().toString(); lockFlag.set(uuid); return commands.set(key, uuid, "NX", "PX", expire); }); return !StringUtils.isEmpty(result); } catch (Exception e) { log.error("set redisDistributeLock occured an exception", e); } return false; }
private boolean setRedis(final String key, final long expire) { try { String result = redisTemplate.execute((RedisCallback<String>) connection -> { JedisCommands commands = (JedisCommands) connection.getNativeConnection(); String uuid = UUID.randomUUID().toString(); lockFlag.set(uuid); return commands.set(key, uuid, "NX", "PX", expire); }); return !StringUtils.isEmpty(result); } catch (Exception e) { log.error("set redisDistributeLock occured an exception", e); } return false; }
/** * @param key * @param value * @param time seconds */ public void set(String key, String value, int time) { call(jedis -> { jedis.set(key, value); if (time > 0) { jedis.expire(key, time); } }); }
/** * 重写redisTemplate的set方法 * <p> * 命令 SET resource-name anystring NX EX max-lock-time 是一种在 Redis 中实现锁的简单方法。 * <p> * 客户端执行以上的命令: * <p> * 如果服务器返回 OK ,那么这个客户端获得锁。 * 如果服务器返回 NIL ,那么客户端获取锁失败,可以在稍后再重试。 * * @param key 锁的Key * @param value 锁里面的值 * @param seconds 过去时间(秒) * @return String */ private String set(final String key, final String value, final long seconds) { Assert.isTrue(!StringUtils.isEmpty(key), "key不能为空"); return redisTemplate.execute((RedisConnection connection) -> { Object nativeConnection = connection.getNativeConnection(); String result = null; if (nativeConnection instanceof JedisCommands) { result = ((JedisCommands) nativeConnection).set(key, value, NX, EX, seconds); } if (!StringUtils.isEmpty(lockKeyLog) && !StringUtils.isEmpty(result)) { log.debug("获取锁{}的时间:{}", lockKeyLog, System.currentTimeMillis()); } return result; }); }
private boolean putLockKey(long leaseMillSec) { RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection(); JedisCommands commands = (JedisCommands) redisConnection.getNativeConnection(); String res = commands.set(key, getCurrThreadId(), "NX", "PX", leaseMillSec); redisConnection.close(); return (res != null && "OK".equalsIgnoreCase(res)) || redisTemplate.opsForValue().get(key).equals(getCurrThreadId()); }
private boolean putLockKey(long leaseMillSec) { RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection(); String res = ((JedisCommands) redisConnection.getNativeConnection()).set(key, getCurrThreadId(), "NX", "PX", leaseMillSec); redisConnection.close(); return "OK".equalsIgnoreCase(res); }
/** {@inheritDoc} **/ @Override public void set(String key, String subkey, Object value, Long expire) { JedisCommands jedis = this.getInstanceByKey(key); jedis.set(concat(key, subkey), serializeValue(value)); if (expire != null) { expire(key, subkey, expire); } }
/** {@inheritDoc} **/ @Override protected void set(String key, String subkey, Object value, Long expire) { JedisCommands jedis = this.getInstanceByKey(key); jedis.set(concat(key, subkey), serializeValue(value)); if (expire != null) { expire(concat(key, subkey), expire); } }
/** * 检查给定 key 是否存在。 * @return */ public boolean set(String value, Date expireAt) { if (value == null) return false; try { boolean result = getJedisCommands(groupName).set(key, value).equals(RESP_OK); if(result){ result = setExpireAt(expireAt); //set可能是更新缓存,所以统一通知各节点清除本地缓存 Level1CacheSupport.getInstance().publishSyncEvent(key); } return result; } finally { getJedisProvider(groupName).release(); } }
/** * 设置缓存指定过期时间间隔 * @param value * @param seconds (过期秒数 ,小于等于0时 不设置) * @return */ public boolean set(String value, long seconds) { if (value == null) return false; try { boolean result = getJedisCommands(groupName).set(key, value).equals(RESP_OK); if(result && seconds > 0){ result = setExpire(seconds); //set可能是更新缓存,所以统一通知各节点清除本地缓存 Level1CacheSupport.getInstance().publishSyncEvent(key); } return result; } finally { getJedisProvider(groupName).release(); } }
c.set( format("correlation:%s", execution.getTrigger().getCorrelationId()), execution.getId()