private RedisConnection getConnection() { return connectionFactory.getConnection(); }
/** * Executed wrapped command upon {@link RedisClusterConnection}. * * @param callback must not be {@literal null}. * @return execution result. Can be {@literal null}. */ @Nullable public <T> T execute(RedisClusterCallback<T> callback) { Assert.notNull(callback, "ClusterCallback must not be null!"); RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection(); try { return callback.doInRedis(connection); } finally { connection.close(); } } }
/** * Execute a transaction, using the default {@link RedisSerializer}s to deserialize any results that are byte[]s or * Collections or Maps of byte[]s or Tuples. Other result types (Long, Boolean, etc) are left as-is in the converted * results. If conversion of tx results has been disabled in the {@link RedisConnectionFactory}, the results of exec * will be returned without deserialization. This check is mostly for backwards compatibility with 1.0. * * @return The (possibly deserialized) results of transaction exec */ @Override public List<Object> exec() { List<Object> results = execRaw(); if (getRequiredConnectionFactory().getConvertPipelineAndTxResults()) { return deserializeMixedResults(results, valueSerializer, hashKeySerializer, hashValueSerializer); } else { return results; } }
/** * Clear database before shut down. */ public @PreDestroy void flushTestDb() { factory.getConnection().flushDb(); }
@Override public RedisClusterConnection getClusterConnection() { System.out.println("getClusterConnection"); return redisConnectionFactory.getClusterConnection(); }
@Override public boolean getConvertPipelineAndTxResults() { return redisConnectionFactory.getConvertPipelineAndTxResults(); }
private void executeLockFree(Consumer<RedisConnection> callback) { RedisConnection connection = connectionFactory.getConnection(); try { callback.accept(connection); } finally { connection.close(); } }
/** * Executed wrapped command upon {@link RedisClusterConnection}. * * @param callback must not be {@literal null}. * @return execution result. Can be {@literal null}. */ @Nullable public <T> T execute(RedisClusterCallback<T> callback) { Assert.notNull(callback, "ClusterCallback must not be null!"); RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection(); try { return callback.doInRedis(connection); } finally { connection.close(); } } }
/** * Execute a transaction, using the default {@link RedisSerializer}s to deserialize any results that are byte[]s or * Collections or Maps of byte[]s or Tuples. Other result types (Long, Boolean, etc) are left as-is in the converted * results. If conversion of tx results has been disabled in the {@link RedisConnectionFactory}, the results of exec * will be returned without deserialization. This check is mostly for backwards compatibility with 1.0. * * @return The (possibly deserialized) results of transaction exec */ @Override public List<Object> exec() { List<Object> results = execRaw(); if (getRequiredConnectionFactory().getConvertPipelineAndTxResults()) { return deserializeMixedResults(results, valueSerializer, hashKeySerializer, hashValueSerializer); } else { return results; } }
private <T> T execute(String name, Function<RedisConnection, T> callback) { RedisConnection connection = connectionFactory.getConnection(); try { checkAndPotentiallyWaitUntilUnlocked(name, connection); return callback.apply(connection); } finally { connection.close(); } }
@PostMapping(value = "/setRedisCluster") public Problem setRedisCluster() { redisConnectionFactory.getClusterConnection().stringCommands() .set("key".getBytes(), "value".getBytes()); stringRedisTemplate.opsForValue().set("stringKey", "stringValue"); return Problem.builder().with("data", "success").build(); }
/** * Execute a transaction, using the default {@link RedisSerializer}s to deserialize any results that are byte[]s or * Collections or Maps of byte[]s or Tuples. Other result types (Long, Boolean, etc) are left as-is in the converted * results. If conversion of tx results has been disabled in the {@link RedisConnectionFactory}, the results of exec * will be returned without deserialization. This check is mostly for backwards compatibility with 1.0. * * @return The (possibly deserialized) results of transaction exec */ @Override public List<Object> exec() { List<Object> results = execRaw(); if (getRequiredConnectionFactory().getConvertPipelineAndTxResults()) { return deserializeMixedResults(results, valueSerializer, hashKeySerializer, hashValueSerializer); } else { return results; } }
@Override public void afterPropertiesSet() throws Exception { if (this.configure == ConfigureRedisAction.NO_OP) { return; } RedisConnection connection = this.connectionFactory.getConnection(); try { this.configure.configure(connection); } finally { try { connection.close(); } catch (Exception ex) { LogFactory.getLog(getClass()).error("Error closing RedisConnection", ex); } } }
/** * Executed wrapped command upon {@link RedisClusterConnection}. * * @param callback must not be {@literal null}. * @return execution result. Can be {@literal null}. */ @Nullable public <T> T execute(RedisClusterCallback<T> callback) { Assert.notNull(callback, "ClusterCallback must not be null!"); RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection(); try { return callback.doInRedis(connection); } finally { connection.close(); } } }
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { RedisCommand commandToExecute = RedisCommand.failsafeCommandLookup(method.getName()); if (isPotentiallyThreadBoundCommand(commandToExecute)) { if (log.isDebugEnabled()) { log.debug(String.format("Invoke '%s' on bound conneciton", method.getName())); } return invoke(method, obj, args); } if (log.isDebugEnabled()) { log.debug(String.format("Invoke '%s' on unbound conneciton", method.getName())); } RedisConnection connection = factory.getConnection(); try { return invoke(method, connection, args); } finally { // properly close the unbound connection after executing command if (!connection.isClosed()) { connection.close(); } } }
@GetMapping(value = "/getRedisCluster") public Problem getRedisCluster() { byte[] bytes = redisConnectionFactory.getClusterConnection().stringCommands() .get("key".getBytes()); if (bytes != null) { String value = new String(bytes, StandardCharsets.UTF_8); String stringValue = stringRedisTemplate.opsForValue().get("stringKey"); return Problem.builder().with("data", value + "_" + stringValue).build(); } else { return Problem.valueOf(Status.BAD_REQUEST); } }
/** * Initialize the message listener by writing requried redis config for {@literal notify-keyspace-events} and * registering the listener within the container. */ public void init() { if (StringUtils.hasText(keyspaceNotificationsConfigParameter)) { RedisConnection connection = listenerContainer.getConnectionFactory().getConnection(); try { Properties config = connection.getConfig("notify-keyspace-events"); if (!StringUtils.hasText(config.getProperty("notify-keyspace-events"))) { connection.setConfig("notify-keyspace-events", keyspaceNotificationsConfigParameter); } } finally { connection.close(); } } doRegister(listenerContainer); }
@Override public Map<Long, CIMServerResVO> loadRouteRelated() { Map<Long, CIMServerResVO> routes = new HashMap<>(64); RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); ScanOptions options = ScanOptions.scanOptions() .match(ROUTE_PREFIX + "*") .build(); Cursor<byte[]> scan = connection.scan(options); while (scan.hasNext()) { byte[] next = scan.next(); String key = new String(next, StandardCharsets.UTF_8); LOGGER.info("key={}", key); parseServerInfo(routes, key); } try { scan.close(); } catch (IOException e) { LOGGER.error("IOException",e); } return routes; }
connection = connectionFactory.getConnection(); if (connection.isSubscribed()) { throw new IllegalStateException("Retrieved connection is already subscribed; aborting listening");
@Bean public RedisConnectionFactory connectionFactory() { RedisConnectionFactory factory = mock(RedisConnectionFactory.class); RedisConnection connection = mock(RedisConnection.class); given(factory.getConnection()).willReturn(connection); given(connection.getConfig(anyString())).willReturn(new Properties()); return factory; } }