Jedis连接Redis,Lettuce连接Redis

Jedis连接Redis

1. 创建maven项目

2. 引入依赖

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
  </dependencies>

3. 测试

  @Test
  public void Test1() {
//    连接Redis
    Jedis jedis = new Jedis("localhost", 6379);
//    操作Redis - 因为Redis命令是什么,Jedis方法就是什么
    jedis.set("name", "李四");
//    释放资源
    jedis.close();
    System.out.println(jedis.get("name"));
  }

Jedis如何存储一个对象到Redis

准备一个User实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User  implements Serializable {
  private String name;
  private String Date;
}

导入spring-context依赖

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

创建Demo测试类,编写内容

  @Test
  public void Test2() {
//    连接Redis
    Jedis jedis = new Jedis("localhost", 6379);
//    准备key(String) -value (user)
    String key = "user";
    User value = new User("张三", "1999");
//    将key和value转换为byte[]
    byte[] byteKey = SerializationUtils.serialize(key);
    byte[] byteValue = SerializationUtils.serialize(value);
//    将key和value存储到Redis
    jedis.set(byteKey, byteValue);
//    释放资源
    jedis.close();

Jedis如何从Redis读取一个对象

测试

@Test
  public void Test3() {
//    连接Redis
    Jedis jedis = new Jedis("localhost", 6379);
//    准备一个key
    String key = "user";
//    将key转换为字节数组类型
    byte[] byteKey = SerializationUtils.serialize(key);
//    jedis去redis中获取value
    byte[] value = jedis.get(byteKey);
//    将value反序列化
    User user = (User) SerializationUtils.deserialize(value);
    System.out.println(user);
//    释放资源
    jedis.close();
  }

如何将一个对象以字符串的形式存储到Redis

导入依赖

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.70</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>

测试

  @Test
  public void test1(){
//    连接Redis
    Jedis jedis = new Jedis("localhost",6379);
//    准备key(String) - value(User)
    String key = "stringUser";
    User value = new User("帅哥","19999");
//    使用fastJSON将value转换为json字符串
    String stringValue = JSON.toJSONString(value);
//    存储到Redis中
    jedis.set(key,stringValue);
//    释放资源
    jedis.close();
  }

如何得到Redis中以字符串(JSON)存在形式的对象

依赖同上

测试

 @Test
  public void test2(){
//    连接Redis
    Jedis jedis = new Jedis("localhost",6379);
//准备一个key
    String key = "stringUser";
    //去Redis中查询value
    String value = jedis.get(key);
    //将value反序列化一个对象
    User user = JSON.parseObject(value, User.class);
    System.out.println("user = "+ user);
//    释放资源
    jedis.close();
  }