一、java连接单机redis🍉
1.创建一个普通的maven工程🥝
2.引入依赖🥝
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>
3.测试🥝
使用其他命令方法
代码:
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class aaa {
public static void main(String[] args) {
//把所有关于对redis的操作都封装到一个类中jedis
Jedis jedis = new Jedis("192.168.179.129",6379);
//测试连通性
String ping = jedis.ping();
System.out.println(ping);
//关于字符串操作
//存入数据 setnx 设置过期时间 20L
jedis.setex("k1",20L,"阿娇");
//获取数据
String k1 = jedis.get("k1");
System.out.println(k1);
//存入数据先判断是否存在不存在则存入存入则不存入 setnx
Long setnx = jedis.setnx("k2", "aj");
System.out.println(setnx);
//获取所有key的值
Set<String> keys = jedis.keys("*");
System.out.println(keys);
//删除数据del
Long del = jedis.del("k1", "k2", "k3");
System.out.println("删除的个数"+del);
//关于hash的操作
//存入数据hset
jedis.hset("k4","name","阿娇");
//获取数据hget
String hget = jedis.hget("k4", "name");
System.out.println(hget);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name","阿娇");
map.put("age","18");
map.put("sex","女");
//存入map类型的数据
jedis.hset("k5",map);
//获取指定key的所有value数据
Map<String, String> k5 = jedis.hgetAll("k5");
System.out.println(k5.get("name"));
}
}
特点: Jedis把对redis的操作都封装到Jedis类对象中了,而每个命令封装了一个对应的方法。
二、java通过连接池连接redis🍉
连接池的作用: 减少频繁创建和销毁连接对象。
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
public class Bbb {
public static void main(String[] args) {
//配置链接对象信息
JedisPoolConfig poolConfig = new JedisPoolConfig();
//设置最小的空闲数
poolConfig.setMinIdle(5);
//当没人连接redis服务器时,该池子中最多能空闲几个连接对象
poolConfig.setMaxIdle(10);
//当没有人连接redis服务时,最大能生成多少个连接对象
poolConfig.setMaxTotal(15);
//最大等待时间
poolConfig.setMaxWait(Duration.ofSeconds(10));
//创建连接池对象
JedisPool jedisPool = new JedisPool(poolConfig, "192.168.179.129", 6379);
long start = System.currentTimeMillis();
//从连接池获取连接对象
for (int i=0;i<1000;i++){
Jedis jedis = jedisPool.getResource();
jedis.ping();
//把给对象还给连接池
jedis.close();
}
long l = System.currentTimeMillis();
System.out.println("耗时:"+(l-start));
}
}
三、 java连接redis集群🍉
在这里插入图片描述
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
public class Ccc {
public static void main(String[] args) {
//必须开启集群模式 连接所有端口
HostAndPort hostAndPort1 = new HostAndPort("192.168.179.129", 7001);
HostAndPort hostAndPort2 = new HostAndPort("192.168.179.129", 7002);
HostAndPort hostAndPort3 = new HostAndPort("192.168.179.129", 7003);
HostAndPort hostAndPort4 = new HostAndPort("192.168.179.129", 7004);
HostAndPort hostAndPort5 = new HostAndPort("192.168.179.129", 7005);
HostAndPort hostAndPort6 = new HostAndPort("192.168.179.129", 7006);
HashSet<HostAndPort> sets = new HashSet<>();
sets.add(hostAndPort1);
sets.add(hostAndPort2);
sets.add(hostAndPort3);
sets.add(hostAndPort4);
sets.add(hostAndPort5);
sets.add(hostAndPort6);
JedisCluster jedisCluster = new JedisCluster(sets);
jedisCluster.set("k1","v1");
}
}
四、 SpringBoot整合Redis🍉
springboot对redis的操作封装到模板类中RedisTemplate和StringRedisTemplate。StringRedisTemplate是Redistemplate的子类,它只能往redis中存放字符串类型。
1.创建Springboot工程🥝
2.引入依赖🥝
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redis-02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-02</name>
<description>redis-02</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.配置信息🥝
#redis的配置信息--单机
spring.redis.host=192.168.179.129
spring.redis.port=6379
4.测试🥝
package com.lzq;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ClusterOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.Map;
import java.util.Set;
@SpringBootTest
class Redis02ApplicationTests {
//springboot创建好该类对象 并交于IOC容器管理
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
//操作key
Boolean k2 = redisTemplate.hasKey("k2");
System.out.println("判断k2是否存在:"+k2);
Set<String> keys = redisTemplate.keys("*");
System.out.println("所有的key:"+keys);
Boolean k5 = redisTemplate.delete("k5");
System.out.println("是否删除成功:"+k5);
//字符串的操作----redisTemplate类中对于每一种数据类型的操作,单独封装了方法
ValueOperations<String, String> aaa = redisTemplate.opsForValue();
aaa.set("k1","阿娇");
String k1 = aaa.get("k1");
System.out.println("获取k1对应的的value值:"+k1);
//setnx判断是否存在 不存在则存入
aaa.setIfAbsent("k1","小芳");
aaa.setIfAbsent("k2","小希");
System.out.println(aaa.get("k1"));
System.out.println(aaa.get("k2"));
//hash 类型操作
HashOperations<String, Object, Object> bbb = redisTemplate.opsForHash();
bbb.put("k3","name","阿娇");
bbb.put("k3","age","18");
Object o = bbb.get("k3", "name");
System.out.println("name:"+o);
Map<Object, Object> k3 = bbb.entries("k3");
System.out.println(k3);
}
}