zoukankan      html  css  js  c++  java
  • Redis数据库连接

    1.建立maven项目pox.xml导入依赖包

               <dependency>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                    <version>5.1.7.RELEASE</version>
                </dependency>

    2.建立连接

    public class RedisConnectionDemoA {
        public static final String REDIS_ADDRESS = "redis://"自己设置的验证信息"@redis-server:6379/0";
    
        public static void main(String[] args) {
            RedisURI redisURI = RedisURI.create(REDIS_ADDRESS);
            RedisClient redisClient = RedisClient.create(redisURI);
            StatefulRedisConnection<String,String> connect = redisClient.connect();
            System.out.println("【连接返回】"+connect);
            connect.close();
            redisClient.shutdown();
        }
    }

    3.创建连接池管理

    public class RedisConnectionPool {
        private static final int MAX_IDLE = 10 ; // 最大的维持连接数量
        private static final int MIN_IDLE = 1 ; // 最小维持的可用数量
        private static final int MAX_TOTAL = 1 ; // 最大的可用数量
        private static final boolean TEST_ON_BORROW = true ;
        public static void main(String[] args) throws Exception {
            // 1、如果要进行连接池的操作,则肯定要进行一些连接池的基本配置
            GenericObjectPoolConfig config = new GenericObjectPoolConfig() ; // 配置对象
            config.setMaxIdle(MAX_IDLE); // 设置最大维持连接数量
            config.setMinIdle(MIN_IDLE); // 设置最小维持连接数量
            config.setMaxTotal(MAX_TOTAL); // 连接池总共的可用连接数量
            config.setTestOnBorrow(TEST_ON_BORROW); // 连接测试后返回
            // 2、连接池的创建需要依赖于连接的配置类实例
            GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(() -> RedisConnectionUtil.getConnection(), config);
            for (int x = 0 ; x < 10 ; x ++) {
                StatefulRedisConnection<String, String> connection = pool.borrowObject();// 通过连接池获取一个连接
                System.out.println("【连接池对象】" + connection);
                // 发出一个“ping”命令
                System.out.println("【测试连接】ping = " + connection.sync().ping());
                connection.close();
            }
        }
    }

    4.连接池工具类。

    public class RedisConnectionPool {
        private static final int MAX_IDLE = 10 ; // 最大的维持连接数量
        private static final int MIN_IDLE = 1 ; // 最小维持的可用数量
        private static final int MAX_TOTAL = 1 ; // 最大的可用数量
        private static final boolean TEST_ON_BORROW = true ;
        public static void main(String[] args) throws Exception {
            // 1、如果要进行连接池的操作,则肯定要进行一些连接池的基本配置
            GenericObjectPoolConfig config = new GenericObjectPoolConfig() ; // 配置对象
            config.setMaxIdle(MAX_IDLE); // 设置最大维持连接数量
            config.setMinIdle(MIN_IDLE); // 设置最小维持连接数量
            config.setMaxTotal(MAX_TOTAL); // 连接池总共的可用连接数量
            config.setTestOnBorrow(TEST_ON_BORROW); // 连接测试后返回
            // 2、连接池的创建需要依赖于连接的配置类实例
            GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(() -> RedisConnectionUtil.getConnection(), config);
            for (int x = 0 ; x < 10 ; x ++) {
                StatefulRedisConnection<String, String> connection = pool.borrowObject();// 通过连接池获取一个连接
                System.out.println("【连接池对象】" + connection);
                // 发出一个“ping”命令
                System.out.println("【测试连接】ping = " + connection.sync().ping());
                connection.close();
            }
        }
    }
  • 相关阅读:
    centos rm -rf 恢复删除的文件
    默认hosts后面为files dns
    linux shell expr 使用
    QQ,MSN,Skype在线客服代码
    LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
    php读取和保存base64编码的图片内容
    linux shell 字符串操作(长度,查找,替换)详解
    tomcat的简单配置与适用默认的web应用
    mybatis左连接需要输出左表的指定内容与筛选
    first head in html 笔记
  • 原文地址:https://www.cnblogs.com/fcitx/p/11048902.html
Copyright © 2011-2022 走看看