• 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+"
    ");
                            count++;
                        }
                    }
                }catch (Exception e) {
                    pool.returnBrokenResource(redis);
                    e.printStackTrace();
                    flag=false;
                } finally {
                    if (null != redis  && null != pool ) {
                        pool.returnResource(redis);
                    }
                }
            }
            return count;
        }
    }
  • 相关阅读:
    C#中页面之间跳转方法比较
    C#中web页面之间传递数组参数
    js跳转传递参数
    枚举
    js操作做GridView
    数据库登录问题汇总
    数据库登录遇到的问题
    关于配置服务器(IIS7)(二)
    关于配置服务器(IIS7)
    使用JavaScript设置、获取父子页面中的值
  • 原文地址:https://www.cnblogs.com/dorothychai/p/4568846.html
Copyright © 2020-2023  润新知