Redis Java API

package cn.ac.iscas.pebble.dc.redispool;



import java.io.File;

import java.io.FileOutputStream;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Set;



import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.Pipeline;

import redis.clients.jedis.Response;

import cn.ac.iscas.pebble.dc.hdfs.AppendToFile;

import cn.ac.iscas.pebble.dc.test.P;



public class RedisPoolProxy {

    

    private HashMap<String,JedisPool> poolList = new HashMap<String,JedisPool>();

    

    public static Date yes = null;



    private static RedisPoolProxy _proxy = null;

    

    

    public static RedisPoolProxy getInstance(){

        if(null == _proxy){

            _proxy = new RedisPoolProxy();

        }

        return _proxy;

    }

    public JedisPool getPool(String ip,int port){

        JedisPool pool = poolList.get(ip + ":" + port);

        if(null == pool){

            JedisPoolConfig config = new JedisPoolConfig();

            config.setMaxTotal(P.maxTotal);//最大连接数

            config.setMaxIdle(P.maxIdle);//最大空闲连接数

            

            config.setMaxWaitMillis(P.maxWaitMillis);//获取连接时的最大等待毫秒数

            config.setTestOnBorrow(true);//在获取连接的时候检查有效性, 默认false

            pool = new JedisPool(config, ip, port,60*1000);

            poolList.put(ip + ":" + port, pool);

        }

        return pool;

    }



    public static Integer getDataFromRedis(String ip, int port, int db, String localfilepath,boolean delete){

        JedisPool pool = RedisPoolProxy.getInstance().getPool(ip,port);

    

        int count = 0;

        boolean flag = true;

        if(null != pool){

            Jedis redis = null;

            try {

                redis = pool.getResource();

                Pipeline pipeline = redis.pipelined();

                pipeline.select(db);

                    

                Response<Set<String>> response = pipeline.keys("*");

                pipeline.sync();

                Set<String> keys = response.get();

                if(null != keys){

                    pipeline.get(key);

                    pipeline.del(key);

                }

                List<Object> result = pipeline.syncAndReturnAll();

                for(Object value:result){

                    if(value instanceof String){

                        System.out.println((String)value+"\n");

                        count++;

                    }

                }

            }catch (Exception e) {

                pool.returnBrokenResource(redis);

                e.printStackTrace();

                flag=false;

            } finally {

                if (null != redis  && null != pool ) {

                    pool.returnResource(redis);

                }

            }

        }

        return count;

    }

}

你可能感兴趣的:(redis)