Java Code Examples for redis.clients.jedis.ShardedJedis#get()

The following examples show how to use redis.clients.jedis.ShardedJedis#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: MarsRedisLock.java    From Mars-Java with MIT License 6 votes vote down vote up
/**
 * 释放锁,使用你自己创建的jedis对象
 *
 * @param key          键
 * @param value        值
 * @param shardedJedis 自己创建的jedis对象
 * @return
 */
public boolean unlock(String key, String value, ShardedJedis shardedJedis) {
    try {
        if (shardedJedis == null) {
            return false;
        }
        String val = shardedJedis.get(key);
        if (val != null && val.equals(value)) {
            shardedJedis.del(key);
        }
        return true;
    } catch (Exception e) {
        logger.error("释放redis锁发生异常", e);
        return false;
    } finally {
        marsRedisTemplate.recycleJedis(shardedJedis);
    }
}
 
Example 2
Source File: TestShardedRedis.java    From craft-atom with MIT License 6 votes vote down vote up
@Test
public void testHashSharded() {
	for (int i = 0; i < 10000; i++) {
		String key = "test-" + i;
		String value = "value-" + i;
		shardedRedis.set(key, key, value);
		ShardedJedis sj = shardedPool.getResource();
		try {
			String v = sj.get(key);
			Assert.assertEquals(value, v);
		} finally {
			sj.close();
		}
	}
	
	System.out.println(String.format("[CRAFT-ATOM-REDIS] (^_^)  <%s>  Case -> test hash sharded. ", CaseCounter.incr(1)));
}
 
Example 3
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 获取字符串
 */
@Override
public String get(String key) {
	ShardedJedis redis = null;
	String result = null;
	try {
		redis = pool.getResource();
		key = getKeyAll(key);
		result = redis.get(key);
		return result;
	} catch (RuntimeException e) { 
        if(redis != null ) {
			redis.close();
		}
        logger.error("redis get(String key):", e);
        return result;
	} finally{ 
           if(redis != null ) {
			redis.close();
		}
	}
}
 
Example 4
Source File: RedisShardedPoolUtil.java    From mmall20180107 with Apache License 2.0 6 votes vote down vote up
public static String get(String key){

        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{}  error",key,e);
            RedisShardedPool.returnBrokenResource(jedis);
            e.printStackTrace();
        }


        RedisShardedPool.returnResource(jedis);

        return result;

    }
 
Example 5
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 获取多个值
 *
 * @param keys key集合
 * @return result
 */
public Map<String, String> get(Collection<String> keys) {
    Map<String, String> result = new HashMap<>(keys.size());
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        for (String key : keys) {
            String value = shardedJedis.get(key);
            result.put(key, value);
        }
    } catch (Exception e) {
        log.error("RedisClientTemplate get error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 6
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 获取单个值
 *
 * @param key key
 * @return value
 */
public String get(String key) {
    String result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.get(key);

    } catch (Exception e) {
        log.error("RedisClientTemplate get error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 7
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get Object
 *
 * @param key
 * @return
 */
public static Object getObjectValue(String key) {
    Object obj = null;
    ShardedJedis client = getInstance();
    try {
        byte[] bytes = client.get(key.getBytes());
        if (bytes != null && bytes.length > 0) {
            obj = unserialize(bytes);
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return obj;
}
 
Example 8
Source File: RedisShardedPoolUtil.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
public static String get(String key){
    ShardedJedis jedis = null;
    String result=null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.get(key);
    } catch (Exception e) {
        log.error("get key:{} error",key,e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
 
Example 9
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get String
 *
 * @param key
 * @return
 */
public static String getStringValue(String key) {
    String value = null;
    ShardedJedis client = getInstance();
    try {
        value = client.get(key);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return value;
}
 
Example 10
Source File: JedisManager.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * get
 * 
 * @param key
 * @return
 */
public String get(String key) {
    ShardedJedis shardedJedis = shardedJedisPool.getResource();
    try {
        return shardedJedis.get(key);
    } catch (Exception e) {
        shardedJedis.close();
        return null;
    }
}
 
Example 11
Source File: ShardedRedisCache.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param key
 * @return <br>
 */
@Override
protected byte[] get(final byte[] key) {
    ShardedJedis shardedJedis = null;
    try {
        shardedJedis = shardedPool.getResource();
        return shardedJedis.get(key);
    }
    finally {
        if (shardedJedis != null) {
            shardedJedis.close();
        }
    }

}
 
Example 12
Source File: RedisClient.java    From dubbo-plus with Apache License 2.0 5 votes vote down vote up
public byte[] getValue(byte[] key){
    ShardedJedis jedis = JEDIS_POOL.getResource();
    try{
        return jedis.get(key);
    }finally {
        JEDIS_POOL.returnResource(jedis);
    }
}
 
Example 13
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    Assert.hasText(key);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        return jedis.get(key);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 14
Source File: MarsRedisTemplate.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 获取redis值
 *
 * @param key
 * @return
 */
public byte[] get(byte[] key) {
    ShardedJedis jedis = null;
    try {
        jedis = getShardedJedis();
        return jedis.get(key);
    } catch (Exception e) {
        logger.error("get redis value error:" + key, e);
    } finally {
        recycleJedis(jedis);
    }

    return null;
}
 
Example 15
Source File: JedisCacheService.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
@Override
public String get(String key) {

	long start = System.currentTimeMillis();

	String result = null;
	ShardedJedis jedis = null;
	try {
		jedis  = cacheFactory.getResource();
		result = jedis.get(key);
	}
	catch (Exception e) {
		log.error("获取缓存失败:", e);
	}
	finally{
		if(jedis != null){
			try {cacheFactory.returnResource(jedis);}
			catch (Exception e2) {
				log.error("不能释放缓存操纵对象:",e2);
			}
		}
	}

	if(report){
		log.info(String.format("The [%s]'s cache %s, total time of %s milliseconds by it.", key, result == null ? "did not got":"is got", System.currentTimeMillis() - start));
	}

	return result;
}
 
Example 16
Source File: JedisCacheService.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObject(String key){

	long start = System.currentTimeMillis();

	Object result = null;
	ShardedJedis jedis = null;
	try {
		jedis = cacheFactory.getResource();
		byte[] value = jedis.get(key.getBytes("UTF-8"));
		result = serializer.deserialize(value);
	}
	catch (Exception e) {
		log.error("获取缓存失败:", e);
	}
	finally{
		if(jedis != null){
			try {cacheFactory.returnResource(jedis);}
			catch (Exception e2) {
				log.error("不能释放缓存操纵对象:",e2);
			}
		}
	}

	if(report){
		log.info(String.format("The [%s]'s cache %s, total time of %s milliseconds by it.", key, result == null ? "did not got":"is got", System.currentTimeMillis() - start));
	}

	return result;
}
 
Example 17
Source File: JedisDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 多机分布式+连接池。
 */
private static void shardPool() {
    // 生成多机连接信息列表
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add( new JedisShardInfo("127.0.0.1", 6379) );
    shards.add( new JedisShardInfo("192.168.56.102", 6379) );
    
    // 生成连接池配置信息
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(10);
    config.setMaxTotal(30);
    config.setMaxWaitMillis(3*1000);
    
    // 在应用初始化的时候生成连接池
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    
    // 在业务操作时,从连接池获取连接
    ShardedJedis client = pool.getResource();
    try {
        // 执行指令
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        // 业务操作完成,将连接返回给连接池
        if (null != client) {
            pool.returnResource(client);
        }
    } // end of try block
    
    // 应用关闭时,释放连接池资源
    pool.destroy();
}