请注意,本文编写于 1090 天前,最后修改于 902 天前,其中某些信息可能已经过时。
在java中我们一般使用Jedis
连接Redis
,以下操作全部基于该依赖。
字符串存储
相关依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
package com.xn2001.jedis;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.Date;
public class Demo1 {
@Test
public void test(){
//连接redis
Jedis jedis = new Jedis("192.168.231.129");
//操作redis,redis命令是什么,方法就是什么
jedis.set("date1",new Date().toString());
String date1 = jedis.get("date1");
System.out.println(date1);
//关闭redis连接
jedis.close();
}
}
对象存储
package com.xn2001.jedis;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {
private String name;
private String age;
private Date date;
}
引入 fastjson
<dependency>
<groupId>com.rover12421</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
package com.xn2001.jedis;
import com.alibaba.fastjson.JSON;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.Date;
public class Demo1 {
private Jedis jedis;
@Before
public void connection(){
//连接redis
jedis = new Jedis("192.168.231.129");
}
@Test
public void test(){
//操作redis,redis命令是什么,方法就是什么
jedis.set("date1",new Date().toString());
String date1 = jedis.get("date1");
System.out.println(date1);
}
//存储对象,使用fastjson转化工具
@Test
public void test2(){
Student student = new Student("钟小湖", "18", new Date());
jedis.set("student1", JSON.toJSONString(student));
String student1 = jedis.get("student1");
System.out.println(student1);
System.out.println(JSON.parseObject(student1,Student.class));
}
@After
public void close(){
//关闭redis连接
jedis.close();
}
}
Jedis连接池
jedis 连接资源的创建与销毁是很消耗程序性能,jedis 为我们提供了 jedis 的池化技术,jedisPool 在创建时初始化一些连接资源存储到连接池中,使用 jedis 连接资源时不需要创建,而是从连接池中获取一个资源进行 redis 的操作,使用完毕后,不需要销毁该连接资源,而是将该资源归还给连接池,供其他请求使用。
@Test
public void test() {
//创建连接池
JedisPool jedisPool = new JedisPool("192.168.231.129");
//获取jedis对象
Jedis jedis = jedisPool.getResource();
String student1 = jedis.get("student1");
System.out.println(student1);
//释放资源
jedis.close();
}
jedis 连接池配置信息
@Test
public void test2(){
// jedis连接池配置类
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100); // 连接池中最大的活跃对象
poolConfig.setMaxIdle(10); // 最大空闲数
poolConfig.setMinIdle(5); // 最小空闲数
poolConfig.setMaxWaitMillis(3000); // 当连接池空了之后,多久没获取到Jedis对象就超时
JedisPool jedisPool = new JedisPool(poolConfig, "192.168.231.129");
}
Redis的管道操作
因为在操作 Redis 的时候,执行一个命令需要先发送请求到 Redis 服务器,这个过程需要经历网络的延迟,Redis 还需要给客户端一个响应。
如果需要一次性执行很多个命令,上述的方式效率非常非常低,于是我们可以通过 Redis 的管道,先将命令全部放到客户端的一个 Pipelin e中,之后一次性执行,同时 Redis 服务端也给将结果一次性全部返回给客户端。
我们先来看一下不使用管道操作的话,增加 100000 消耗了多少时间。
@Test
public void test1() {
String s1 = formatter.format(LocalDateTime.now());
System.out.println("开始时间:" + s1);
JedisPool jedisPool = new JedisPool("192.168.231.129");
Jedis jedis = jedisPool.getResource();
for (int i = 0; i < 100000; i++) {
jedis.incr("number1");
}
jedis.close();
String s2 = formatter.format(LocalDateTime.now());
System.out.println("不使用管道操作,完成时间:" + s2);
}
开始时间:2020-10-08 15:59:07
不使用管道操作,完成时间:2020-10-08 15:59:57
接下来我们用管道操作去测试。
@Test
public void test2(){
String s1 = formatter.format(LocalDateTime.now());
System.out.println("开始时间:" + s1);
JedisPool jedisPool = new JedisPool("192.168.231.129");
Jedis jedis = jedisPool.getResource();
//创建管道
Pipeline pipelined = jedis.pipelined();
//将命令放入管道
for (int i = 0; i < 100000; i++) {
pipelined.incr("number2");
}
//执行命令
pipelined.syncAndReturnAll();
jedis.close();
String s2 = formatter.format(LocalDateTime.now());
System.out.println("不使用管道操作,完成时间:" + s2);
}
开始时间:2020-10-08 16:05:07
不使用管道操作,完成时间:2020-10-08 16:05:07
可以看到操作几乎是一瞬间就完成的。哪怕再加一个0,也仅仅用不到2秒的时间。
连接集群
不需要关注close以及连接池等问题。
@Test
public void test1(){
HashSet<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.231.129",7001));
nodes.add(new HostAndPort("192.168.231.129",7002));
nodes.add(new HostAndPort("192.168.231.129",7003));
nodes.add(new HostAndPort("192.168.231.129",7004));
nodes.add(new HostAndPort("192.168.231.129",7005));
nodes.add(new HostAndPort("192.168.231.129",7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
String a = jedisCluster.get("a");
String b = jedisCluster.get("b");
String c = jedisCluster.get("c");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
版权属于:乐心湖's Blog
本文链接:https://xn2001.com/archives/597.html
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!