redis客户端工具

1.3.0开始支持

redis客户端工具是一个可以通过yml配置,自动初始化连接池的工具,不需要重复做代码的开发。

引入工具包

<dependency>
    <groupId>com.wueasy</groupId>
    <artifactId>wueasy-data-redis</artifactId>
    <version>最新版本</version>
</dependency>

支持多库和单库,只能使用一种方式,不能同事使用

单库使用

单库可以直接使用spring reids配置信息

配置说明

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 123456
    timeout: 30000
    lettuce:
      pool:
        max-active: 100 # 连接池最大连接数(使用负值表示没有限制) 默认 8
        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
        max-idle: 10 # 连接池中的最大空闲连接 默认 8
        min-idle: 0 # 连接池中的最小空闲连接 默认 0

java中引用redis客户端

方式一(string类型)

@Autowired
private StringRedisTemplate stringRedisTemplate;

方式二(泛型)

如果需要使用泛型方式(自定义类型),可以通过此模式。

@Autowired
private RedisTemplate<String,User> redisTemplate;

测试示例


import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.wueasy.Application;



/**
 * redis测试
 * @author: fallsea
 * @version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class RedisTests {


  @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Test
    public void testObj()  {

        try{

            for(int i=0;i<1000;i++) {

                Long id = stringRedisTemplate.opsForValue().increment("demo", 1L);

                System.err.println(id);

                Thread.sleep(500);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }

    }


}

多库使用

按业务区分不同的redis数据库

配置说明

  • redis:配置redis连接集合,可以配置多个redis连接
    • test1:redis连接的key,可以配置多个,唯一
      • type:客户端类型,lettucejedis;默认jedis,3.4新增
      • database:数据库索引(默认为0)
      • host:服务器地址
      • port:服务器连接端口
      • password:服务器连接密码(默认为空)
      • pool:连接池配置
        • maxIdle:接池中的最大空闲连接,默认值也是8
        • minIdle:连接池中的最小空闲连接,默认值也是0
        • maxTotal:如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
        • maxWaitMillis:等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
      • timeout:连接超时时间(毫秒)
wueasy :
  data:
    redis: #redis配置
      test1:
        database : 1 #数据库索引(默认为0)
        host : 127.0.0.1 #服务器地址
        port : 6379 #服务器连接端口
        password : 123456  #服务器连接密码(默认为空)
        pool : #连接池配置
          maxIdle : 8 # 连接池中的最大空闲连接,默认值也是8。
          minIdle : 0 #连接池中的最小空闲连接,默认值也是0。
          maxTotal : 2000 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
          maxWaitMillis : 1000 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
        timeout : 3000 #连接超时时间(毫秒)
      test2:
        database : 2 #数据库索引(默认为0)
        host : 127.0.0.1 #服务器地址
        port : 6379 #服务器连接端口
        password : 123456  #服务器连接密码(默认为空)
        pool : #连接池配置
          maxIdle : 8 # 连接池中的最大空闲连接,默认值也是8。
          minIdle : 0 #连接池中的最小空闲连接,默认值也是0。
          maxTotal : 2000 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
          maxWaitMillis : 1000 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
        timeout : 3000 #连接超时时间(毫秒)

java中引用redis客户端

通过@Qualifier注解注入,并指定服务名称。

方式一(string类型)

  • 服务名称说明:规则由redis配置的连接key + StringRedisTemplate组成
@Autowired
@Qualifier("test1StringRedisTemplate")
private StringRedisTemplate test1StringRedisTemplate;

@Autowired
@Qualifier("test2StringRedisTemplate")
private StringRedisTemplate test2StringRedisTemplate;

方式二(泛型)

2.2.1开始支持。

如果需要使用泛型方式(自定义类型),可以通过此模式。

  • 服务名称说明:规则由redis配置的连接key + RedisTemplate组成
@Autowired
@Qualifier("test1RedisTemplate")
private RedisTemplate<String,User> test1RedisTemplate;

@Autowired
@Qualifier("test2RedisTemplate")
private RedisTemplate<String,User> test2RedisTemplate;

测试示例


import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.wueasy.Application;



/**
 * redis测试
 * @author: fallsea
 * @version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class RedisTests {


  @Autowired
    @Qualifier("test1StringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

  @Autowired
    @Qualifier("test2StringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate2;


    @Test
    public void testObj()  {

        try{

            for(int i=0;i<1000;i++) {

                Long id = stringRedisTemplate.opsForValue().increment("demo", 1L);

                System.err.println(id);

                Long id2 = stringRedisTemplate2.opsForValue().increment("demo", 1L);

                System.out.println(id2);

                Thread.sleep(500);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }

    }


}
Copyright © wueasy.com 2017-2022 all right reserved,powered by Gitbook未经允许,禁止以任何形式传播 修订时间: 2023-03-16

results matching ""

    No results matching ""