# SpringBoot连接Redis
# Springboot2+Jedis客户端
Jedis是一个流行的用于Java语音的Redis客户端,支持连接池管理、事务、管道、数据类型操作
# 配置
# pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
# yml
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
jedis:
pool:
min-idle: 0
max-active: 8
max-idle: 8
max-wait: -1ms
connect-timeout: 30000ms
# RedisConfig
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
// 创建redis模板
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
# RedisTemplate使用
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private RedisTemplate<String, User> redisTemplate;
@ApiOperation("add")
@PostMapping("add")
public ResponseResult<User> add(User user){
redisTemplate.opsForValue().set(String.valueOf(user.getId()),user);
return ResponseResult.success(redisTemplate.opsForValue().get(String.valueOf(user.getId())));
}
@ApiOperation("find")
@GetMapping("find/{userId}")
public ResponseResult<User> edit(@PathVariable("userId") String userId){
return ResponseResult.success(redisTemplate.opsForValue().get(userId));
}
}
# Springboot2+lettuce
lettuce是Springboot默认的redis连接方式,lettuce使用了netty框架,具备线程共享和异步支持
# 配置
# pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
# yml
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
lettuce:
pool:
mix-idle: 0
max-active: 8
min-idle: 8
max-wait: -1ms
connect-timeout: 30000ms
# RedisConfig(同Jedis)
# RedisTemplate使用(同Jedis)
# 区别
- 连接方式
- Jedis是直接的、阻塞式的连接
- Lettuce是基于Netty框架的异步、非阻塞连接
- 线程安全性
- Jedis是线程不安全的,多线程环境需要连接池管理
- Lettuce是线程安全的,可以多线程共享实例
- 性能
- Lettuce在高并发情况下比Jedis具有更好的性能
# 数据类封装
# 接口封装
封装是为了保证项目使用redis方法的一致性
public interface IRedisService<T> {
void set(String key, T value);
void set(String key, T value, long time);
T get(String key);
void delete(String key);
void delete(Collection<String> keys);
boolean expire(String key, long time);
Long getExpire(String key);
boolean hasKey(String key);
Long increment(String key, long delta);
Long decrement(String key, long delta);
void addSet(String key, T value);
Set<T> getSet(String key);
void deleteSet(String key, T value);
T execute(RedisCallback<T> redisCallback);
}
# 实现接口
@Service
public class RedisServiceImpl<T> implements IRedisService<T> {
@Resource
private RedisTemplate<String, T> redisTemplate;
@Override
public void set(String key, T value, long time) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
@Override
public void set(String key, T value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public T get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public void delete(String key) {
redisTemplate.delete(key);
}
@Override
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
@Override
public boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
@Override
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
@Override
public boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
@Override
public Long increment(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
@Override
public Long decrement(String key, long delta) {
return redisTemplate.opsForValue().increment(key, -delta);
}
@Override
public void addSet(String key, T value) {
redisTemplate.opsForSet().add(key, value);
}
@Override
public Set<T> getSet(String key) {
return redisTemplate.opsForSet().members(key);
}
@Override
public void deleteSet(String key, T value) {
redisTemplate.opsForSet().remove(key, value);
}
@Override
public T execute(RedisCallback<T> redisCallback) {
return redisTemplate.execute(redisCallback);
}
}
# 使用
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IRedisService<User> redisService;
@ApiOperation("add")
@PostMapping("add")
public ResponseResult<User> add(User user){
redisService.set(String.valueOf(user.getId()),user);
return ResponseResult.success(redisService.get(String.valueOf(user.getId())));
}
@ApiOperation("find")
@GetMapping("find/{userId}")
public ResponseResult<User> edit(@PathVariable("userId") String userId){
return ResponseResult.success(redisService.get(userId));
}
}