Jedis连接Redis读写基本操作

Jedis连接Redis读写基本操作
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说Jedis连接Redis读写基本操作,希望能够帮助大家进步!!!

  • 依赖
  <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>
  • 测试
  @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"));
  }
  • 存入对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User  implements Serializable {
  private String name;
  private String Date;
}
  • 导入依赖
    <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>
  • 测试
  @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();
  • 读取对象

@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();
  }
  • 字符串现使存入

 <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();
  }
  • 获取字符串格式
 @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();
  }

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/17990

发表评论