Refine search
/** * Constructs a new <code>JedisConnection</code> instance backed by a jedis pool. * * @param jedis * @param pool can be null, if no pool is used * @param dbIndex * @param clientName the client name, can be {@literal null}. * @since 1.8 */ protected JedisConnection(Jedis jedis, @Nullable Pool<Jedis> pool, int dbIndex, String clientName) { this.jedis = jedis; this.pool = pool; this.dbIndex = dbIndex; this.clientName = clientName; // select the db // if this fail, do manual clean-up before propagating the exception // as we're inside the constructor if (dbIndex != jedis.getDB()) { try { select(dbIndex); } catch (DataAccessException ex) { close(); throw ex; } } }
@Override public void restore(byte[] key, long ttlInMillis, byte[] serializedValue, boolean replace) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(serializedValue, "Serialized value must not be null!"); if (replace) { this.connection.execute("RESTORE", new byte[][] { key, JedisConverters.toBytes(ttlInMillis), serializedValue, JedisConverters.toBytes("REPLACE") }); return; } if (ttlInMillis > Integer.MAX_VALUE) { throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis."); } try { if (isPipelined()) { pipeline(connection .newStatusResult(connection.getRequiredPipeline().restore(key, (int) ttlInMillis, serializedValue))); return; } if (isQueueing()) { transaction(connection .newStatusResult(connection.getRequiredTransaction().restore(key, (int) ttlInMillis, serializedValue))); return; } connection.getJedis().restore(key, (int) ttlInMillis, serializedValue); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public List<Object> closePipeline() { if (pipeline != null) { try { return convertPipelineResults(); } finally { pipeline = null; pipelinedResults.clear(); } } return Collections.emptyList(); }
@Override public Long publish(byte[] channel, byte[] message) { try { if (isPipelined()) { pipeline(newJedisResult(getRequiredPipeline().publish(channel, message))); return null; } if (isQueueing()) { transaction(newJedisResult(getRequiredTransaction().publish(channel, message))); return null; } return jedis.publish(channel, message); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long ttl(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().ttl(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().ttl(key))); return null; } return connection.getJedis().ttl(key); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Set<byte[]> keys(byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().keys(pattern))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().keys(pattern))); return null; } return connection.getJedis().keys(pattern); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public List<Long> bitField(byte[] key, BitFieldSubCommands subCommands) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(subCommands, "Command must not be null!"); byte[][] args = ByteUtils.mergeArrays(key, JedisConverters.toBitfieldCommandArguments(subCommands)); return connection.execute("BITFIELD", args, Client::getIntegerMultiBulkReply, JedisClientUtils::getResponse); }
@Override public byte[] get(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().get(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().get(key))); return null; } return connection.getJedis().get(key); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean exists(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().exists(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().exists(key))); return null; } return connection.getJedis().exists(key); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Map<byte[], byte[]> hGetAll(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().hgetAll(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().hgetAll(key))); return null; } return connection.getJedis().hgetAll(key); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long zAdd(byte[] key, Set<Tuple> tuples) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(tuples, "Tuples must not be null!"); try { if (isPipelined()) { pipeline( connection.newJedisResult(connection.getRequiredPipeline().zadd(key, JedisConverters.toTupleMap(tuples)))); return null; } if (isQueueing()) { transaction(connection .newJedisResult(connection.getRequiredTransaction().zadd(key, JedisConverters.toTupleMap(tuples)))); return null; } return connection.getJedis().zadd(key, JedisConverters.toTupleMap(tuples)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean zAdd(byte[] key, double score, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().zadd(key, score, value), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().zadd(key, score, value), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().zadd(key, score, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public DataType type(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline( connection.newJedisResult(connection.getRequiredPipeline().type(key), JedisConverters.stringToDataType())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().type(key), JedisConverters.stringToDataType())); return null; } return JedisConverters.toDataType(connection.getJedis().type(key)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Boolean pExpire(byte[] key, long millis) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().pexpire(key, millis), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().pexpire(key, millis), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().pexpire(key, millis)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Boolean expireAt(byte[] key, long unixTime) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().expireAt(key, unixTime), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().expireAt(key, unixTime), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().expireAt(key, unixTime)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Long pTtl(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().pttl(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().pttl(key))); return null; } return connection.getJedis().pttl(key); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Boolean persist(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline( connection.newJedisResult(connection.getRequiredPipeline().persist(key), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().persist(key), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().persist(key)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public void rename(byte[] sourceKey, byte[] targetKey) { Assert.notNull(sourceKey, "Source key must not be null!"); Assert.notNull(targetKey, "Target key must not be null!"); try { if (isPipelined()) { pipeline(connection.newStatusResult(connection.getRequiredPipeline().rename(sourceKey, targetKey))); return; } if (isQueueing()) { transaction(connection.newStatusResult(connection.getRequiredTransaction().rename(sourceKey, targetKey))); return; } connection.getJedis().rename(sourceKey, targetKey); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public byte[] dump(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().dump(key))); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().dump(key))); return null; } return connection.getJedis().dump(key); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }