Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7fe8b7

Browse files
committed
add redis util in lettuce sample sync io
1 parent 1d727d5 commit c7fe8b7

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

springboot-lettuce-sample/src/main/java/com/ipman/springboot/lettuce/sample/config/LettucePoolConfig.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.lettuce.core.RedisClient;
44
import io.lettuce.core.api.StatefulRedisConnection;
55
import io.lettuce.core.support.ConnectionPoolSupport;
6+
import lombok.Getter;
67
import org.apache.commons.pool2.impl.GenericObjectPool;
78
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
89
import org.springframework.beans.factory.annotation.Value;
@@ -45,11 +46,12 @@ public class LettucePoolConfig {
4546
//Apache-Common-Pool是一个对象池,用于缓存Redis连接,
4647
//因为Letture本身基于Netty的异步驱动,但基于Servlet模型的同步访问时,连接池是必要的
4748
//连接池可以很好的复用连接,减少重复的IO消耗与RedisURI创建实例的性能消耗
49+
@Getter
4850
GenericObjectPool<StatefulRedisConnection<String, String>> redisConnectionPool;
4951

5052
//Servlet初始化时先初始化Lettuce连接池
5153
@PostConstruct
52-
public void init() {
54+
private void init() {
5355
GenericObjectPoolConfig<StatefulRedisConnection<String, String>> redisPoolConfig
5456
= new GenericObjectPoolConfig<>();
5557
redisPoolConfig.setMaxIdle(this.maxIdle);
@@ -62,7 +64,7 @@ public void init() {
6264

6365
//Servlet销毁时先销毁Lettuce连接池
6466
@PreDestroy
65-
public void destroy() {
67+
private void destroy() {
6668
redisConnectionPool.close();
6769
redisClient.shutdown();
6870
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.ipman.springboot.lettuce.sample.utils;
2+
3+
import com.ipman.springboot.lettuce.sample.config.LettucePoolConfig;
4+
import io.lettuce.core.api.StatefulRedisConnection;
5+
import io.lettuce.core.api.sync.RedisCommands;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
/**
11+
* Created by ipipman on 2021/1/20.
12+
*
13+
* @version V1.0
14+
* @Package com.ipman.springboot.lettuce.sample.utils
15+
* @Description: (用一句话描述该文件做什么)
16+
* @date 2021/1/20 7:04 下午
17+
*/
18+
@Component
19+
@Slf4j
20+
public class LettuceUtil {
21+
22+
@Autowired
23+
LettucePoolConfig lettucePoolConfig;
24+
25+
//编写executeSync方法,在方法中,获取Redis连接,利用Callback操作Redis,最后释放连接,并返回结果
26+
//这里使用的同步的方式执行cmd指令
27+
public <T> T executeSync(SyncCommandCallback<T> callback) {
28+
//这里利用try的语法糖,执行完,自动给释放连接
29+
try (StatefulRedisConnection<String, String> connection = lettucePoolConfig.getRedisConnectionPool().borrowObject()) {
30+
//开启自动提交,如果false,命令会被缓冲,调用flushCommand()方法发出
31+
connection.setAutoFlushCommands(true);
32+
//设置为同步模式
33+
RedisCommands<String, String> commands = connection.sync();
34+
//执行传入的实现类
35+
return callback.doInConnection(commands);
36+
} catch (Exception e) {
37+
log.error(e.getMessage());
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
42+
//分装一个set方法
43+
public String set(final String key, final String val) {
44+
return executeSync(commands -> commands.set(key, val));
45+
}
46+
47+
//分装一个get方法
48+
public String get(final String key) {
49+
return executeSync(commands -> commands.get(key));
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ipman.springboot.lettuce.sample.utils;
2+
3+
import io.lettuce.core.api.sync.RedisCommands;
4+
5+
/**
6+
* Created by ipipman on 2021/1/20.
7+
*
8+
* @version V1.0
9+
* @Package com.ipman.springboot.lettuce.sample.utils
10+
* @Description: (用一句话描述该文件做什么)
11+
* @date 2021/1/20 7:02 下午
12+
*/
13+
@FunctionalInterface
14+
public interface SyncCommandCallback<T> {
15+
16+
//抽象方法,为了简化代码,便于传入回调函数
17+
T doInConnection(RedisCommands<String, String> commands);
18+
}

0 commit comments

Comments
 (0)