zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    vue截取video视频中的某一帧
    sm加密前端解析--JS实现国密算法SM2加密
    前端js解析识别图片二维码
    如果让你来设计一个分页功能, 你会怎么设计? 前后端如何交互?
    jQuery事件
    如何实现文件上传?说说你的思路
    实现JWT鉴权机制
    对中间件概念的理解,如何封装 node 中间件
    Node 文件查找的优先级以及 Require 方法的文件查找策略
    angularjs input使用ng-model双向绑定无效bug解决
  • 原文地址:https://www.cnblogs.com/dorothychai/p/4568846.html
Copyright © 2011-2022 走看看