1.pom.xml
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commonsPool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.application.yml
spring:
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
#客户端超时时间单位是毫秒
timeout: 10000ms
jedis:
pool:
#最大连接数
max-active: 8
#最大连接阻塞等待时间 毫秒
max-wait: 10000ms
#最大空闲连接
max-idle: 200
#最小空闲连接
min-idle: 5
3.RedisConfig类
实现序列化 否则以二进制形式存储
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
//为string类型的key设置序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//为string类型的value设置序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//为hash类型的key设置序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//为hash类型的value设置序列化
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}
4.User类
public class User implements Serializable {
private Integer uid;
private String userName;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
5.五种数据类型和键操作
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Resource
private RedisTemplate<String, String> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 连接
*/
@Test
public void init(){
//二进制形式
ValueOperations ops = redisTemplate.opsForValue();
ops.set("name","str");
//正常看到
ValueOperations<String,String> valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set("lwx","mmg");
}
/**
* 序列化
*/
@Test
public void SerialTest(){
User user = new User();
user.setUid(1);
user.setUserName("lwx");
ValueOperations ops = redisTemplate.opsForValue();
ops.set("user",user);
}
@Test
public void testString(){
ValueOperations ops = redisTemplate.opsForValue();
ops.set("str","string");
String str = (String) ops.get("str");
System.out.println(str);
//层级
ops.set("user:01","list1");
//添加多条
Map<String,String> map = new HashMap<>();
map.put("str1","string");
map.put("str2","string");
ops.multiSet(map);
//获取多条
List<String> keys = new ArrayList<>();
keys.add("str");
keys.add("str1");
keys.add("str2");
List list = ops.multiGet(keys);
list.forEach(System.out::println);
redisTemplate.delete("str1");
}
@Test
public void hashTest(){
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("map","name","lwx");
hashOperations.get("map","name");
//多条
Map<String,String> map = new HashMap<>();
map.put("age","20");
map.put("address","sd");
hashOperations.putAll("map",map);
//获取多条
List<String> keys = new ArrayList<>();
keys.add("name");
keys.add("age");
keys.add("address");
List list = hashOperations.multiGet("map",keys);
list.forEach(System.out::println);
//获取所有
Map<String,String> m = hashOperations.entries("map");
m.entrySet().forEach(e ->{
System.out.println(e.getKey() + " " + e.getValue());
});
hashOperations.delete("map","name","address");
}
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPush("list","1");
listOperations.leftPush("list","2");
listOperations.rightPush("list","3");
List list = listOperations.range("list",0,3);
list.forEach(System.out::println);
System.out.println(listOperations.size("list"));
listOperations.remove("list",1,"1");
}
@Test
public void setTest(){
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set","a","b");
//获取
Set set = setOperations.members("set");
set.forEach(System.out::println);
setOperations.remove("set","a");
}
@Test
public void SortedSetTest(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
ZSetOperations.TypedTuple<Object> objectTypedTuple = new DefaultTypedTuple<>(
"lwx",6D);
ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>(
"lwx1",7D);
ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>(
"lwx2",5D);
Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();
tuples.add(objectTypedTuple);
tuples.add(objectTypedTuple1);
tuples.add(objectTypedTuple2);
zSetOperations.add("score",tuples);
Set set = zSetOperations.range("score",0,5);
set.forEach(System.out::println);
}
@Test
public void keyTest(){
Set keys = redisTemplate.keys("*");
ValueOperations ops = redisTemplate.opsForValue();
ops.set("code","test",20, TimeUnit.SECONDS);
redisTemplate.getExpire("code");
//给已经存在的key设置失效时间
redisTemplate.expire("address",20,TimeUnit.SECONDS);
}
}
标签:map,set,ops,Spring,redis,使用,new,public,redisTemplate
From: https://www.cnblogs.com/lwx11111/p/16932586.html