Java Code Examples for io.lettuce.core.api.sync.RedisCommands#get()

The following examples show how to use io.lettuce.core.api.sync.RedisCommands#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LettuceController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/lettuce-case")
@ResponseBody
public String lettuceCase() {
    RedisClient redisClient = RedisClient.create("redis://" + address);
    StatefulRedisConnection<String, String> connection0 = redisClient.connect();
    RedisCommands<String, String> syncCommand = connection0.sync();
    syncCommand.get("key");

    StatefulRedisConnection<String, String> connection1 = redisClient.connect();
    RedisAsyncCommands<String, String> asyncCommands = connection1.async();
    asyncCommands.setAutoFlushCommands(false);
    List<RedisFuture<?>> futures = new ArrayList<>();
    futures.add(asyncCommands.set("key0", "value0"));
    futures.add(asyncCommands.set("key1", "value1"));
    asyncCommands.flushCommands();
    LettuceFutures.awaitAll(5, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()]));

    connection0.close();
    connection1.close();
    redisClient.shutdown();
    return "Success";
}
 
Example 2
Source File: TestRedisTemplate.java    From ad with Apache License 2.0 5 votes vote down vote up
@Test
public void nativeAPI() {
    RedisClient redisClient = RedisClient.create("redis://127.0.0.1:6379/0");
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    String set = commands.set("String_key", "String_value");
    String value = commands.get("String_key");
    log.debug(value);

    Object _value = redisTemplate.opsForValue().get("String_key");
    log.debug("{}", _value);
}
 
Example 3
Source File: RedisDataSource.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public String readSource() {
    if (this.redisClient == null) {
        throw new IllegalStateException("Redis client has not been initialized or error occurred");
    }
    RedisCommands<String, String> stringRedisCommands = redisClient.connect().sync();
    return stringRedisCommands.get(ruleKey);
}
 
Example 4
Source File: StandaloneRedisDataSourceTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitAndParseFlowRuleSuccess() {
    RedisCommands<String, String> stringRedisCommands = client.connect().sync();
    String value = stringRedisCommands.get(ruleKey);
    List<FlowRule> flowRules = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRules.size(), 1);
    stringRedisCommands.del(ruleKey);
}
 
Example 5
Source File: StandaloneRedisDataSourceTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadResourceFail() {
    RedisCommands<String, String> stringRedisCommands = client.connect().sync();
    stringRedisCommands.del(ruleKey);
    String value = stringRedisCommands.get(ruleKey);
    Assert.assertNull(value);
}
 
Example 6
Source File: SentinelModeRedisDataSourceTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectToSentinelAndPubMsgSuccess() {
    int maxQueueingTimeMs = new Random().nextInt();
    String flowRulesJson =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisCommands<String, String> subCommands = client.connect().sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRulesJson);
    subCommands.publish(channel, flowRulesJson);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 7
Source File: RedisDataSource.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public String readSource() {
    if (this.redisClient == null) {
        throw new IllegalStateException("Redis client has not been initialized or error occurred");
    }
    RedisCommands<String, String> stringRedisCommands = redisClient.connect().sync();
    return stringRedisCommands.get(ruleKey);
}
 
Example 8
Source File: StandaloneRedisDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitAndParseFlowRuleSuccess() {
    RedisCommands<String, String> stringRedisCommands = client.connect().sync();
    String value = stringRedisCommands.get(ruleKey);
    List<FlowRule> flowRules = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRules.size(), 1);
    stringRedisCommands.del(ruleKey);
}
 
Example 9
Source File: StandaloneRedisDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadResourceFail() {
    RedisCommands<String, String> stringRedisCommands = client.connect().sync();
    stringRedisCommands.del(ruleKey);
    String value = stringRedisCommands.get(ruleKey);
    Assert.assertNull(value);
}
 
Example 10
Source File: SentinelModeRedisDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectToSentinelAndPubMsgSuccess() {
    int maxQueueingTimeMs = new Random().nextInt();
    String flowRulesJson =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisCommands<String, String> subCommands = client.connect().sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRulesJson);
    subCommands.publish(channel, flowRulesJson);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 11
Source File: RedisBackedSessionMap.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Session get(SessionId id) throws NoSuchSessionException {
  Require.nonNull("Session ID", id);

  URI uri = getUri(id);

  RedisCommands<String, String> commands = connection.sync();
  String rawCapabilities = commands.get(capabilitiesKey(id));
  Capabilities caps = rawCapabilities == null ?
    new ImmutableCapabilities() :
    JSON.toType(rawCapabilities, Capabilities.class);

  return new Session(id, uri, caps);
}
 
Example 12
Source File: KVEntry.java    From Arraybot with Apache License 2.0 4 votes vote down vote up
/**
 * Gets a value from Redis.
 * @param key The key.
 * @return The value, can be null.
 */
public final String get(String key) {
    RedisCommands resource = redis.getResource();
    Object result = resource.get(UDatabase.getKey(category, key));
    return result == null ? null : result.toString();
}
 
Example 13
Source File: LettuceIntegrationLiveTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void givenAString_thenSaveItAsRedisStringsSync() {

    RedisCommands<String, String> syncCommands = redisConnection.sync();

    String key = "key";
    String value = "value";

    syncCommands.set(key, value);
    String response = syncCommands.get(key);

    Assert.assertEquals(value, response);
}