4
4
import static org .junit .Assert .assertEquals ;
5
5
import static org .junit .Assert .assertNull ;
6
6
7
+ import com .github .benmanes .caffeine .cache .Cache ;
7
8
import com .github .benmanes .caffeine .cache .Caffeine ;
8
- import com .google .common .cache .CacheBuilder ;
9
+ import com .github .benmanes .caffeine .cache .stats .CacheStats ;
10
+
11
+ import java .util .concurrent .TimeUnit ;
9
12
import java .util .function .Supplier ;
10
13
import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
11
14
import org .hamcrest .Matchers ;
22
25
import redis .clients .jedis .JedisClientConfig ;
23
26
import redis .clients .jedis .JedisPooled ;
24
27
25
- public class ClientSideCacheLibsTest {
26
-
28
+ public class CaffeineClientSideCacheTest {
29
+
27
30
protected static final HostAndPort hnp = HostAndPorts .getRedisServers ().get (1 );
28
-
31
+
29
32
protected Jedis control ;
30
-
33
+
31
34
@ Before
32
35
public void setUp () throws Exception {
33
36
control = new Jedis (hnp , DefaultJedisClientConfig .builder ().password ("foobared" ).build ());
34
37
control .flushAll ();
35
38
}
36
-
39
+
37
40
@ After
38
41
public void tearDown () throws Exception {
39
42
control .close ();
40
43
}
41
-
44
+
42
45
private static final Supplier <JedisClientConfig > clientConfig
43
46
= () -> DefaultJedisClientConfig .builder ().resp3 ().password ("foobared" ).build ();
44
-
47
+
45
48
private static final Supplier <GenericObjectPoolConfig <Connection >> singleConnectionPoolConfig
46
49
= () -> {
47
50
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig ();
48
51
poolConfig .setMaxTotal (1 );
49
52
return poolConfig ;
50
53
};
51
-
52
- @ Test
53
- public void guavaSimple () {
54
- GuavaClientSideCache guava = GuavaClientSideCache .builder ().maximumSize (10 ).ttl (10 )
55
- .hashFunction (com .google .common .hash .Hashing .farmHashFingerprint64 ()).build ();
56
- try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (), guava )) {
57
- control .set ("foo" , "bar" );
58
- assertEquals ("bar" , jedis .get ("foo" ));
59
- control .del ("foo" );
60
- assertThat (jedis .get ("foo" ), Matchers .oneOf ("bar" , null )); // ?
61
- }
62
- }
63
-
64
- @ Test
65
- public void guavaMore () {
66
-
67
- com .google .common .cache .Cache guava = CacheBuilder .newBuilder ().recordStats ().build ();
68
-
69
- try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (), new GuavaClientSideCache (guava ),
70
- singleConnectionPoolConfig .get ())) {
71
- control .set ("foo" , "bar" );
72
- assertEquals (0 , guava .size ());
73
- assertEquals ("bar" , jedis .get ("foo" ));
74
- assertEquals (1 , guava .size ());
75
- control .flushAll ();
76
- assertEquals (1 , guava .size ());
77
- assertEquals ("bar" , jedis .get ("foo" ));
78
- assertEquals (1 , guava .size ());
79
- jedis .ping ();
80
- assertEquals (0 , guava .size ());
81
- assertNull (jedis .get ("foo" ));
82
- assertEquals (0 , guava .size ());
83
- }
84
-
85
- com .google .common .cache .CacheStats stats = guava .stats ();
86
- assertEquals (1L , stats .hitCount ());
87
- assertThat (stats .missCount (), Matchers .greaterThan (0L ));
88
- }
89
-
54
+
90
55
@ Test
91
- public void caffeineSimple () {
56
+ public void simple () {
92
57
CaffeineClientSideCache caffeine = CaffeineClientSideCache .builder ().maximumSize (10 ).ttl (10 ).build ();
93
58
try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (), caffeine )) {
94
59
control .set ("foo" , "bar" );
@@ -97,12 +62,12 @@ public void caffeineSimple() {
97
62
assertThat (jedis .get ("foo" ), Matchers .oneOf ("bar" , null )); // ?
98
63
}
99
64
}
100
-
65
+
101
66
@ Test
102
- public void caffeineMore () {
103
-
104
- com . github . benmanes . caffeine . cache . Cache caffeine = Caffeine .newBuilder ().recordStats ().build ();
105
-
67
+ public void individualCommandsAndThenStats () {
68
+
69
+ Cache caffeine = Caffeine .newBuilder ().recordStats ().build ();
70
+
106
71
try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (),
107
72
new CaffeineClientSideCache (caffeine , new OpenHftCommandHasher ()),
108
73
singleConnectionPoolConfig .get ())) {
@@ -119,9 +84,51 @@ public void caffeineMore() {
119
84
assertNull (jedis .get ("foo" ));
120
85
assertEquals (0 , caffeine .estimatedSize ());
121
86
}
122
-
123
- com . github . benmanes . caffeine . cache . stats . CacheStats stats = caffeine .stats ();
87
+
88
+ CacheStats stats = caffeine .stats ();
124
89
assertEquals (1L , stats .hitCount ());
125
90
assertThat (stats .missCount (), Matchers .greaterThan (0L ));
126
91
}
92
+
93
+ @ Test
94
+ public void maximumSize () {
95
+ final long maxSize = 10 ;
96
+ final long maxEstimatedSize = 40 ;
97
+ int count = 1000 ;
98
+ for (int i = 0 ; i < count ; i ++) {
99
+ control .set ("k" + i , "v" + i );
100
+ }
101
+
102
+ Cache caffeine = Caffeine .newBuilder ().maximumSize (maxSize ).recordStats ().build ();
103
+ try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (), new CaffeineClientSideCache (caffeine ))) {
104
+ for (int i = 0 ; i < count ; i ++) {
105
+ jedis .get ("k" + i );
106
+ assertThat (caffeine .estimatedSize (), Matchers .lessThan (maxEstimatedSize ));
107
+ }
108
+ }
109
+ assertThat (caffeine .stats ().evictionCount (), Matchers .greaterThan (count - maxEstimatedSize ));
110
+ }
111
+
112
+ @ Test
113
+ public void timeToLive () throws InterruptedException {
114
+ int count = 1000 ;
115
+ for (int i = 0 ; i < count ; i ++) {
116
+ control .set ("k" + i , "v" + i );
117
+ }
118
+
119
+ Cache caffeine = Caffeine .newBuilder ().expireAfterWrite (1 , TimeUnit .SECONDS ).recordStats ().build ();
120
+ try (JedisPooled jedis = new JedisPooled (hnp , clientConfig .get (), new CaffeineClientSideCache (caffeine ))) {
121
+ for (int i = 0 ; i < count ; i ++) {
122
+ jedis .get ("k" + i );
123
+ }
124
+ }
125
+ assertThat (caffeine .estimatedSize (), Matchers .equalTo ((long ) count ));
126
+ assertThat (caffeine .stats ().evictionCount (), Matchers .equalTo (0L ));
127
+
128
+ TimeUnit .SECONDS .sleep (2 );
129
+ caffeine .cleanUp ();
130
+ assertThat (caffeine .estimatedSize (), Matchers .equalTo (0L ));
131
+ assertThat (caffeine .stats ().evictionCount (), Matchers .equalTo ((long ) count ));
132
+ }
133
+
127
134
}
0 commit comments