redis学习02--使用Jedis调用Redis

1.Jedis连接Redis

1.1 在pom.xml文件中,引入maven依赖

 1 <dependencies>
 2  <!--1、Jedis依赖包-->
 3  <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
 4  <dependency>
 5    <groupId>redis.clients</groupId>
 6    <artifactId>jedis</artifactId>
 7    <version>2.9.0</version>
 8  </dependency>
 9  <!--2、Junit测试-->
10  <dependency>
11    <groupId>junit</groupId>
12    <artifactId>junit</artifactId>
13    <version>4.13</version>
14  </dependency>
15  <!--3、Lombok依赖包-->
16  <dependency>
17    <groupId>org.projectlombok</groupId>
18    <artifactId>lombok</artifactId>
19    <version>1.18.12</version>
20  </dependency>
21 </dependencies>

1.2 测试连接Redis

 1 public class Demo1 {
 2    @Test
 3    public void set(){
 4        //1、连接Redis
 5        Jedis jedis = new Jedis("127.0.0.1",6379);
 6        //2、操作Redis - redis的命令是什么jedis对应的方法就是什么
 7        jedis.set("name","zhangsan");
 8        //3、释放资源
 9        jedis.close();
10   }
11    @Test
12    public void get(){
13        //1、连接Redis
14        Jedis jedis = new Jedis("127.0.0.1",6379);
15        //2、操作Redis - redis的命令是什么jedis对应的方法就是什么
16        String value = jedis.get("name");
17        System.out.println(value);
18        //3、释放资源
19        jedis.close();
20   }
21 }

2.存储一个对象到Redis

2.1 准备一个User类

1 @Data
2 @AllArgsConstructor
3 @NoArgsConstructor
4 public class User implements Serializable {
5    private Long id;
6    private String name;
7    private Date birthday;
8 }

导入spring-context依赖

<!--4、导入spring-context-->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>4.3.20.RELEASE</version>
</dependency>

2.2 创建demo测试类

 1 public class Demo2 {
 2    //存储对象 -- 以byte[]形式存储在redis中
 3    @Test
 4    public void setByteArray(){
 5        //1、连接redis服务
 6        Jedis jedis = new Jedis("127.0.0.1",6379);
 7        //2.1 准备key(String) - value(User)
 8        String key = "user";
 9        User user = new User(1L,"张三",new Date());
10        //2.2 将key和value转换为byte[]
11        byte[] byteKey = SerializationUtils.serialize(key);
12        //user对象序列化和反序列化,需要在User类实现Serializable接口
13        byte[] byteValue = SerializationUtils.serialize(user);
14        //2.3 将key和value存储到redis
15        jedis.set(byteKey,byteValue);
16        //3、释放资源
17        jedis.close();
18   }
19    @Test
20    public void getByteArray(){
21        //1、连接redis服务
22        Jedis jedis = new Jedis("127.0.0.1",6379);
23        //2.1 准备key(String)
24        String key = "user";
25        //2.2 将key转换为byte[]
26        byte[] byteKey = SerializationUtils.serialize(key);
27        //2.3 获取value
28        byte[] byteValue = jedis.get(byteKey);
29        //2.4 将value反序列化为user对象
30        User user2 = (User)SerializationUtils.deserialize(byteValue);
31        System.out.println(user2);
32        //3、释放资源
33        jedis.close();
34   }
35 }

2.3 Jedis如何存储一个对象到Redis,以String的形式存储

导入fastjson依赖

<!--5、导入fastjson-->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.71</version>
</dependency>
 1 public class Demo3 {
 2    //存储的对象,以String形式
 3    @Test
 4    public void setString(){
 5        //1、连接redis
 6        Jedis jedis = new Jedis("127.0.0.1",6379);
 7        //2.1 准备key(String) - value(User)
 8        String stringKey = "stringUser";
 9        User value = new User(2L,"李四",new Date());
10        //2.2 使用fastjson将value格式化为json字符串
11        String stringVlue = JSON.toJSONString(value);
12        //2.3 存储到redis中
13        jedis.set(stringKey,stringVlue);
14        //3关闭连接
15        jedis.close();
16   }
17    @Test
18    public void getString(){
19        //1、连接redis
20        Jedis jedis = new Jedis("127.0.0.1",6379);
21        //2.1 准备key
22        String stringKey = "stringUser";
23        //2.2 去redis中查询value
24        String stringValue =jedis.get(stringKey);
25        //2.3 将value反序列化为User
26        User user = JSON.parseObject(stringValue,User.class);
27        System.out.println(user);
28        //3关闭连接
29        jedis.close();
30   }
31 }

2.4 Jedis连接池操作

 1 @Test
 2 public void pool2(){
 3    //1、创建连接池的配置信息
 4    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
 5    //连接池中最大的活跃数
 6    config.setMaxTotal(100);
 7    //最大空闲数
 8    config.setMaxIdle(10);
 9    //最大空闲数
10    config.setMinIdle(5);
11    //当连接池空了之后,多久没获取到jedis对象就超时,单位毫秒
12    config.setMaxWaitMillis(3000);
13    //2、创建连接池
14    JedisPool pool = new JedisPool(config,"127.0.0.1",6379);
15    //3、获取jedis
16    Jedis jedis = pool.getResource();
17    //4、操作
18    String value = jedis.get("stringUser");
19    System.out.println(value);
20    //6、释放连接
21    jedis.close();
22 }

2.5 Redis管道操作

 1 //Redis的管道操作
 2 @Test
 3 public void pipeline(){
 4    //1、创建连接
 5    JedisPool pool = new JedisPool("127.0.0.1",6379);
 6    long start = System.currentTimeMillis();
 7    //2、获取一个连接对象
 8    Jedis jedis = pool.getResource();
 9 //       //3、执行incr - 10000次
10 //       for (int i = 0; i < 50000; i++) {
11 //           jedis.incr("pp");
12 //       }
13 //       //4、释放资源
14 //       jedis.close();
15    //------------------
16 
17    //3、创建管道
18    Pipeline pipeline = jedis.pipelined();
19    //4、执行incr - 10000次放到管道中
20    for (int i = 0; i < 50000; i++) {
21        pipeline.incr("qq");
22   }
23    pipeline.syncAndReturnAll();
24    //5、释放资源
25    jedis.close();
26    long end = System.currentTimeMillis();
27    System.out.println(end-start);
28 }
原文地址:https://www.cnblogs.com/asenyang/p/14363156.html