zoukankan      html  css  js  c++  java
  • redis(lettuce)

    1.依赖包

                <dependency>
                    <groupId>org.apache.commons</groupId><!--连接池依赖包-->
                    <artifactId>commons-pool2</artifactId>
                    <version>2.6.2</version>
                </dependency>
    
                <dependency><!--lettuce依赖包-->
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                    <version>5.1.7.RELEASE</version>
                </dependency>            
    

    2.lettuce结合commons-pool2连接池结合的连接redis数据库类

    package com.yootk.lettuce.util;
    
    import io.lettuce.core.RedisClient;
    import io.lettuce.core.RedisURI;
    import io.lettuce.core.api.StatefulRedisConnection;
    import io.lettuce.core.support.ConnectionPoolSupport;
    import org.apache.commons.pool2.impl.GenericObjectPool;
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    
    public class RedisConnectionUtil {
        public static final String REDIS_ADDRESS = "redis://hellolee@redis-server:6379/0";
        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 ;
        private static GenericObjectPool<StatefulRedisConnection<String, String>> pool ;
        private static final RedisURI REDIS_URI = RedisURI.create(REDIS_ADDRESS) ;
        private static final RedisClient REDIS_CLIENT = RedisClient.create(REDIS_URI) ; // 构建RedisClient实例
        private static final ThreadLocal<StatefulRedisConnection> REDIS_CONNECTION_THREAD_LOCAL = new ThreadLocal<>()  ;
        static {
            // 1、如果要进行连接池的操作,则肯定要进行一些连接池的基本配置
            GenericObjectPoolConfig config = new GenericObjectPoolConfig() ; // 配置对象
            config.setMaxIdle(MAX_IDLE); // 设置最大维持连接数量
            config.setMinIdle(MIN_IDLE); // 设置最小维持连接数量
            config.setMaxTotal(MAX_TOTAL); // 连接池总共的可用连接数量
            config.setTestOnBorrow(TEST_ON_BORROW); // 连接测试后返回
            // 2、连接池的创建需要依赖于连接的配置类实例
            pool = ConnectionPoolSupport.createGenericObjectPool(() -> REDIS_CLIENT.connect(), config);
        }
        public static StatefulRedisConnection getConnection() {
            StatefulRedisConnection<String, String> connection = REDIS_CONNECTION_THREAD_LOCAL.get() ;
            if (connection == null) {
                connection = build() ;
                REDIS_CONNECTION_THREAD_LOCAL.set(connection);
            }
            return connection ;
        }
        public static void close() {
            StatefulRedisConnection<String, String> connection = REDIS_CONNECTION_THREAD_LOCAL.get() ;
            if (connection != null) {
                connection.close();
                REDIS_CONNECTION_THREAD_LOCAL.remove();
            }
        }
        private static StatefulRedisConnection build() {
            try {
                return pool.borrowObject() ;
            } catch (Exception e) {
                return null ;
            }
        }
    }
    

    3.举例

    package com.yootk.lettuce.data;
    import com.yootk.lettuce.util.RedisConnectionUtil;
    import io.lettuce.core.api.async.RedisAsyncCommands;
    import java.util.HashMap;
    import java.util.Map;
    public class HashCommand {
        public static void main(String[] args) throws Exception{
            RedisAsyncCommands commands = RedisConnectionUtil.getConnection().async();// 创建操作命令
            commands.hset("member-lee", "name", "小李老师").get();
            // 在进行Hash内容设置的时候还可以将内容保存在Map集合里面,进行整体的内容定义
            Map<String, String> map = new HashMap<>();
            map.put("age", String.valueOf(18));
            map.put("salar", String.valueOf(1.1));
            commands.hmset("member-lee", map).get();
            System.out.println("【获取Hash数据】" + commands.hgetall("member-lee").get());
            RedisConnectionUtil.close();
        }
    }
  • 相关阅读:
    Android面试题目整理与解说(二)
    大学?做码农?做project师?
    图形学领域的关键算法及源代码链接
    假设在本地搭一个server和mysql数据库环境,假设使用java来訪问数据库
    [容斥原理] hdu 4135 Co-prime
    leetcode第一刷_Merge Intervals
    关于HashMap的一些深入探索与理解
    摄像头拍照上传
    rowid快速分页解析
    flare-spork: 自己维护的Pig on Spark项目
  • 原文地址:https://www.cnblogs.com/wxl123/p/11110371.html
Copyright © 2011-2022 走看看