• JAVA中通过Jedis操作Redis连接与插入简单库


    一、简述

      JAVA中通过Jedis操作Redis连接与插入简单库

    二、依赖

            <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>

    三、代码

    package com.test.utils.redis;
    
    import lombok.extern.log4j.Log4j2;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    import redis.clients.jedis.Pipeline;
    import com.test.utils.redis.items.KvItem;
    
    import java.io.IOException;
    import java.util.List;
    
    @Log4j2
    public class RedisUtils {
        private final JedisPool jedisPool;
        private int dbIndex;
    
        /*
        Redis辅助插入类。
        * */
        public RedisUtils(String host, int post, int timeout, String password, boolean ssl, int maxTotal, int maxIdel, int dbIndex) {
            this.dbIndex = dbIndex;
            JedisPoolConfig config = new JedisPoolConfig();
            config.setTestOnBorrow(true);
            config.setMaxWaitMillis(120000);
            config.setMaxIdle(maxIdel);
            config.setMaxTotal(maxTotal);
            jedisPool = new JedisPool(config, host, post, timeout, password, ssl);
        }
    
        public boolean checkConnection() {
            try {
                Jedis jedis = jedisPool.getResource();
                if (jedis != null) {
                    jedis.close();
                    return true;
                }
            } catch (Exception ignored) {
                log.warn("[checkConnection] check redis connection failed. ", ignored);
            }
            return false;
        }
    
        private synchronized Jedis getJedis(int maxRetry) {
            Jedis jedis = null;
            Exception lastEx = new Exception("no error.");
            for (int i = 0; i < maxRetry; i++) {
                if (jedisPool == null) break;
                try {
                    jedis = jedisPool.getResource();
                    if (jedis == null) {
                        Thread.sleep(1000);
                    } else {
                        jedis.select(dbIndex); //临时使用
                        break;
                    }
                } catch (Exception e) {
                    jedis = null;
                    lastEx = e;
                }
            }
            if (jedis == null) {
                log.error("[get a jedis] get a jedis from pools failed, has been retry [" + maxRetry + "] times. please check connection. ", lastEx);
            }
            return jedis;
        }
    
        public synchronized boolean add(List<KvItem> item) {
            if (item == null || item.isEmpty()) return true;
            try {
                Jedis jedis = getJedis(300);
                if (jedis == null) {
                    log.error("[add to redis] add to [" + item.size() + "] items to redis, but get a jedis failed. please check");
                    return false;
                }
                Pipeline p = jedis.pipelined();
                for (KvItem kv : item) {
                    p.set(kv.getKey(), kv.getValue());
                }
                p.sync();
                try {
                    p.close();
                } catch (IOException e) {
                    log.warn("[add to redis] close jedis Pipeline failed.", e);
                }
                jedis.close();
                return true;
            } catch (Exception ex) {
                log.warn("[add to redis] occur a error.", ex);
                return false;
            }
        }
    }
  • 相关阅读:
    51Nod-1013 3的幂的和【快速模幂+逆元】
    51Nod-1082 与7无关的数【进制+打表】
    51Nod-1080 两个数的平方和【暴力法】
    51Nod-1015 水仙花数【进制+查表搜索】
    51Nod-1003 阶乘后面0的数量【分析思维】
    51Nod-1002 数塔取数问题【DP】
    51Nod-1179 最大的最大公约数【暴力】
    51Nod-1018 排序【排序】
    51Nod-1126 求递推序列的第N项【递推序列+模除】
    51Nod-1031 骨牌覆盖【递推】
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/10441976.html
Copyright © 2020-2023  润新知