提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、idea新建springboot项目,勾选4个依赖项
注意:在applicaiton.properties配置文件中配置
Redis服务器地址
spring.redis.host=192.168.12.59
二、项目结构
三、String操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class StringDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key1 = "tea#01";
private final String key2 = "tea#02";
public void test() {
redisTemplate.opsForValue().set(key1, "张三");
String s = redisTemplate.opsForValue().get(key1);
System.out.println(s);
}
public void test2() throws InterruptedException {
redisTemplate.opsForValue().set(key2, "李四", 30, TimeUnit.SECONDS);
String s = redisTemplate.opsForValue().get(key2);
System.out.println("第一次获取值:" + s);
Thread.sleep(31 * 1000);
String s2 = redisTemplate.opsForValue().get(key2);
System.out.println("第二次获取值:" + s2);
}
}
单元测试类:
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class StringDemoTest {
@Autowired
private StringDemo stringDemo;
@Test
void test1() {
stringDemo.test();
}
@Test
void test2() throws InterruptedException {
stringDemo.test2();
}
}
四、Set操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class SetDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key5 = "tea#04";
private final String key6 = "tea#05";
public void test() {
redisTemplate.opsForSet().add(key5,"语文");
redisTemplate.opsForSet().add(key5,"数学");
redisTemplate.opsForSet().add(key5,"英语");
redisTemplate.opsForSet().add(key6,"语文");
redisTemplate.opsForSet().add(key6,"英语");
Set<String> intersect = redisTemplate.opsForSet().intersect(key5, key6);
System.out.println(intersect);
}
}
单元测试类:
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class SetDemoTest {
@Autowired
private SetDemo setDemo;
@Test
void test1() {
setDemo.test();
}
}
五、 Hash操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class HashDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key3 ="tea#03";
public void test(){
redisTemplate.opsForHash().put(key3,"家庭住址","开封");
redisTemplate.opsForHash().put(key3,"大学","开封大学");
List<Object> values = redisTemplate.opsForHash().values(key3);
for (Object value:values){
System.out.println(value);
}
}
}
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class HashDemoTest {
@Autowired
private HashDemo hashDemo;
@Test
void test1() {
hashDemo.test();
}
}
六、List操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class ListDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key1="tea#07";
public void test(){
redisTemplate.opsForList().leftPush(key1,"张三");
redisTemplate.opsForList().leftPush(key1,"李四");
redisTemplate.opsForList().leftPush(key1,"王五");
Object o = redisTemplate.opsForList().rightPop(key1);
System.out.println(o);
}
}
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class ListDemoTest {
@Autowired
private ListDemo listDemo;
@Test
void test1() {
listDemo.test();
}
}
七、ZSet操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class ZSetDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key6 = "tea#06";
public void test() {
redisTemplate.opsForZSet().add(key6, "语文", 88);
redisTemplate.opsForZSet().add(key6, "数学", 99);
redisTemplate.opsForZSet().add(key6, "英语", 66);
ZSetOperations.TypedTuple<String> stringTypedTuple = redisTemplate.opsForZSet().popMax(key6);
System.out.println("成绩最高的科目:"+stringTypedTuple.getValue());
System.out.println("成绩最高的科目:"+stringTypedTuple.getScore());
}
}
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class ZSetDemoTest {
@Autowired
private ZSetDemo zsetDemo;
@Test
void test1() {
zsetDemo.test();
}
}
八、 BitMap操作
package cn.hncj.springbootredis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@Component
public class BitDemo {
@Autowired
private StringRedisTemplate redisTemplate;
private final String key3 ="tea#08";
public void test(){
redisTemplate.opsForValue().setBit(key3,2,true);
redisTemplate.opsForValue().setBit(key3,144,true);
RedisCallback<Long> callback=connection -> {
return connection.bitCount(key3.getBytes(),0,19);
};
Long totalCount = redisTemplate.execute(callback);
System.out.println(totalCount);
}
}
package cn.hncj.springbootredis.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created on 2022/5/26.
*
* @author Hou chaof
*/
@SpringBootTest
class BitMapDemoTest {
@Autowired
private BitDemo bitDemo;
@Test
void test1() {
bitDemo.test();
}
}