@Override public Boolean setEx(byte[] key, long seconds, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); if (seconds > Integer.MAX_VALUE) { throw new IllegalArgumentException("Time must be less than Integer.MAX_VALUE for setEx in Jedis."); } try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } return Converters.stringToBoolean(connection.getJedis().setex(key, (int) seconds, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
private void setex0(String key, int seconds, String value) { t.setex(key, seconds, value); }
@Override public void set(String key, String sessionsKey, byte[] session, int timeout) throws UnsupportedEncodingException { try(Jedis jedis = this.jedisPool.getResource()) { Transaction t = jedis.multi(); t.setex(key.getBytes(Protocol.CHARSET), timeout, session); t.sadd(sessionsKey, key); t.exec(); } }
@Override public void commit() { if (connection != null) { if (!msetTarget.isEmpty()) { Transaction multi = connection.multi(); for (Map.Entry<String, String> entry : msetTarget.entrySet()) { multi.setex(entry.getKey(), DEFAULT_TTL, entry.getValue()); } multi.exec(); } msetTarget.clear(); // `connection` object is created for every time when ParamServerClientConnection // is injected, and is not a singleton object. // So we must close connection manually. connection.close(); this.connection = null; } }
@Test public void set() throws UnsupportedEncodingException { byte[] session = "session".getBytes(); this.jedisNodeClient.set("key", SESSIONS_KEY, session, timeout); verify(this.transaction, times(1)).setex("key".getBytes(Protocol.CHARSET), timeout, session); verify(this.transaction, times(1)).sadd(SESSIONS_KEY,"key"); verify(this.transaction, times(1)).exec(); verify(this.jedis, times(1)).close(); }
@Override public void push(T document) { long timestampMs = System.currentTimeMillis(); String serialized; String stateSerialized; try { serialized = mapper.serialize(new TimedWrap<>(document, timestampMs)); stateSerialized = stateMapper.serialize(new StateInfo(NEW, timestampMs, "")); } catch (SerializationException e) { serializationErrors.mark(); throw new RedisqException("Could not serialize element " + document.getIdAsString(), e); } LOG.debug("Jedis active: {}, idle: {}", jedisPool.getNumActive(), jedisPool.getNumIdle()); try (Jedis jedis = jedisPool.getResource(); Timer.Context ignored = pushTimer.time();) { Transaction transaction = jedis.multi(); String id = document.getIdAsString(); String lockId = names.lockKeyFromId(id); transaction.setex(lockId, lockTime, "locked"); transaction.lpush(queueName, id); transaction.setex(names.contentKeyFromId(id), ttlStateInfo, serialized); transaction.setex(names.stateKeyFromId(id), ttlStateInfo, stateSerialized); transaction.publish(names.stateChannelKeyFromId(id), stateSerialized); transaction.exec(); LOG.debug("Pushed {} with lockTime {}s lock id: {}", id, lockTime, lockId); } }
@Override public void put(Object key, Object value) { redisContext.run(jedis -> { byte[] keyBytes = toKeyBytes(key); Transaction multi = jedis.multi(); long currentSeconds = System.currentTimeMillis() / 1000; if (expireTime > 0) { multi.setex(keyBytes, expireTime, serialize(value)); } else { multi.set(keyBytes, serialize(value)); } multi.zadd(keysetBytes, currentSeconds, keyBytes); multi.exec(); }); }
@Override public Boolean setEx(byte[] key, long seconds, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); if (seconds > Integer.MAX_VALUE) { throw new IllegalArgumentException("Time must be less than Integer.MAX_VALUE for setEx in Jedis."); } try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } return Converters.stringToBoolean(connection.getJedis().setex(key, (int) seconds, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean setEx(byte[] key, long seconds, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); if (seconds > Integer.MAX_VALUE) { throw new IllegalArgumentException("Time must be less than Integer.MAX_VALUE for setEx in Jedis."); } try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().setex(key, (int) seconds, value), Converters.stringToBooleanConverter(), () -> false)); return null; } return Converters.stringToBoolean(connection.getJedis().setex(key, (int) seconds, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }