Lettuce
是一个高性能基于Java
编写的Redis
驱动框架,底层集成了Project Reactor
提供天然的反应式编程,通信框架集成了Netty
使用了非阻塞IO
,5.x
版本之后融合了JDK1.8
的异步编程特性,在保证高性能的同时提供了十分丰富易用的API
。
io.lettuce
lettuce-core
5.1.8.RELEASE
dependencies {
compile 'io.lettuce:lettuce-core:5.1.8.RELEASE'
}
Lettuce
使用的时候依赖于四个主要组件:
RedisURI
:连接信息。RedisClient
:Redis
客户端,特殊地,集群连接有一个定制的RedisClusterClient
。Connection
:Redis
连接,主要是StatefulConnection
或者StatefulRedisConnection
的子类,连接的类型主要由连接的具体方式(单机、哨兵、集群、订阅发布等等)选定,比较重要。RedisCommands
:Redis
命令API
接口,基本上覆盖了Redis
发行版本的所有命令,提供了同步(sync
)、异步(async
)、反应式(reative
)的调用方式,对于使用者而言,会经常跟RedisCommands
系列接口打交道。public void test() throws Exception {
RedisURI redisUri = RedisURI.builder()
.withHost("localhost")
.withPort(6379)
.withTimeout(Duration.of(10, ChronoUnit.SECONDS))
.build();
RedisClient redisClient = RedisClient.create(redisUri);
StatefulRedisConnection connection = redisClient.connect();
RedisCommands redisCommands = connection.sync();
SetArgs setArgs = SetArgs.Builder.nx().ex(5);
String result = redisCommands.set("name", "throwable", setArgs);
Assertions.assertThat(result).isEqualToIgnoringCase("OK");
result = redisCommands.get("name");
connection.close();
redisClient.shutdown();
}
Lettuce
主要提供三种API
:
sync
):RedisCommands
。async
):RedisAsyncCommands
。reactive
):RedisReactiveCommands
。public interface StatefulRedisConnection extends StatefulConnection {
boolean isMulti();
RedisCommands sync();
RedisAsyncCommands async();
RedisReactiveCommands reactive();
}
public void testSyncSetAndGet() throws Exception {
RedisCommands COMMAND = CONNECTION.sync();
SetArgs setArgs = SetArgs.Builder.nx().ex(5);
COMMAND.set("name", "throwable", setArgs);
String value = COMMAND.get("name");
log.info("Get value: {}", value);
}
public interface RedisFuture extends CompletionStage, Future {
String getError();
boolean await(long timeout, TimeUnit unit) throws InterruptedException;
}
public void testAsyncSetAndGet() throws Exception {
RedisAsyncCommands ASYNC_COMMAND = CONNECTION.async();
SetArgs setArgs = SetArgs.Builder.nx().ex(5);
RedisFuture future = ASYNC_COMMAND.set("name", "throwable", setArgs);
future.thenAccept(value -> log.info("Set命令返回:{}", value));
future.get();
}
public void testReactiveSetAndGet() throws Exception {
RedisReactiveCommands REACTIVE_COMMAND = CONNECTION.reactive();
SetArgs setArgs = SetArgs.Builder.nx().ex(5);
REACTIVE_COMMAND.set("name", "throwable", setArgs).block();
REACTIVE_COMMAND.get("name").subscribe(value -> log.info("Get命令返回:{}", value));
Thread.sleep(1000);
}
RedisClusterClient clusterClient = ...
StatefulRedisClusterPubSubConnection connection = clusterClient.connectPubSub();
connection.addListener(new RedisPubSubListener() { ... });
// 同步命令
RedisPubSubCommands sync = connection.sync();
sync.subscribe("channel");
// 异步命令
RedisPubSubAsyncCommands async = connection.async();
RedisFuture future = async.subscribe("channel");
// 反应式命令
RedisPubSubReactiveCommands reactive = connection.reactive();
reactive.subscribe("channel").subscribe();
reactive.observeChannels().doOnNext(patternMessage -> {...}).subscribe()
事务相关的命令就是WATCH
、UNWATCH
、EXEC
、MULTI
和DISCARD
,在RedisCommands
系列接口中有对应的方法。
public void testSyncMulti() throws Exception {
COMMAND.multi();
COMMAND.setex("name-1", 2, "throwable");
COMMAND.setex("name-2", 2, "doge");
TransactionResult result = COMMAND.exec();
int index = 0;
for (Object r : result) {
log.info("Result-{}:{}", index, r);
index++;
}
}
Redis
的Pipeline
(管道机制)可以理解为把多个命令打包在一次请求发送到Redis
服务端,然后Redis
服务端把所有的响应结果打包好一次性返回,从而节省不必要的网络资源(最主要是减少网络请求次数)。
Redis
对于Pipeline
机制如何实现并没有明确的规定,也没有提供特殊的命令支持Pipeline
机制。Pipeline
在Lettuce
中对使用者是透明的,由于底层的通讯框架是Netty
,所以网络通讯层面的优化Lettuce
不需要过多干预,Netty
帮Lettuce
从底层实现了Redis
的Pipeline
机制。
先引入依赖:
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
配置
spring:
redis:
host: 127.0.0.1 # IP
port: 6379 # 端口号
password: 123456 # 密码
timeout: 10000
lettuce:
pool:
max-active: 8 # 连接池最大连接数
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
max-idle: 8 # 连接池中的最大空闲连接
@Configuration
@EnableCaching //启用缓存
public class CacheConfig extends CachingConfigurerSupport {
/**
* 自定义缓存key的生成策略。默认的生成策略是看不懂的(乱码内容) 通过Spring 的依赖注入特性进行自定义的配置注入并且此类是一个配置类可以更多程度的自定义配置
*
* @return
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
/**
* 缓存配置管理器
*/
@Bean
public CacheManager cacheManager(LettuceConnectionFactory factory) {
//创建默认缓存配置对象
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
RedisCacheManager cacheManager = new RedisCacheManager(writer, config);
return cacheManager;
}
/**
* 获取缓存操作助手对象
*
* @return
*/
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory factory) {
//创建Redis缓存操作助手RedisTemplate对象
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(factory);
//以下代码为将RedisTemplate的Value序列化方式由JdkSerializationRedisSerializer更换为Jackson2JsonRedisSerializer
//此种序列化方式结果清晰、容易阅读、存储字节少、速度快,所以推荐更换
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
/**
* 缓存提供类
*/
public class CacheUtils {
//由于当前class不在spring boot框架内(不在web项目中)所以无法使用autowired,使用此种方法进行注入
private static RedisTemplate template = (RedisTemplate) SpringBeanUtil.getBean("redisTemplate");
public static boolean set(String key, T value) {
Gson gson = new Gson();
return set(key, gson.toJson(value));
}
public static boolean set(String key, String value, long validTime) {
boolean result = template.execute(new RedisCallback() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer serializer = template.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
connection.expire(serializer.serialize(key), validTime);
return true;
}
});
return result;
}
public static T get(String key, Class clazz) {
Gson gson = new Gson();
return gson.fromJson(get(key), clazz);
}
public static String get(String key) {
String result = template.execute(new RedisCallback() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer serializer = template.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
public static boolean del(String key) {
return template.delete(key);
}
}