lettuce的用法,lettuce和jedis区别

另一方面,redis客户端比较1 )、Jedis Jedis作为redis官方推荐的客户端之一,简单易用,基础功能齐全,在中小项目中还是很容易使用的,但Jedis是直连模式,有多个

为什么不安全? Redis是单线程的,不是世界上最安全的吗?

官员请不要激动。 这里的不安并不是说Redis处理数据不安全,而是说Jedis向Redis服务器提交数据或从Redis中提取数据不安全。 快速阅读Jedis源代码时,在单个Jedis实例中将数据推送到Redis的写入流RedisOutputStream )和从Redis获取数据的读取流read stream )都是全局的很明显,如果在多个线程中同时使用该Jedis实例,也就是同时操作Redis的写入流和读取流,就会发生数据不可描述的奇妙融合

不能使用-Jedis吗?

不,多个线程使用一个实例时会出现问题。 那就避免吧。 为每个线程分配一个Jedis实例,让他们处理自己的数据。 通常使用JedisPoll线程池实现。

2 )、Lettuce Lettuce也是Redis官方推荐的客户端,是基于netty和Reactor的可扩展线程安全Redis客户端。 Lettuce提供了同步、异步和响应API以与Redis进行交互。 与Jedis不同,Lettuce实例是线程安全的。 这意味着多个线程可以共享一个Lettuce实例并与Redis服务端交互。 当然,Lettuce还支持通过添加Lettuce实例来满足项目的需要。

您是否优先使用-LetTuce?

是的,通常在springboot中引入的Redis支持中依赖的是Lettuce。

3 )、Redisson Redisson也是Redis官方推荐的客户端。 与前面的两种客户端相比,最后一种客户端一定有些人不太清楚。

引用官方网站简要介绍Redisson

Redisson采用基于NIO的Netty框架,不仅可以作为Redis的基础驱动器客户端,还可以连接到各种Redis配置格式、同步发送、异步发送、异步流发送或Redis命令本机Redis Hash、List、Set、String、Geo、HyperLogLog等数据结构是Java中最常见的映射Map )、列表)、集)、 不仅封装到公共对象桶Object Bucket )中,而且基于基数估计算法HyperLogLog )等结构,生成分布式多值映射Multimap )、本地缓存映射localcaloglog ) 评分顺序集ScoredSortedSet )、提供字典顺序集的块队列Blocking Queue )、有界块队列Bounded Blocking Queue )、两端队列cueue ) 公平队列) Blocking Fair Queue )、延迟多维数据集房间过滤器Bloom Filter )、原子长度AtomicLong )、原子双精度浮点数AtomicDouble )、BitSet )等等此外,Redisson还为Redis文档如分布式锁定锁定)提供了更高级的APP应用程序场景。 实际上,除了分布式锁定之外,Redisson还包括互锁、读写锁定、公平锁定、红色锁定通过实现提供可能过期信号量的基于Redis的高级APP应用程序,Redisson成为构建分布式系统的重要工具。

在提供这些工具的过程中,Redisson广泛使用Redis订阅发行功能中提供的分布式主题Topic )功能。 即使在复杂的分布式环境中,Redisson的每个实例都有能力保持相互交流。 在此基础上,结合具有独特功能的分布式工具,Redisson还提供了更适合不同场景的分布式服务,如分布式远程服务Remote Service )、分布式执行服务distribute reconvery service )、分布式调度任务服务) 已将Redisson设置为基于Redis的Java中间件Middleware )。

Jedis和Lettuce是相对纯的Redis客户端,很少提供高级功能。 因为Jedis的性能很差,所以如果不需要使用Redis的高级功能,建议优先使用Lettuce。

Redisson的优点是提供了许多开箱即用的Redis高级功能。 如果您的APP应用程序需要使用Redis高级功能,建议使用Redisson。 在这种情况下,一般为l

ettuce +Redisson一起使用。

二、springboot简单集成 1)、springboot集成Lettuce

引入依赖:

<!–redis(spring-boot-starter-data-redis中包含的Lettuce)–><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency><!–Lettuce使用线程池必要包–><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId></dependency>

配置文件:

####################redis连接配置############redis: host: 127.0.0.1 port: 6379 password: 123456 database: 0 timeout: 2000ms lettuce: pool: # 连接池最大连接数 max-active: 20 # 连接池中的最小空闲连接 max-idle: 10 # 连接池最大阻塞等待时间使用负数表示没有限制,单位ms) max-wait: 3000

配置RedisTemplate<String, Object>:

@Configurationpublic class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplateRedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>); // 设置数据源的连接工厂(默认会传入框架中自带的(也就是读取完配置文件装配的)LettuceConnectionFactory ) // 也可以自己定义,注入容器,再通过@Qualifier””)传进来 template.setConnectionFactoryfactory); //设置key的序列化器 template.setKeySerializernew StringRedisSerializer)); template.setValueSerializernew jldlr2JsonRedisSerializerObject.class)); // hash的key也采用String的序列化方式 template.setHashKeySerializernew StringRedisSerializer)); template.setHashValueSerializernew jldlr2JsonRedisSerializerObject.class)); return template; }} 2)、Redisson

引入依赖:

<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.11.1</version></dependency>

配置文件:

@Configurationpublic class RedisConfig { @Value”${spring.redis.host}”) private String host; @Value”${spring.redis.port}”) private String port; @Value”${spring.redis.password}”) private String password; @Bean public RedissonClient redissonClient) { Config config = new Config); // redis为单机模式 config.useSingleServer) .setAddress”redis://” + host + “:” + port) .setPasswordpassword); return Redisson.createconfig); } @Bean public RedisTemplate<String, Object> redisTemplateRedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>); //关联 template.setConnectionFactoryfactory); //设置key的序列化器 template.setKeySerializernew StringRedisSerializer)); template.setValueSerializernew jldlr2JsonRedisSerializerObject.class)); // hash的key也采用String的序列化方式 template.setHashKeySerializernew StringRedisSerializer)); template.setHashValueSerializernew jldlr2JsonRedisSerializerObject.class)); return template; }}

完毕!

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注