Redis入门案例
1.导入jar包
<!--spring整合redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
1.2客户端操作String类型
package com.jt.test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
public class TestRedis {
@Test
public void test01() throws InterruptedException {
Jedis jedis = new Jedis("192.168.126.129",6379);
jedis.set("a", "动态获取redis中的数据");
System.out.println(jedis.get("a"));
if(jedis.exists("a")){
jedis.set("a", "修改数据");
}else{
jedis.set("a", "新增数据");
}
jedis.del("a");
jedis.flushDB();
jedis.flushAll();
jedis.set("b", "设定超时时间");
jedis.expire("b", 10);
Thread.sleep(2000);
System.out.println(jedis.ttl("b"));
}
@Test
public void test02(){
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.set("c", "测试redis");
jedis.setnx("d","测试setnx方法");
System.out.println(jedis.get("d"));
jedis.setex("s", 20, "为数据添加超时111");
System.out.println("获取超时时间:"+jedis.ttl("s"));
}
@Test
public void test03(){
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll();
SetParams setParams = new SetParams();
setParams.xx().ex(20);
jedis.set("a", "测试方法",setParams);
System.out.println(jedis.get("a"));
}
}