Springboot+redis操作多种实现(以及Jedis,Redisson,Lettu。。。Springboot + redis 操作多种实现
⼀、Jedis,Redisson,Lettuce三者的区别
共同点:都提供了基于Redis操作的Java API,只是封装程度,具体实现稍有不同。
不同点:
1.1、Jedis
是Redis的Java实现的客户端。⽀持基本的数据类型如:String、Hash、List、Set、Sorted Set。
特点:使⽤阻塞的I/O,⽅法调⽤同步,程序流需要等到socket处理完I/O才能执⾏,不⽀持异步操作。Jedis客户端实例不是线程安全的,需要通过连接池来使⽤Jedis。
1.1、Redisson
优点点: 分布式锁,分布式集合,可通过Redis⽀持延迟队列。
1.3、 Lettuce
⽤于线程安全同步,异步和响应使⽤,⽀持集,Sentinel,管道和编码器。
基于Netty框架的事件驱动的通信层,其⽅法调⽤是异步的。Lettuce的API是线程安全的,所以可以操作单个Lettuce连接来完成各种操作。
⼆、Jedis
三、RedisTemplate
3.1、使⽤配置
maven配置引⼊,(要加上版本号,我这⾥是因为Parent已声明)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 192.168.1.140
port:6379
password:
database:15 # 指定redis的分库(共16个0到15)
3.2、使⽤⽰例
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public CustomersEntity findById(Integer id){
// 需要缓存
/
/ 所有涉及的缓存都需要删除,或者更新
try{
String toString = stringRedisTemplate.opsForHash().get(REDIS_CUSTOMERS_ONE, id +"").toString();
if(toString != null){
Bean(toString, CustomersEntity.class);
}
}catch(Exception e){
e.printStackTrace();
}
// 缓存为空的时候,先查,然后缓存redis
Optional<CustomersEntity> byId = customerRepo.findById(id);
if(byId.isPresent()){
CustomersEntity customersEntity = ();
try{
stringRedisTemplate.opsForHash().put(REDIS_CUSTOMERS_ONE, id +"", JsonStr(customersEntity));
}catch(Exception e){
e.printStackTrace();
}
return customersEntity;
}
return null;
redis支持的五种数据类型}
3.3、扩展
3.3.1、spring-boot-starter-data-redis的依赖包
3.3.2、stringRedisTemplate API(部分展⽰)
opsForHash --> hash操作
opsForList --> list操作
opsForSet --> set操作
opsForValue --> string操作
opsForZSet --> Zset操作
3.3.3 StringRedisTemplate默认序列化机制
public class StringRedisTemplate extends RedisTemplate<String, String> {
/**
* Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)} * and {@link #afterPropertiesSet()} still need to be called.
*/
public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
setKeySerializer(stringSerializer);
setValueSerializer(stringSerializer);
setHashKeySerializer(stringSerializer);
setHashValueSerializer(stringSerializer);
}
}
四、RedissonClient 操作⽰例
4.1 基本配置
4.1.1、Maven pom 引⼊
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
<dependency>
<groupId&disson</groupId>
<artifactId>redisson</artifactId>
<version>3.8.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId&disson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId> <version>LATEST</version>
</dependency>
4.1.2、添加配置⽂件Yaml或者json格式l
# Redisson 配置
singleServerConfig:
address:"redis://192.168.1.140:6379"
password:null
clientName:null
database:15 #选择使⽤哪个数据库0~15 idleConnectionTimeout:10000
pingTimeout:1000
connectTimeout:10000
timeout:3000
retryAttempts:3
retryInterval:1500
reconnectionTimeout:3000
failedAttempts:3
subscriptionsPerConnection:5 subscriptionConnectionMinimumIdleSize:1 subscriptionConnectionPoolSize:50 connectionMinimumIdleSize:32 connectionPoolSize:64
dnsMonitoringInterval:5000
#dnsMonitoring: false
threads:0
nettyThreads:0
codec:
class:"dec.JsonJacksonCodec" transportMode:"NIO"
或者,配置 redisson-config.json
{
"singleServerConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"reconnectionTimeout":3000,
"failedAttempts":3,
"password":null,
"subscriptionsPerConnection":5,
"clientName":null,
"address":"redis://192.168.1.140:6379",
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"connectionMinimumIdleSize":10,
"connectionPoolSize":64,
"database":0,
"dnsMonitoring":false,
"dnsMonitoringInterval":5000
},
"threads":0,
"nettyThreads":0,
"codec":null,
"useLinuxNativeEpoll":false
}
4.1.3、读取配置
新建读取配置类
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redisson() throws IOException {
// 两种读取⽅式,Config.fromYAML 和 Config.fromJSON
// Config config = Config.fromJSON(ClassLoader().getResource("redisson-config.json")); Config config = Config.fromYAML(ClassLoader().getResource("redisson-co
}
}
或者,在 l中配置如下
spring:
redis:
redisson:
config: classpath:redisson-config.yaml
4.2 使⽤⽰例
@RestController
@RequestMapping("/")
public class TeController {
@Autowired
private RedissonClient redissonClient;
static long i = 20;
static long sum = 300;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论