jedis仓库地址
jedis简单案例
jedisAPI文档
java提供了jedis客户端来连接redis,jedis提供了丰富的api,基本覆盖了redis的命令,本篇文章主要记录redis连接过程。
redis配置文件:redis.conf
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>4.3.0version>
dependency>
想用个新版本来着,但半天没下载下来,一看官网在升级……
顺着仓库找了下,下载了放本地仓库也好使
redis出于安全考虑,安全模式开启的情况下没有配置新用户和密码的时候,只能通过本地连接,无法远程
注释redis.conf中
#bind 127.0.0.1 -::1
redis默认提供了一个default用户
acl genpass
requirepass 21797d7480c1270b848a1524128671b31dcad0725762cf41cc81c21e15fa35b2
Test.java
package redis;
import redis.clients.jedis.Jedis;
/**
* Create by zjg on 2022/10/23
*/
public class Test {
public static void main(String[] args) {
String password="21797d7480c1270b848a1524128671b31dcad0725762cf41cc81c21e15fa35b2";
Jedis jedis = new Jedis("ip", 6379);
jedis.auth(password);
String ping = jedis.ping();
System.out.println("ping:"+ping);
}
}
JedisPoolUtils.java
package com.student.sys.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.PostConstruct;
/**
* Create by zjg on 2022/10/27
*/
@Configuration
public class JedisPoolUtils {
private static JedisPool jedisPool;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.user:default}")
private String user;
@Value("${spring.redis.password}")
private String password;
@PostConstruct
private void init(){
jedisPool=new JedisPool(host, port, user, password);
}
public static Jedis getResource() {
synchronized (JedisPoolUtils.class){
if(jedisPool==null){
new JedisPoolUtils().init();
}
}
return jedisPool.getResource();
}
public static void close(){
jedisPool.close();
}
}
@Test
public void redisPoolTest(){
Jedis jedis= JedisPoolUtils.getResource();
System.out.println(jedis.ping());
jedis.close();
JedisPoolUtils.close();
}
spring提供了spring-data-redis,springboot提供了启动自动配置类
spring操作redis参考
spring操作redisAPI
pom.xml
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>4.3.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
<version>2.7.0version>
dependency>
application.yml
spring :
redis:
host: 127.0.0.1
port: 6379
password: 21797d7480c1270b848a1524128671b31dcad0725762cf41cc81c21e15fa35b2
redis的配置基于RedisProperties类,提供了两种默认模板在RedisAutoConfiguration类
SpringBootTest.java
package redis;
import com.student.SpringbootStart;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashSet;
import java.util.Set;
/**
* Create by zjg on 2022/10/24
*/
@RunWith(SpringRunner.class)
@org.springframework.boot.test.context.SpringBootTest(classes = SpringbootStart.class)
public class SpringBootTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisTest(){
//string
ValueOperations<String, String> stringOperations = redisTemplate.opsForValue();
stringOperations.set("user","zhangjg");
String user = stringOperations.get("user");
System.out.println("stringSize:"+user.length());
System.out.println("string:"+ user);
//list
ListOperations listOperations = redisTemplate.opsForList();
//listOperations.leftPushAll("list",1,2);
System.out.println("listSize:"+listOperations.size("list"));
System.out.println("list:"+listOperations.range("list",0,-1));
//set
SetOperations setOperations = redisTemplate.opsForSet();
//setOperations.add("set","s1","s2","s3","s4","s5");
Long setSize = setOperations.size("set");
System.out.println("setSize:"+setSize);
System.out.println("set:"+setOperations.randomMembers("set",setSize));
//hash
HashOperations hashOperations = redisTemplate.opsForHash();
//hashOperations.put("hash","field","value");
System.out.println("hashSize:"+hashOperations.size("hash"));
System.out.println("hash:"+hashOperations.entries("hash"));
//sorted set
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
Set<ZSetOperations.TypedTuple> zset=new HashSet<>();
DefaultTypedTuple v1 = new DefaultTypedTuple("v1", 10D);
DefaultTypedTuple v2 = new DefaultTypedTuple("v2", 20D);
DefaultTypedTuple v3 = new DefaultTypedTuple("v3", 50D);
zset.add(v3);zset.add(v1);zset.add(v2);
zSetOperations.add("zset",zset);
System.out.println("zsetSize:"+zSetOperations.size("zset"));
System.out.println("zset:"+zSetOperations.range("zset",0,-1));
}
}
redis常用命令
jedis封装大量api对应redis的命令,只要redis命令学会了,jedis很容易上手;
spring对redis又进行了一层封装,提供了例如RedisTemplate的模板类,方法名不如jedis来得简单,具体可以参考aip文档来学习。