溫馨提示×

redis如何循環(huán)讀出數(shù)據(jù)庫

小新
303
2021-01-21 13:39:37
欄目: 云計算

redis如何循環(huán)讀出數(shù)據(jù)庫

redis循環(huán)讀出數(shù)據(jù)庫的示例:

RedisDO rd = new RedisDO();

rd.open();

Set s = rd.jedis.keys("*");

Iterator it = s.iterator();

while (it.hasNext()) {

String key = (String) it.next();

String value = rd.jedis.get(key);

System.out.println(key + value);

}

rd.close();

rd的算法為集成redis 運算,代碼:

package com.javaer.click.way;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisDO {

public Jedis jedis;

public void close(){

jedis.disconnect();

jedis = null;

}

public Jedis open(){

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxActive(100);

config.setMaxIdle(20);

config.setMaxWait(1000l);

JedisPool pool;

pool = new JedisPool(config, "xxxxxxxx.xx.xx.xx", 6379);

boolean borrowOrOprSuccess = true;

try {

jedis = pool.getResource();

// do redis opt by instance

} catch (JedisConnectionException e) {

borrowOrOprSuccess = false;

if (jedis != null)

pool.returnBrokenResource(jedis);

} finally {

if (borrowOrOprSuccess)

pool.returnResource(jedis);

}

jedis = pool.getResource();

return jedis;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

0