类org.springframework.data.redis.core.RedisConnectionUtils源码实例Demo

下面列出了怎么用org.springframework.data.redis.core.RedisConnectionUtils的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: rqueue   文件: RedisUtils.java
public static int updateAndGetVersion(
    RedisConnectionFactory redisConnectionFactory, String versionDbKey, int defaultVersion) {
  RedisConnection connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
  byte[] versionKey = versionDbKey.getBytes();
  byte[] versionFromDb = connection.get(versionKey);
  if (SerializationUtils.isEmpty(versionFromDb)) {
    Long count =
        connection.eval(
            "return #redis.pcall('keys', 'rqueue-*')".getBytes(), ReturnType.INTEGER, 0);
    if (count != null && count > 0L) {
      int version = 1;
      connection.set(versionKey, String.valueOf(version).getBytes());
      return version;
    }
    connection.set(versionKey, String.valueOf(defaultVersion).getBytes());
    return defaultVersion;
  }
  return Integer.parseInt(new String(versionFromDb));
}
 
源代码2 项目: yshopmall   文件: RedisUtils.java
/**
 * 查找匹配key
 * @param pattern key
 * @return /
 */
public List<String> scan(String pattern) {
    ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>();
    while (cursor.hasNext()) {
        result.add(new String(cursor.next()));
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
源代码3 项目: AutoLoadCache   文件: SpringRedisLock.java
@Override
protected boolean setnx(String key, String val, int expire) {
    if (null == redisConnectionFactory || null == key || key.isEmpty()) {
        return false;
    }
    RedisConnection redisConnection = getConnection();
    try {
        Expiration expiration = Expiration.from(expire, TimeUnit.SECONDS);
        return redisConnection.stringCommands().set(STRING_SERIALIZER.serialize(key), STRING_SERIALIZER.serialize(val), expiration, RedisStringCommands.SetOption.SET_IF_ABSENT);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    } finally {
        RedisConnectionUtils.releaseConnection(redisConnection, redisConnectionFactory);
    }
    return false;
}
 
private ICacheManager createRedisCacheManager(AutoloadCacheProperties config, ISerializer<Object> serializer, JedisConnectionFactory connectionFactory) {
    RedisConnection redisConnection = null;
    try {
        redisConnection = connectionFactory.getConnection();
        AbstractRedisCacheManager cacheManager = null;
        if (redisConnection instanceof JedisClusterConnection) {
            JedisClusterConnection redisClusterConnection = (JedisClusterConnection) redisConnection;
            // 优先使用JedisCluster; 因为JedisClusterConnection 批量处理,需要使用JedisCluster
            JedisCluster jedisCluster = redisClusterConnection.getNativeConnection();
            cacheManager = new JedisClusterCacheManager(jedisCluster, serializer);
        } else {
            cacheManager = new SpringRedisCacheManager(connectionFactory, serializer);
        }
        // 根据需要自行配置
        cacheManager.setHashExpire(config.getJedis().getHashExpire());
        return cacheManager;
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
        throw e;
    } finally {
        RedisConnectionUtils.releaseConnection(redisConnection, connectionFactory);
    }
}
 
源代码5 项目: spring-boot-demo   文件: RedisUtil.java
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}
 
源代码6 项目: yshopmall   文件: RedisUtils.java
/**
 * 分页查询 key
 * @param patternKey key
 * @param page 页码
 * @param size 每页数目
 * @return /
 */
public List<String> findKeysForPage(String patternKey, int page, int size) {
    ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>(size);
    int tmpIndex = 0;
    int fromIndex = page * size;
    int toIndex = page * size + size;
    while (cursor.hasNext()) {
        if (tmpIndex >= fromIndex && tmpIndex < toIndex) {
            result.add(new String(cursor.next()));
            tmpIndex++;
            continue;
        }
        // 获取到满足条件的数据后,就可以退出了
        if(tmpIndex >=toIndex) {
            break;
        }
        tmpIndex++;
        cursor.next();
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
源代码7 项目: spring-boot-demo   文件: RedisUtil.java
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}
 
源代码8 项目: spring-boot-demo   文件: RedisUtil.java
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}
 
源代码9 项目: jeesupport   文件: AbsRedisDao.java
@Override
public void heartbeat( int _idx ){
    database( _idx );
    try{
        tpl.opsForValue().get( "heartbeat" );
    }catch ( Exception e ){
        log.error( "heartbeat 发生错误:IDX=[" + _idx + "]" + e.toString(), e );
    }finally {
        if( tpl != null && tpl.getConnectionFactory() != null ){
            RedisConnectionUtils.unbindConnection( tpl.getConnectionFactory() );
        }
    }
}
 
源代码10 项目: blog_demos   文件: RedisClient.java
/**
 * 释放连接
 * @param redisConnection
 */
private void releaseConnection(RedisConnection redisConnection){
    if(null!=redisConnection && null!=redisTemplate){
        RedisConnectionFactory redisConnectionFactory = redisTemplate.getConnectionFactory();

        if(null!=redisConnectionFactory){
            RedisConnectionUtils.releaseConnection(redisConnection, redisConnectionFactory);
        }
    }
}
 
源代码11 项目: blog_demos   文件: RedisClient.java
/**
 * 释放连接
 * @param redisConnection
 */
private void releaseConnection(RedisConnection redisConnection){
    if(null!=redisConnection && null!=redisTemplate){
        RedisConnectionFactory redisConnectionFactory = redisTemplate.getConnectionFactory();

        if(null!=redisConnectionFactory){
            RedisConnectionUtils.releaseConnection(redisConnection, redisConnectionFactory);
        }
    }
}
 
源代码12 项目: redis-admin   文件: RedisServiceImpl.java
private String getDataType(String serverName, int dbIndex, String key) {
	RedisTemplate redisTemplate = RedisApplication.redisTemplatesMap.get(serverName);
	RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
	connection.select(dbIndex);
	DataType dataType = connection.type(key.getBytes());
	connection.close();
	return dataType.name().toUpperCase();
}
 
源代码13 项目: redis-admin   文件: RedisServiceImpl.java
private Object getKV(String serverName, int dbIndex, String key) {
	RedisTemplate redisTemplate = RedisApplication.redisTemplatesMap.get(serverName);
	RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
	connection.select(dbIndex);
	DataType dataType = connection.type(key.getBytes());
	connection.close();
	Object values = null;
	switch(dataType) {
	case STRING:
		values = redisDao.getSTRING(serverName, dbIndex, key);
		break;
	case LIST:
		values = redisDao.getLIST(serverName, dbIndex, key);
		break;
	case SET:
		values = redisDao.getSET(serverName, dbIndex, key);
		break;
	case ZSET:
		values = redisDao.getZSET(serverName, dbIndex, key);
		break;
	case HASH:
		values = redisDao.getHASH(serverName, dbIndex, key);
		break;
	case NONE:
		//never be here
		values = null;
	}
	return values;
}
 
源代码14 项目: redis-admin   文件: RedisApplication.java
protected void initRedisKeysCache(RedisTemplate redisTemplate, String serverName , int dbIndex) {
	RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
	connection.select(dbIndex);
	Set<byte[]> keysSet = connection.keys("*".getBytes());
	connection.close();
	List<RKey> tempList = new ArrayList<RKey>();
	ConvertUtil.convertByteToString(connection, keysSet, tempList);
	Collections.sort(tempList);
	CopyOnWriteArrayList<RKey> redisKeysList = new CopyOnWriteArrayList<RKey>(tempList);
	if(redisKeysList.size()>0) {
		redisKeysListMap.put(serverName+DEFAULT_SEPARATOR+dbIndex, redisKeysList);
	}
}
 
源代码15 项目: AutoLoadCache   文件: SpringRedisLock.java
@Override
protected void del(String key) {
    if (null == redisConnectionFactory || null == key || key.length() == 0) {
        return;
    }
    RedisConnection redisConnection = getConnection();
    try {
        redisConnection.keyCommands().del(STRING_SERIALIZER.serialize(key));
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    } finally {
        RedisConnectionUtils.releaseConnection(redisConnection, redisConnectionFactory);
    }
}
 
源代码16 项目: rqueue   文件: RedisUtils.java
public static void setVersion(
    RedisConnectionFactory connectionFactory, String versionKey, int version) {
  RedisConnection connection = RedisConnectionUtils.getConnection(connectionFactory);
  byte[] versionKeyBytes = versionKey.getBytes();
  connection.set(versionKeyBytes, String.valueOf(version).getBytes());
}
 
源代码17 项目: jeesupport   文件: AbsRedisDao.java
public void changeDatabase( LettuceConnectionFactory _lcf ){
    RedisConnectionUtils.unbindConnection( tpl.getConnectionFactory() );
    tpl.setConnectionFactory( _lcf );
    index = _lcf.getDatabase();
    heartbeat();
}
 
源代码18 项目: AutoLoadCache   文件: SpringRedisCacheManager.java
public RedisConnectionClient(RedisConnectionFactory redisConnectionFactory, AbstractRedisCacheManager cacheManager) {
    this.redisConnectionFactory = redisConnectionFactory;
    this.redisConnection = RedisConnectionUtils.getConnection(redisConnectionFactory);
    // TransactionSynchronizationManager.hasResource(redisConnectionFactory);
    this.cacheManager = cacheManager;
}
 
源代码19 项目: AutoLoadCache   文件: SpringRedisCacheManager.java
@Override
public void close() {
    RedisConnectionUtils.releaseConnection(redisConnection, redisConnectionFactory);
}
 
源代码20 项目: AutoLoadCache   文件: SpringRedisLock.java
private RedisConnection getConnection() {
    return RedisConnectionUtils.getConnection(redisConnectionFactory);
}
 
 类方法
 同包方法