资源相关
相关代码示例
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
spring:
application:
name: boot-redis
redis:
sentinel:
nodes: 192.168.44.121:26379
master: hs6379
cluster:
nodes: 192.168.44.121:6380,192.168.44.121:6379,192.168.44.121:6381,192.168.44.121:6382
database: 0
timeout: 6000
jedis:
pool:
max-active: 1000
max-wait: -1ms
max-idle: 50
min-idle: 0
package com.hf.redis.config;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Copyright (C), 2016-2019 hf
* @FileName: RedisTempleConfig
* @Author: hf
* @Date: 2019/9/29 2:41
* @Description: RedisTemple 模板配置
*/
@Configuration
public class IsRedisTempleConfig {
//设置序列化规则
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//key使用StringRedisSerializer序列化 value使用jackson2JsonRedisSerializer序列化
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// Hash结构设置
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
@Autowired
RedisTemplate<Object, Object> redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void test_set_06() {
redisTemplate.opsForSet().add("set01", "111", "222", "333", "444", "555", "666");
Set<Object> set01 = redisTemplate.opsForSet().members("set01");
System.out.println(set01);
}
@Test
public void test_hash_04() {
redisTemplate.boundHashOps("userList").put("user_k1", new User(1, "测试用户", "nopwd", "noEmail"));
Object obj = redisTemplate.boundHashOps("userList").get("user_k1");
System.out.println(obj);
}
@Test
public void test_obj_03() {
User user = new User(1, "刘一", "liuyi", "liuyi@163.com");
redisTemplate.opsForValue().set("user-01", user);
Object obj = redisTemplate.opsForValue().get("user-01");
System.out.println(obj);
}
@Test
public void test_list_02() {
stringRedisTemplate.opsForList().leftPushAll("list01", Arrays.asList("11111", "22222", "33333", "44444"));
List<String> list01 = stringRedisTemplate.opsForList().range("list01", 0, -1);
System.out.println("Redis中获取list列表的值:" + list01);
}
@Test
public void test_str_01() {
stringRedisTemplate.opsForValue().set("k2", "我是k2的值");
stringRedisTemplate.opsForValue().set("k1", "Hello", 20, TimeUnit.SECONDS);
String str1 = stringRedisTemplate.opsForValue().get("k1");
String str2 = stringRedisTemplate.opsForValue().get("k2");
System.out.println("Redis获取到k1值:" + str1);
System.out.println("Redis获取到k2值:" + str2);
}