springboot 1.x默认使用的是jedis,到了springboot 2.x默认使用的是Lettuce。
jedis与Lettuce的区别
Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。
Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的, 所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
导入依赖
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在yml中配置一下redis服务器的配置
server:
port: 8086
spring:
redis:
host: 127.0.0.1
port: 6379
# 密码 没有则可以不填
#password: 123456789
# 如果使用的jedis 则将lettuce改成jedis即可
lettuce:
pool:
# 最大活跃链接数 默认8
max-active: 8
# 最大空闲连接数 默认8
max-idle: 8
# 最小空闲连接数 默认0
min-idle: 0
pojo实体类
public class User implements Serializable {
private static final long serialVersionUID = 1222221L;
private Long id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 年龄
*/
private Integer age;
/**
* 性别 1=男 2=女 其他=保密
*/
private Integer sex;
省略get和set
redis配置类RedisConfig
注意:在实例化RedisTemplate可以不用指定其序列化和反序列化的类型,默认是JdkSerializationRedisSerializer
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
/**
* 配置自定义redisTemplate
* @return
*/
@Bean
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
写一个简单的RedisUtils
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 写入缓存
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新缓存
*/
public boolean getAndSet(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
测试一下
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
private static final Logger logger = LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);
@Autowired
private RedisTemplate redisTemplate;
@Test
public void contextLoads() {
}
@Resource
private RedisUtils redisUtils;
@Test
public void redisTest(){
//redis存储数据
String key = "name";
redisTemplate.opsForValue().set(key,"sn");
//redis获取数据
String value = (String)redisTemplate.opsForValue().get(key);
logger.info("获取缓存中的key为:{},value为:{}",key,value);
User user = new User();
user.setUsername("sndada");
user.setId(1L);
user.setSex(1);
user.setPassword("123456");
user.setAge(18);
String userKey = "sn";
redisTemplate.opsForValue().set(userKey,user);
Object userValue = redisTemplate.opsForValue().get(userKey);
logger.info("获取缓存中的key为:{},value为:{}",userKey,userValue);
}
//使用redisUtil
/**
* 插入缓存数据
*/
@Test
public void set() {
redisUtils.set("redis_key", "redis_vale");
}
/**
* 读取缓存数据
*/
@Test
public void get() {
Object value = redisUtils.get("redis_key");
System.out.println(value);
}
}
使用redis可视化工具redis desktop manager查看缓存中的结果
下列的就是Redis其它类型所对应的操作方式
opsForValue: 对应 String(字符串)
opsForZSet: 对应 ZSet(有序集合)
opsForHash: 对应 Hash(哈希)
opsForList: 对应 List(列表)
opsForSet: 对应 Set(集合)
本文主要是用来写笔记,好日后回顾。
部分内容来自空佬的博客:
https://www.jianshu.com/p/feef1421ab0b?utm_source=oschina-app