3
3
import lombok .extern .slf4j .Slf4j ;
4
4
import org .apache .commons .pool2 .impl .GenericObjectPool ;
5
5
import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .dao .DataAccessException ;
6
7
import org .springframework .data .redis .connection .RedisConnectionFactory ;
7
8
import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
9
+ import org .springframework .data .redis .core .RedisOperations ;
10
+ import org .springframework .data .redis .core .SessionCallback ;
8
11
import org .springframework .data .redis .core .StringRedisTemplate ;
9
12
import org .springframework .stereotype .Component ;
10
13
import redis .clients .jedis .Jedis ;
13
16
import javax .annotation .PostConstruct ;
14
17
import javax .transaction .Transactional ;
15
18
import java .lang .reflect .Field ;
19
+ import java .util .List ;
16
20
import java .util .concurrent .Executors ;
17
21
import java .util .concurrent .TimeUnit ;
18
22
import java .util .stream .IntStream ;
@@ -30,14 +34,14 @@ public class TestService {
30
34
public String multiTest () {
31
35
Long current = System .currentTimeMillis ();
32
36
stringRedisTemplateTran .multi ();
33
- IntStream .rangeClosed (1 , 1000 ).forEach (i -> stringRedisTemplateTran .opsForValue ().increment ("test" , 1 ));
37
+ IntStream .rangeClosed (1 , 10000 ).forEach (i -> stringRedisTemplateTran .opsForValue ().increment ("test" , 1 ));
34
38
stringRedisTemplateTran .exec ();
35
39
return System .currentTimeMillis () - current + "ms " + stringRedisTemplateTran .opsForValue ().get ("test" );
36
40
}
37
41
38
42
public String noMultiTest () {
39
43
Long current = System .currentTimeMillis ();
40
- IntStream .rangeClosed (1 , 1000 ).forEach (i -> stringRedisTemplate .opsForValue ().increment ("test" , 1 ));
44
+ IntStream .rangeClosed (1 , 10000 ).forEach (i -> stringRedisTemplate .opsForValue ().increment ("test" , 1 ));
41
45
return System .currentTimeMillis () - current + "ms " + stringRedisTemplate .opsForValue ().get ("test" );
42
46
}
43
47
@@ -46,12 +50,21 @@ public String redisTemplateInTransactional() {
46
50
return "" + stringRedisTemplateTran .opsForValue ().increment ("test" , 1 );
47
51
}
48
52
49
- @ Transactional
50
- public String multiTestInTransactional () {
53
+ public String redisTemplateNotInTransactional () throws InterruptedException {
54
+ TimeUnit .SECONDS .sleep (10 );
55
+ return "" + stringRedisTemplateTran .opsForValue ().increment ("test" , 1 );
56
+ }
57
+
58
+ public String multiTestCallback () {
51
59
Long current = System .currentTimeMillis ();
52
- stringRedisTemplateTran .multi ();
53
- IntStream .rangeClosed (1 , 1000 ).forEach (i -> stringRedisTemplateTran .opsForValue ().increment ("test" , 1 ));
54
- stringRedisTemplateTran .exec ();
60
+ List <Object > txResults = stringRedisTemplate .execute (new SessionCallback <List <Object >>() {
61
+ public List <Object > execute (RedisOperations operations ) throws DataAccessException {
62
+ operations .multi ();
63
+ IntStream .rangeClosed (1 , 10000 ).forEach (i -> stringRedisTemplate .opsForValue ().increment ("test" , 1 ));
64
+ return operations .exec ();
65
+ }
66
+ });
67
+ log .info ("txResults = {}" , txResults );
55
68
return System .currentTimeMillis () - current + "ms " + stringRedisTemplate .opsForValue ().get ("test" );
56
69
}
57
70
0 commit comments