Redis的Java客户端

Redis的Java客户端

Jedis线程不安全,lettuce线程安全

Jedis

测试

引入依赖

<dependencies>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.7.0</version>
    </dependency>
</dependencies>

Test.java

public class JedisTest {
    private Jedis jedis;

    @BeforeEach
    void setUp() {
        //1.建立连接
        jedis=new Jedis("192.168.164.128",6379);
        //2.设置密码
        jedis.auth("123456");
        //3.选择库
        jedis.select(0);
    }

    @Test
    void testString() {
        //存入
        String result=jedis.set("name","熊哥");
        System.out.println("result="+result);
        //获取
        String name=jedis.get("name");
        System.out.println("name="+name);

        //存入
        jedis.hset("user:1","name","Jack");
        jedis.hset("user:1","age","21");
        //获取
        Map<String, String> map = jedis.hgetAll("user:1");
        System.out.println(map);
    }

    @AfterEach
    void tearDown() {
        //关闭连接
        if(jedis!=null){
            jedis.close();
        }
    }
}

Jedis连接池

Jedis本身是线程不安全的,且频繁的创建和销毁连接有性能损耗,因此推荐使用Jedis连接池代替Jedis直接连接

JedisConnectionFactory.java

public class JedisConnectionFactory {
    //连接池对象
    private static final JedisPool jedisPool;

    //初始化
    static {
        //配置连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);  //最大8个连接
        poolConfig.setMaxIdle(8);   //最大空闲数量
        poolConfig.setMinIdle(0);   //最小空闲数量
        poolConfig.setMaxWaitMillis(1000);  //当池内没有返回对象时最大等待时间

        //创建连接池对象(JedisPoolConfig,ip,port,timeout,password)
        jedisPool=new JedisPool(poolConfig,"192.168.164.128",6379,1000,"123456");
    }
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

Test.java(改变建立连接的语句)

public class JedisTest {
    private Jedis jedis;

    @BeforeEach
    void setUp() {
        //1.建立连接
//        jedis=new Jedis("192.168.164.128",6379);
        jedis= JedisConnectionFactory.getJedis();
        //2.设置密码
        jedis.auth("123456");
        //3.选择库
        jedis.select(0);
    }

    @Test
    void testString() {
        //存入
        String result=jedis.set("name","熊哥");
        System.out.println("result="+result);
        //获取
        String name=jedis.get("name");
        System.out.println("name="+name);

        //存入
        jedis.hset("user:1","name","Jack");
        jedis.hset("user:1","age","21");
        //获取
        Map<String, String> map = jedis.hgetAll("user:1");
        System.out.println(map);
    }

    @AfterEach
    void tearDown() {
        //关闭连接
        if(jedis!=null){
            jedis.close();
        }
    }
}

SpringDataRedis

SpringDataRedis中提供RedisTemplate工具类,封装了各种对Redis的操作。springboot2.x以前默认支持Jedis,以后默认支持lettuce

依赖

<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--common-pool-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

application.yaml

spring:
  redis:
    host: 192.168.164.128
    port: 6379
    password: 123456
    database: 0
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

Test.java

@SpringBootTest
class RedisDemoApplicationTests {
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        //写入一条String数据
        redisTemplate.opsForValue().set("name","王五");
        //获取
        Object name=redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}

序列化以实现跨平台存储(在上面的基础上修改)

添加配置类RedisConfig.java

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //创建RedisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //设置连接工厂
        template.setConnectionFactory(connectionFactory);
        //创建JSON序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        //设置Key的序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        //设置Value的序列化
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        //返回
        return template;
    }
}

Test.java中修改下面这行

private RedisTemplate<String,Object> redisTemplate;

若linux下还是显示乱码,在启动客户端时最后加上 --raw

StringRedisTemplate(上面的经典白学)

Spring默认提供了一个StringRedisTemplate类,它的key和value的序列化方式默认是String方式。省去了自定义RedisTemplate的过程

Test.java

@SpringBootTest
public class RedisStringTest {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void contextLoads() {
        //写入一条String数据
        stringRedisTemplate.opsForValue().set("name","王五");
        //获取
        Object name=stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}

热门相关:无量真仙   天启预报   豪门闪婚:帝少的神秘冷妻   第一神算:纨绔大小姐   苏醒的秘密