1、pom.xml文件里面增加如下依赖:
org.springframework.boot
spring-boot-starter-data-redis
2、yml文件增加如下配置:
redis:
host: localhost
port: 6379
password: heming
database: 10
配置结构如下图所示:
3、新增RedisConfiguration.java类
import lombok.extern.slf4j.Slf4j;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//创建Template
RedisTemplate redisTemplate = new RedisTemplate<>();
//设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//key和hashKey采用string序列化方式
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
//value和hashValue采用Json序列化方式
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
}
4、新增RedisTestController.java类
import com.hmblogs.backend.entity.Girl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTest")
public void getGirlList(){
log.info("bbbbb");
redisTemplate.opsForValue().set("name","小明"); // 设置k v
String city = (String) redisTemplate.opsForValue().get("name"); // 拿到key为name的值
log.info(city);
}
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestJson")
public void redisTestJson(){
log.info("ccccc");
Girl girlData = new Girl();
girlData.setId(1);
girlData.setCupSize("xxxx");
girlData.setAge(12);
redisTemplate.opsForValue().set("user:100",girlData);
Girl girl = (Girl) redisTemplate.opsForValue().get("user:100");
log.info(girl.toString());
}
}
4、验证
浏览器直接访问GET接口
http://localhost:8081/redisTest
然后查看数据
发现没有乱码。
访问GET接口http://localhost:8081/redisTestJson
5、操作哈希类型数据
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestHash")
public void redisTestHash(){
log.info("ddddd");
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("100","name","tom"); // 设置k为100,哈希k为name v为100
hashOperations.put("100","age","20");
String name = (String) hashOperations.get("100", "name"); // 获取k为100,hashk为name的v
log.info(name);
Set keys = hashOperations.keys("100"); // 获取k为100的集合k
log.info(keys.toString());
List values = hashOperations.values("100"); // 获取k为100的集合v
log.info(values.toString());
hashOperations.delete("100","age"); // 删除age字段
}
访问http://localhost:8081/redisTestHash
6、操作列表类型数据
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestList")
public void redisTestList(){
log.info("eeeee");
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("mylist","a","b","c"); // 往k为mylist插入a,b,c
log.info(listOperations.range("mylist", 0, -1).toString());
listOperations.leftPush("mylist","d"); // 插入d
List mylist = listOperations.range("mylist", 0, -1);
log.info(mylist.toString()); // 输出
listOperations.rightPop("mylist"); // 删除
Long size = listOperations.size("mylist");
log.info(size.toString());
log.info(listOperations.range("mylist", 0, -1).toString());
}
访问如下接口:http://localhost:8081/redisTestList
服务端console打印如下:
rpop的时候,移除了最先放入的a,规则就是先进先出。
数据如下图所示:
7、操作集合类型数据
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestSet")
public void redisTestSet(){
log.info("fffff");
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d"); // 往set1 插入abcd
setOperations.add("set2","a","b","x","y"); // 往set2 插入abxy
Set members1 = setOperations.members("set1");
log.info("set1:"+members1); // 输出
Long size = setOperations.size("set1");
log.info(size.toString()); // 输出
Set members2 = setOperations.members("set2");
log.info("set2:"+members2); // 输出
Set intersect = setOperations.intersect("set1", "set2"); // 他们的交集
log.info("inter:"+intersect);
Set union = setOperations.union("set1", "set2");// 他们的并集
log.info("union:"+union);
setOperations.remove("set1","a","b"); // 在set1里面删除ab
}
查看服务端的console打印如下:
查看set1的数据,如下图所示:
查看set2的数据,如下图所示:
8、操作有序集合类型数据
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestZSet")
public void redisTestZSet(){
log.info("ggggg");
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1","a",10); // 设置k为zset1v为a,分数为10
zSetOperations.add("zset1","b",12); //同理
zSetOperations.add("zset1","c",9); // 同理
Set zset1 = zSetOperations.range("zset1", 0, -1);
log.info(zset1.toString()); // 输出
zSetOperations.incrementScore("zset1","c",10); // 为k为zset1v为c的加10分
Set zsetAfter = zSetOperations.range("zset1", 0, -1);
log.info(zsetAfter.toString()); // 输出
zSetOperations.remove("zset1","a","b"); // 删除ab
Set zsetDel = zSetOperations.range("zset1", 0, -1);
log.info(zsetDel.toString()); // 输出
}
查看console打印如下:
查看数据,如下图所示:
9、通用命令操作
/**
* redis test
* @return
*/
@GetMapping(value = "/redisTestCommon")
public void redisTestCommon(){
log.info("hhhhh");
//keys exists type del
Set keys = redisTemplate.keys("*"); // 查找所有符合给定模式( pattern)的 key
log.info(keys.toString());
Boolean name = redisTemplate.hasKey("name");
log.info(name.toString());
Boolean set1 = redisTemplate.hasKey("set1");
log.info(set1.toString());
for (Object key : keys) {
DataType type = redisTemplate.type(key); // 查找他们的类型是什么
log.info(type.name());
}
redisTemplate.delete("mylist"); // 删除mylist
log.info("删除后:");
for (Object key : keys) {
DataType type = redisTemplate.type(key); // 查找他们的类型是什么
log.info(type.name());
}
}
查看console打印如下内容:
mylist被删掉了,截图如下:
Spring Data Redis中提供了一个高度封装的类:RedisTemplate,对相关api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下:
ValueOperations:string数据操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:hash类型的数据操作
ListOperations:list类型的数据操作