• Redis学习笔记2:基于springboot的lettuce redis客户端eagerInitialization提前初始化连接


    LettuceConnectionFactory连接工厂类默认是不会提前初始化本地物理连接的,也就是懒加载模式,只有等到客户端的RedisTemplate等具体要操作Redis时才会去建立连接。

    一个对springboot redis框架进行重写,支持lettuce、jedis、连接池、同时连接多个集群、多个redis数据库、开发自定义属性配置的开源SDK

    <dependency>
        <groupId>io.github.mingyang66groupId>
        <artifactId>emily-spring-boot-redisartifactId>
        <version>4.3.9version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    GitHub地址:https://github.com/mingyang66/spring-parent

    一、LettuceConnectionFactory连接工厂类属性
    //是否提前初始化共享本地连接,默认:false
    private boolean eagerInitialization = false;
    private @Nullable SharedConnection<byte[]> connection;
    private @Nullable SharedConnection<ByteBuffer> reactiveConnection;
    
    • 1
    • 2
    • 3
    • 4
    二、LettuceConnectionFactory#afterPropertiesSet方法提前初始化本地连接
    	public void afterPropertiesSet() {
        //创建Redis连接对象
    		this.client = createClient();
    
    		this.connectionProvider = new ExceptionTranslatingConnectionProvider(createConnectionProvider(client, CODEC));
    		this.reactiveConnectionProvider = new ExceptionTranslatingConnectionProvider(
    				createConnectionProvider(client, LettuceReactiveRedisConnection.CODEC));
    
    		if (isClusterAware()) {
    
    			this.clusterCommandExecutor = new ClusterCommandExecutor(
    					new LettuceClusterTopologyProvider((RedisClusterClient) client),
    					new LettuceClusterConnection.LettuceClusterNodeResourceProvider(this.connectionProvider),
    					EXCEPTION_TRANSLATION);
    		}
    
    		this.initialized = true;
       	//只有在共享本地连接对象shareNativeConnection为true,并且eagerInitialization为true的时候才会进行本地连接初始化
    		if (getEagerInitialization() && getShareNativeConnection()) {
    			initConnection();
    		}
    	}
    
     public boolean getEagerInitialization() {
    		return eagerInitialization;
    	}
    public boolean getShareNativeConnection() {
    		return shareNativeConnection;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    LettuceConnectionFactory连接工厂类实现了InitializingBean接口,会在类初始化完成后调用afterPropertiesSet方法

    三、LettuceConnectionFactory#initConnection初始化本地连接对象
    	public void initConnection() {
    		//重置调当前连接工厂类的连接
    		resetConnection();
        //获取并初始化通用共享本地连接,初始化属性connection
    		if (isClusterAware()) {
    			getSharedClusterConnection();
    		} else {
    			getSharedConnection();
    		}
       //获取并初始化响应式共享本地连接,初始化属性reactiveConnection
    		getSharedReactiveConnection();
    	}
    	public void resetConnection() {
    
    		assertInitialized();
    
    		Optionals.toStream(Optional.ofNullable(connection), Optional.ofNullable(reactiveConnection))
    				.forEach(SharedConnection::resetConnection);
    
    		synchronized (this.connectionMonitor) {
          //将连接工厂类的两个属性置为null
    			this.connection = null;
    			this.reactiveConnection = null;
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    JavaScript系列之逻辑运算符
    【代码精读】optee_os的启动
    应用架构的演进:亚马逊的微服务实践
    【算法每日一练]-图论(保姆级教程 篇3(遍历))#图的遍历 #奶牛牧场 #杂务
    算法详解——贪心算法
    [附源码]java毕业设计法律咨询信息管理系统
    Compose 动画艺术探索之属性动画
    10.面试题——JVM
    Win10系统如何安装配置maven
    2023年电工(中级)证模拟考试题库及电工(中级)理论考试试题
  • 原文地址:https://blog.csdn.net/yaomingyang/article/details/134087408