@Override public Status update(String table, String key, Map<String, ByteIterator> values) { return jedis.hmset(key, StringByteIterator.getStringMap(values)) .equals("OK") ? Status.OK : Status.ERROR; }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { if (jedis.hmset(key, StringByteIterator.getStringMap(values)) .equals("OK")) { jedis.zadd(INDEX_KEY, hash(key), key); return Status.OK; } return Status.ERROR; }
/** * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key * 关联 * * @param hash * @param time */ public void hmset(String key, Map<String, String> hash, int time) { call(jedis -> { jedis.hmset(key, hash); if (time > 0) { jedis.expire(key, time); } }); }
/** {@inheritDoc} **/ @Override public <T> void hmset(String key, String subkey, Map<String, T> map) { if (map == null || map.isEmpty()) return; JedisCommands jedis = this.getInstanceByKey(key); Map<String, String> values = new HashMap<>(); for (Map.Entry<String, T> entry : map.entrySet()) { values.put(entry.getKey(), serializeValue(entry.getValue())); } jedis.hmset(concat(key, subkey), values); }
/** {@inheritDoc} **/ @Override public <T> void hmset(String key, String subkey, Map<String, T> map) { if (map == null || map.isEmpty()) return; JedisCommands jedis = this.getInstanceByKey(key); Map<String, String> values = new HashMap<>(); for (Map.Entry<String, T> entry : map.entrySet()) { values.put(entry.getKey(), serializeValue(entry.getValue())); } jedis.hmset(concat(key, subkey), values); }
/** {@inheritDoc} **/ @Override public <T> void hoset(String key, String subkey, T object) { Objects.requireNonNull(object, "object"); JedisCommands jedis = this.getInstanceByKey(key); Map<String, String> map = serializeFields(object); jedis.hmset(concat(key, subkey), map); }
/** {@inheritDoc} **/ @Override public <T> void hoset(String key, String subkey, T object) { Objects.requireNonNull(object, "object"); JedisCommands jedis = this.getInstanceByKey(key); Map<String, String> map = serializeFields(object); jedis.hmset(concat(key, subkey), map); }
/** * 设置 key 指定的哈希集中指定字段的值。该命令将重写所有在哈希集中存在的字段。如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key * 关联 * * @param hash * @param time */ public void hmset(String key, Map<String, String> hash, int time) { call(jedis -> { jedis.hmset(key, hash); if (time > 0) { jedis.expire(key, time); } }); }
public void set(String id, JedisTask task) { String taskId = "task:" + task.getId(); Map<String, String> data = new HashMap<>(); data.put("id", task.getId()); data.put("startTimeMs", Long.toString(task.getStartTimeMs())); data.put("ownerId", task.getOwnerId()); retry(() -> redisClientDelegate.withCommandsClient(client -> { client.hmset(taskId, data); client.expire(taskId, TASK_TTL); client.sadd(RUNNING_TASK_KEY, id); }), format("Writing task %s", id)); }
/** * 设置hash缓存 * * @param datas * @return */ public boolean set(Map<String, String> datas) { if(datas == null || datas.isEmpty())return false; boolean result = false; try { result = getJedisCommands(groupName).hmset(key, datas).equals(RESP_OK); //设置超时时间 if(result)setExpireIfNot(expireTime); return result; } finally { getJedisProvider(groupName).release(); } }
@Override public void updateStatus(ExecutionType type, @Nonnull String id, @Nonnull ExecutionStatus status) { ImmutablePair<String, RedisClientDelegate> pair = fetchKey(id); RedisClientDelegate delegate = pair.getRight(); String key = pair.getLeft(); delegate.withCommandsClient(c -> { Map<String, String> data = new HashMap<>(); data.put("status", status.name()); if (status == ExecutionStatus.RUNNING) { data.put("canceled", "false"); data.put("startTime", String.valueOf(currentTimeMillis())); } else if (status.isComplete() && c.hget(key, "startTime") != null) { data.put("endTime", String.valueOf(currentTimeMillis())); } if (status == BUFFERED) { c.sadd(allBufferedExecutionsKey(type), id); } else { c.srem(allBufferedExecutionsKey(type), id); } c.hmset(key, data); }); }
@Override public void cancel(ExecutionType type, @Nonnull String id, String user, String reason) { ImmutablePair<String, RedisClientDelegate> pair = fetchKey(id); RedisClientDelegate delegate = pair.getRight(); delegate.withCommandsClient(c -> { Map<String, String> data = new HashMap<>(); data.put("canceled", "true"); if (StringUtils.isNotEmpty(user)) { data.put("canceledBy", user); } if (StringUtils.isNotEmpty(reason)) { data.put("cancellationReason", reason); } ExecutionStatus currentStatus = ExecutionStatus.valueOf(c.hget(pair.getLeft(), "status")); if (currentStatus == ExecutionStatus.NOT_STARTED) { data.put("status", ExecutionStatus.CANCELED.name()); } c.hmset(pair.getLeft(), data); c.srem(allBufferedExecutionsKey(type), id); }); }
@Override public void resume(ExecutionType type, @Nonnull String id, String user, boolean ignoreCurrentStatus) { ImmutablePair<String, RedisClientDelegate> pair = fetchKey(id); RedisClientDelegate delegate = pair.getRight(); delegate.withCommandsClient(c -> { ExecutionStatus currentStatus = ExecutionStatus.valueOf(c.hget(pair.getLeft(), "status")); if (!ignoreCurrentStatus && currentStatus != ExecutionStatus.PAUSED) { throw new UnresumablePipelineException(format( "Unable to resume pipeline that is not PAUSED (executionId: %s, currentStatus: %s)", id, currentStatus )); } try { PausedDetails pausedDetails = mapper.readValue(c.hget(pair.getLeft(), "paused"), PausedDetails.class); pausedDetails.setResumedBy(user); pausedDetails.setResumeTime(currentTimeMillis()); Map<String, String> data = new HashMap<>(); data.put("paused", mapper.writeValueAsString(pausedDetails)); data.put("status", ExecutionStatus.RUNNING.toString()); c.hmset(pair.getLeft(), data); c.srem(allBufferedExecutionsKey(type), id); } catch (IOException e) { throw new ExecutionSerializationException("Failed converting pausedDetails to json", e); } }); }
@Override public void pause(ExecutionType type, @Nonnull String id, String user) { ImmutablePair<String, RedisClientDelegate> pair = fetchKey(id); RedisClientDelegate delegate = pair.getRight(); delegate.withCommandsClient(c -> { ExecutionStatus currentStatus = ExecutionStatus.valueOf(c.hget(pair.getLeft(), "status")); if (currentStatus != ExecutionStatus.RUNNING) { throw new UnpausablePipelineException(format( "Unable to pause pipeline that is not RUNNING (executionId: %s, currentStatus: %s)", id, currentStatus )); } PausedDetails pausedDetails = new PausedDetails(); pausedDetails.setPausedBy(user); pausedDetails.setPauseTime(currentTimeMillis()); Map<String, String> data = new HashMap<>(); try { data.put("paused", mapper.writeValueAsString(pausedDetails)); } catch (JsonProcessingException e) { throw new ExecutionSerializationException("Failed converting pausedDetails to json", e); } data.put("status", ExecutionStatus.PAUSED.toString()); c.hmset(pair.getLeft(), data); c.srem(allBufferedExecutionsKey(type), id); }); }