博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起 学习!一起加油!
redis-cli
shutdwn
exit
String类型的命令
set key value 设置数据,如果key存在就覆盖
get key 获取key对应的值
hash类型的命令
hset key filed value 设置filed字段的value
hget key filed 获取filed字段的value
nil 表示为空
clear 清屏
key* 获取所有的key
⭐⭐⭐注意:导入相关依赖,勾上之后,springboot会自动导入所需的依赖。
redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
测试:
package com.jkj;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class Springboot10RedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void set(){
ValueOperations ops = redisTemplate.opsForValue();
ops.set("name","rc");
}
@Test
void get() {
ValueOperations ops = redisTemplate.opsForValue();
Object value = ops.get("name");
System.out.println(value);
}
}
package com.jkj;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class Springboot10RedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void hset(){
HashOperations hash = redisTemplate.opsForHash();
hash.put("person","name","xfx");
hash.put("person","name1","xmg");
}
@Test
void hget() {
HashOperations hash = redisTemplate.opsForHash();
Object o = hash.get("person", "name");
System.out.println(o);
}
}
StringRedisTemplate以字符串作为key和value
测试:
package com.jkj;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
public class StringRedisApplication {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void get(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String name = ops.get("name");
System.out.println(name);
}
}
验证是否等效
<dependency>
<groupId>redis.clients</ groupId>
<artifactId>jedis</artifactId>
</dependency>
spring:
redis:
host: localhost
port: 6379
client-type: jedis
spring:
redis:
host: localhost
port: 6379
client-type: lettucelettuce:
pool:
max-active: 16
jedis:
pool:
max-active: 16