application.properties4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.1 com.ccit springbootredis 0.0.1-SNAPSHOT springbootredis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-redis org.projectlombok lombok true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
spring.redis.host=127.0.0.1 spring.redis.port=6379redis配置类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class CommonConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate redisTemplate(){
RedisTemplate redisTemplate=new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
@Bean
public StringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringRedisTemplate;
}
}
操作字符串
操作代码
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisTemplateTest() throws JsonProcessingException {
//操作字符串
log.info("开始操作redis字符串");
final String content="RedisTemplate实战";
final String key="redis:3.12.1";
ValueOperations valueOperations=redisTemplate.opsForValue();
//存入redis数据库
valueOperations.set(key,content);
log.info("存入Redis键值对为:key"+key+"value:"+content);
//取数据
String getValue= (String) valueOperations.get(key);
log.info("取出数据为:"+getValue);
}
}
运行截图
操作pojo
创建User
package com.ccit.springbootredis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private Long id;
private String name;
private int age;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", password='" + password + ''' +
'}';
}
}
操作代码
package com.ccit.springbootredis;
import com.ccit.springbootredis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisTemplateTest() throws JsonProcessingException {
//操作Java类
log.info("开始操作java类");
User user=new User( 1L,"一棵小白杨",22,"password");
ValueOperations valueOperations=redisTemplate.opsForValue();
final String key="redis:3.12.2";
final String content=objectMapper.writevalueAsString(user);
valueOperations.set(key,content);
Object getValue=valueOperations.get(key);
if(getValue!=null){
User resultUser=objectMapper.readValue(getValue.toString(),User.class);
log.info("读取到key:"+key+"的内容为:");
log.info(resultUser.toString());
}
}
}
操作列表
操作代码
package com.ccit.springbootredis;
import com.ccit.springbootredis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
// 列表操作 常用场景 排行榜 近期访问数据
@Test
void redisListTest() throws JsonProcessingException {
List userList=new ArrayList<>();
userList.add(new User(1L,"张三",22,"123"));
userList.add(new User(2L,"李四",22,"123"));
userList.add(new User(3L,"王五",22,"123"));
final String key="redis:3.20";
ListOperations listOperations=redisTemplate.opsForList();
for(User user:userList){
String content=objectMapper.writevalueAsString(user);
listOperations.leftPush(key,content);
}
Object getValue=listOperations.rightPop(key);
while(getValue!=null){
log.info("当前数据为{}",objectMapper.readValue(getValue.toString(),User.class));
getValue=listOperations.rightPop(key);
}
}
}
运行截图
操作set
操作代码
package com.ccit.springbootredis;
import com.ccit.springbootredis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisSetTest(){
List userListString=new ArrayList<>();
userListString.add("debug");
userListString.add("info");
userListString.add("狗");
userListString.add("毛");
userListString.add("狗");
userListString.add("毛");
userListString.add("狗");
userListString.add("毛");
userListString.add("猫");
userListString.add("猫");
userListString.add("猫");
log.info("待处理的用户{}",userListString);
final String key="redis:3.21";
SetOperations setOperations=redisTemplate.opsForSet();
for(String str:userListString){
setOperations.add(key,str);
}
//从缓存中获取用户
Object getValue=setOperations.pop(key);
while (getValue!=null){
log.info("从缓存中获取用户集合-当前用户:{}" ,getValue);
getValue=setOperations.pop(key);
}
}
}
运行截图
操作zset
创建 PhoneUser
package com.ccit.springbootredis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class PhoneUser {
private String phone;
private Double fare;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if(o==null||getClass()!=o.getClass()) {
return false;
}
PhoneUser phoneUser=(PhoneUser)o;
return phone!=null?phone.equals(phoneUser.getPhone()):phoneUser.phone==null;
}
@Override
public int hashCode() {
return phone != null?phone.hashCode(): 0;
}
}
操作代码
package com.ccit.springbootredis;
import com.ccit.springbootredis.pojo.PhoneUser;
import com.ccit.springbootredis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisZSet() throws JsonProcessingException {
List phoneUserList=new ArrayList<>();
phoneUserList.add(new PhoneUser("103",130.0));
phoneUserList.add(new PhoneUser("101",120.0));
phoneUserList.add(new PhoneUser("102",80.0));
phoneUserList.add(new PhoneUser("105",70.0));
phoneUserList.add(new PhoneUser("106",50.0));
phoneUserList.add(new PhoneUser("104",150.0));
log.info("构造模拟数据:{}",phoneUserList);
final String key="redis:3.21";
log.info("向redis插入数据是先删除key");
redisTemplate.delete(key);
ZSetOperations zSetOperations=redisTemplate.opsForZSet();
for(PhoneUser phoneUser:phoneUserList){
String content=objectMapper.writevalueAsString(phoneUser);
zSetOperations.add(key, content,phoneUser.getFare());
}
Long size=zSetOperations.size(key);
//从小到大
//Set resSet=zSetOperations.range(key,0L,size);
//从大到小
Set resSet=zSetOperations.reverseRange(key,0L,size);
Set phoneUserSet=new HashSet();
for(String s:resSet){
log.info("从缓存中读取手机充值记录排序列表:{}",objectMapper.readValue(s,PhoneUser.class));
}
}
}
运行如图
操作hash
创建Student
package com.ccit.springbootredis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private String id;
private String userName;
}
创建 Fruit
package com.ccit.springbootredis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Fruit {
private String name;
private String color;
}
操作代码
package com.ccit.springbootredis;
import com.ccit.springbootredis.pojo.Fruit;
import com.ccit.springbootredis.pojo.PhoneUser;
import com.ccit.springbootredis.pojo.Student;
import com.ccit.springbootredis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import java.util.*;
@SpringBootTest
class SpringbootredisApplicationTests {
private final static Logger log= LoggerFactory.getLogger(SpringbootredisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisHashTest() throws JsonProcessingException {
//构造学生和水果列表
List studentList=new ArrayList<>();
List fruitList=new ArrayList<>();
studentList.add(new Student("1","张三"));
studentList.add(new Student("2","李四"));
studentList.add(new Student("3","王五"));
fruitList.add(new Fruit("红富士苹果","红色"));
fruitList.add(new Fruit("大鸭梨","黄色"));
final String sKey="redis:3.22";
final String fKey="redis:3.23";
HashOperations hashOperations=redisTemplate.opsForHash();
for(Student student:studentList){
hashOperations.put(sKey,student.getId(),objectMapper.writevalueAsString(student));
}
for(Fruit fruit:fruitList){
hashOperations.put(fKey,fruit.getName(),objectMapper.writevalueAsString(fruit));
}
Map sMap=hashOperations.entries(sKey);
Map studentMap=new HashMap<>();
for(Map.Entry m:sMap.entrySet()){
studentMap.put(m.getKey(),objectMapper.readValue(m.getValue(),Student.class));
}
log.info("学生列表为{}",studentMap);
}
}
运行截图



