SpringBoot2整合Redis
Posted 虎老狮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot2整合Redis相关的知识,希望对你有一定的参考价值。
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml
spring:
redis:
host: 192.168.16.128
port: 6379
# 下面这些可以不加
jedis:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
测试类(测试需要关闭Linux的防火墙)
-
StringRedisTemplate采用String的序列化策略;RedisTemplate采用JDK的序列化策略。
-
如果redis里存字符串使用StringRedisTemplate即可。
-
如果redis里存储对象类型,而取出时又不想做数据转换,建议使用RedisTemplate。
StringRedisTemplate
package com.ah.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StringRedisTemplateTests {
@Autowired
StringRedisTemplate redis;
@Test
public void contextLoads() {
// 测试字符串(opsForValue)
ValueOperations<String, String> strKV = redis.opsForValue();
strKV.set("name", "Dog" + new java.util.Date().getTime());
System.out.println(strKV.get("name"));
// 测试Hash结构的数据(boundHashOps)
BoundHashOperations<String, Object, Object> hash = redis.boundHashOps("book");
hash.put("name", "西游记");
hash.put("author", "吴承恩");
System.out.println(hash.get("name"));
// 直接获取Hash数据的键值对
Map<Object, Object> entries = hash.entries();
System.out.println("entries=" + entries);
}
}
RedisTemplate(操作对象)
package com.ah.redis;
import org.springframework.cache.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableCaching
public class RedisTemplateConf extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerializer来序列化/反序列化redis的value值
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL,
com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// value
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// 使用StringRedisSerializer来序列化/反序列化redis的key值
RedisSerializer<?> redisSerializer = new StringRedisSerializer();
// key
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
package com.ah.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTemplateTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
public void test() {
// 删(如果有,删除成功)
Boolean delete = redisTemplate.delete("user");
System.out.println("delete:" + delete);
// 增
String key = "user";
User value = new User("八戒");
redisTemplate.opsForValue().set(key, value);
// 查
User user = (User) redisTemplate.opsForValue().get("user");
System.out.println(user);
}
}
class User {
private String name;
public User() {// 必须
}
public User(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
以上是关于SpringBoot2整合Redis的主要内容,如果未能解决你的问题,请参考以下文章