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

The following examples show how to use redis.clients.jedis.ShardedJedis#close() . 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: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Use transactions to add index file and then delete the old one
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param oldField      the old hash field
 * @param newField      the new hash field
 * @param values        the data values of the old hash field
 * @param fileLength    the data length of the old hash field
 */
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example 2
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 在某个时间点失效
 *
 * @param key      key
 * @param unixTime 失效时间点
 * @return result
 */
public Long expireAt(String key, long unixTime) {
    Long result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.expireAt(key, unixTime);

    } catch (Exception e) {
        log.error("RedisClientTemplate expireAt error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 3
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 4
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改:
 * EX second :设置键的过期时间为 second 秒。 SET key value EX second 效果等同于 SETEX key second value 。
 * PX millisecond :设置键的过期时间为 millisecond 毫秒。 SET key value PX millisecond 效果等同于 PSETEX key millisecond value 。
 * NX :只在键不存在时,才对键进行设置操作。 SET key value NX 效果等同于 SETNX key value 。
 * XX :只在键已经存在时,才对键进行设置操作。
 *
 * @param key
 * @param value
 * @param milliseconds
 * @return
 */
public boolean setnx(String key, String value, long milliseconds) {
	ShardedJedis redis = null;
	try {
		redis = pool.getResource();
		key = getKeyAll(key);
		//nx|xx ex|px  seconds|milliseconds
		String result = redis.set(key, value, "nx", "px", milliseconds);
		return "OK".equalsIgnoreCase(result);
	} catch (RuntimeException e) {
		logger.error("redis setex(String key, int seconds, String value):", e);
		return false;
	} finally {
		redis.close();
	}
}
 
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
/**
 * list左侧插入(队头)
 *
 * @param key    key
 * @param string value
 * @return result
 */
public Long lpush(String key, String string) {
    Long result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }
    try {
        result = shardedJedis.lpush(key, string);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 7
Source File: JedisShardProvider.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public ShardedJedis get() throws JedisException {
ShardedJedis jedis = context.get();
      if(jedis != null)return jedis;
      try {
          jedis = jedisPool.getResource();
      } catch (JedisException e) {
          if(jedis!=null){
          	jedis.close();
          }
          throw e;
      }
      context.set(jedis);
      if(logger.isTraceEnabled()){
      	logger.trace(">>get a redis conn[{}]",jedis.toString());
      }
      return jedis;
  }
 
Example 8
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public List<byte[]> loadFileOnce(String fileDataKey, String fileName, long blockSize) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    List<byte[]> res = new ArrayList<>();
    List<Response<byte[]>> temps = new ArrayList<>();
    int temp = 0;
    //如果不分批次sync容易read time out和Java heap space
    while (temp < blockSize) {
        Response<byte[]> data = pipelined.hget(fileDataKey.getBytes(), getBlockName(fileName, temp));
        temps.add(data);
        if (temp % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
            temps.clear();
            pipelined = shardedJedis.pipelined();
        }
        temp++;
    }
    try {
        pipelined.sync();
    } catch (JedisConnectionException e) {
        log.error("pipelined = {}, blockSize = {}!", pipelined.toString(), blockSize);
        log.error("", e);
    } finally {
        shardedJedis.close();
    }
    res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
    temps.clear();
    return res;
}
 
Example 9
Source File: ShardedJedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkPoolRepairedWhenJedisIsBroken() {
  ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
  ShardedJedis jedis = pool.getResource();
  jedis.disconnect();
  jedis.close();

  jedis = pool.getResource();
  jedis.incr("foo");
  jedis.close();
  pool.destroy();
}
 
Example 10
Source File: ShardedRedisCache.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param nodeName
 * @param key
 * @return <br>
 */
@Override
protected byte[] get(final byte[] nodeName, final byte[] key) {
    ShardedJedis shardedJedis = null;
    try {
        shardedJedis = shardedPool.getResource();
        return shardedJedis.hget(nodeName, key);
    }
    finally {
        if (shardedJedis != null) {
            shardedJedis.close();
        }
    }
}
 
Example 11
Source File: JedisManager.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 设置超时时间
 * 
 * @param key
 * @param seconds
 * @return
 */
public boolean expire(String key, int seconds) {
    ShardedJedis shardedJedis = shardedJedisPool.getResource();
    try {
        return shardedJedis.expire(key, seconds) == 1;
    } catch (Exception e) {
        shardedJedis.close();
        return false;
    }
}
 
Example 12
Source File: JedisShardProvider.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public void release() {
ShardedJedis jedis = context.get();
      if (jedis != null) {
      	context.remove();
      	jedis.close();
      	if(logger.isTraceEnabled()){
          	logger.trace("<<release a redis conn[{}]",jedis.toString());
          }
      }
  }
 
Example 13
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * 存入字符串, 并设置失效时间
 */
@Override
public void setex(String key, int seconds, String value) {
	ShardedJedis redis = null;
	try {
		redis = pool.getResource();
		key = getKeyAll(key);
		redis.setex(key, seconds, value);
	} catch (RuntimeException e) {
		logger.error("redis setex(String key, int seconds, String value):", e);
	} finally{
		redis.close();
	}
	
}
 
Example 14
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * expire reset
 *
 * @param key
 * @param seconds 存活时间,单位/秒
 * @return Integer reply, specifically:
 * 1: the timeout was set.
 * 0: the timeout was not set since the key already has an associated timeout (versions lt 2.1.3), or the key does not exist.
 */
public static long expire(String key, int seconds) {
    Long result = null;
    ShardedJedis client = getInstance();
    try {
        result = client.expire(key, seconds);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return result;
}
 
Example 15
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * exists valid
 *
 * @param key
 * @return Boolean reply, true if the key exists, otherwise false
 */
public static boolean exists(String key) {
    Boolean result = null;
    ShardedJedis client = getInstance();
    try {
        result = client.exists(key);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return result;
}
 
Example 16
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * incrBy i(+i)
 *
 * @param key
 * @param i
 * @return new value after incr
 */
public static Long incrBy(String key, int i) {
    Long result = null;
    ShardedJedis client = getInstance();
    try {
        result = client.incrBy(key, i);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return result;
}
 
Example 17
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Use transactions to delete index file
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param field         the hash field
 * @param blockSize     the index file data block size
 */
@Override
public void deleteFile(String fileLengthKey, String fileDataKey, String field, long blockSize) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    //delete file length
    pipelined.hdel(fileLengthKey.getBytes(), field.getBytes());
    //delete file content
    for (int i = 0; i < blockSize; i++) {
        byte[] blockName = getBlockName(field, i);
        pipelined.hdel(fileDataKey.getBytes(), blockName);
    }
    pipelined.sync();
    shardedJedis.close();
}
 
Example 18
Source File: JedisManager.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * del
 * 
 * @param key
 */
public boolean del(String key) {
    ShardedJedis shardedJedis = shardedJedisPool.getResource();
    try {
        return shardedJedis.del(key) == 1;
    } catch (Exception e) {
        shardedJedis.close();
        return false;
    }
}
 
Example 19
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set String
 *
 * @param key
 * @param value
 * @param seconds 存活时间,单位/秒
 * @return
 */
public static String setStringValue(String key, String value, int seconds) {
    String result = null;
    ShardedJedis client = getInstance();
    try {
        result = client.setex(key, seconds, value);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return result;
}
 
Example 20
Source File: RedisUtil.java    From base with MIT License 4 votes vote down vote up
public static void returnJedis(ShardedJedis jedis) {
	if (jedis != null)
		jedis.close();
}