Search in sources :

Example 51 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project tech by ffyyhh995511.

the class JedisShardService method set.

public String set(String key, String value) {
    ShardedJedis shardedJedis = pool.getResource();
    String rs = shardedJedis.set(key, value);
    shardedJedis.close();
    return rs;
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis)

Example 52 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project oxCore by GluuFederation.

the class RedisShardedProvider method get.

@Override
public Object get(String key) {
    ShardedJedis jedis = pool.getResource();
    try {
        byte[] value = jedis.get(key.getBytes());
        Object deserialized = null;
        if (value != null && value.length > 0) {
            deserialized = SerializationUtils.deserialize(value);
        }
        return deserialized;
    } finally {
        jedis.close();
    }
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis)

Example 53 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project java-example by 1479005017.

the class RedisPoolTest method testShare2.

@Test
public void testShare2() throws Exception {
    ShardedJedisPool jedisPool = RedisConfig.getSharedPool();
    ShardedJedis jedis = jedisPool.getResource();
    System.out.println(String.format("name1: %s", jedis.get("name1")));
    System.out.println(String.format("name2: %s", jedis.get("name2")));
    System.out.println(String.format("name3: %s", jedis.get("name3")));
    System.out.println(String.format("name4: %s", jedis.get("name4")));
    Thread.sleep(10 * 1000);
    // 抛出 JedisException: Could not get a resource from the pool
    System.out.println("sleep 10 seconds, and shutdown one redis");
    System.out.println(String.format("name1: %s", jedis.get("name1")));
    System.out.println(String.format("name2: %s", jedis.get("name2")));
    System.out.println(String.format("name3: %s", jedis.get("name3")));
    System.out.println(String.format("name4: %s", jedis.get("name4")));
    jedis.close();
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Example 54 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project blog by liqianggh.

the class RedisShardedPoolUtil method del.

// 设置有效期  单位是秒
public static Long del(String key) {
    ShardedJedis jedis = null;
    Long result = null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.del(key);
    } catch (Exception e) {
        log.error("del key:{} error", key, e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis)

Example 55 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project blog by liqianggh.

the class RedisShardedPoolUtil method setEx.

// 设置过期时间 秒
public static String setEx(String key, String value, int exTime) {
    ShardedJedis jedis = null;
    String result = null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.setex(key, exTime, value);
    } catch (Exception e) {
        log.error("setex:{} value:{} exTime{} error", key, value, exTime, e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis)

Aggregations

ShardedJedis (redis.clients.jedis.ShardedJedis)83 Test (org.junit.Test)56 JedisShardInfo (redis.clients.jedis.JedisShardInfo)36 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)35 ArrayList (java.util.ArrayList)33 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)33 Jedis (redis.clients.jedis.Jedis)21 URI (java.net.URI)6 ShardedJedisPipeline (redis.clients.jedis.ShardedJedisPipeline)6 Before (org.junit.Before)3 Serializable (java.io.Serializable)2 BinaryShardedJedis (redis.clients.jedis.BinaryShardedJedis)2 JedisException (redis.clients.jedis.exceptions.JedisException)1