1
1
package redis .clients .jedis .util ;
2
2
3
3
import java .net .URI ;
4
+ import redis .clients .jedis .ClientSideCache ;
4
5
import redis .clients .jedis .HostAndPort ;
5
6
import redis .clients .jedis .Protocol ;
6
7
import redis .clients .jedis .RedisProtocol ;
@@ -54,11 +55,11 @@ public static int getDBIndex(URI uri) {
54
55
public static RedisProtocol getRedisProtocol (URI uri ) {
55
56
if (uri .getQuery () == null ) return null ;
56
57
57
- String [] pairs = uri .getQuery ().split ("&" );
58
- for (String pair : pairs ) {
59
- int idx = pair .indexOf ("=" );
60
- if ("protocol" .equals (pair .substring (0 , idx ))) {
61
- String ver = pair .substring (idx + 1 );
58
+ String [] params = uri .getQuery ().split ("&" );
59
+ for (String param : params ) {
60
+ int idx = param .indexOf ("=" );
61
+ if ("protocol" .equals (param .substring (0 , idx ))) {
62
+ String ver = param .substring (idx + 1 );
62
63
for (RedisProtocol proto : RedisProtocol .values ()) {
63
64
if (proto .version ().equals (ver )) {
64
65
return proto ;
@@ -70,6 +71,73 @@ public static RedisProtocol getRedisProtocol(URI uri) {
70
71
return null ; // null (default) when not defined
71
72
}
72
73
74
+ private static final Integer ZERO_INTEGER = 0 ;
75
+
76
+ public static ClientSideCache getClientSideCache (URI uri ) {
77
+ if (uri .getQuery () == null ) return null ;
78
+
79
+ boolean guava = false , caffeine = false ; // cache_lib
80
+ Integer maxSize = null ; // cache_max_size --> 0 = disbale
81
+ Integer ttl = null ; // cache_ttl --> 0 = no ttl
82
+ // cache-max-idle
83
+
84
+ String [] params = uri .getQuery ().split ("&" );
85
+ for (String param : params ) {
86
+ int idx = param .indexOf ("=" );
87
+ String key = param .substring (0 , idx );
88
+ String val = param .substring (idx + 1 );
89
+
90
+ switch (key ) {
91
+
92
+ case "cache_lib" :
93
+ switch (val ) {
94
+ case "guava" :
95
+ guava = true ;
96
+ break ;
97
+ case "caffeine" :
98
+ caffeine = true ;
99
+ break ;
100
+ default :
101
+ throw new IllegalArgumentException ("Unsupported library " + val );
102
+ }
103
+ break ;
104
+
105
+ case "cache_max_size" :
106
+ maxSize = Integer .parseInt (val );
107
+ break ;
108
+
109
+ case "ttl" :
110
+ ttl = Integer .parseInt (val );
111
+ break ;
112
+ }
113
+ }
114
+
115
+ // special cases
116
+ if (ZERO_INTEGER .equals (maxSize )) {
117
+ return null ;
118
+ }
119
+ if (ZERO_INTEGER .equals (ttl )) {
120
+ ttl = null ; // below, only null will be checked
121
+ }
122
+ if (!guava && !caffeine ) {
123
+ throw new IllegalArgumentException ("The cache library (guava OR caffeine) must be selected." );
124
+ }
125
+
126
+ if (guava ) {
127
+ GuavaCSC .Builder guavaBuilder = GuavaCSC .builder ();
128
+ if (maxSize != null ) guavaBuilder .maximumSize (maxSize );
129
+ if (ttl != null ) guavaBuilder .ttl (ttl );
130
+ return guavaBuilder .build ();
131
+ } else if (caffeine ) {
132
+ CaffeineCSC .Builder caffeineBuilder = CaffeineCSC .builder ();
133
+ if (maxSize != null ) caffeineBuilder .maximumSize (maxSize );
134
+ if (ttl != null ) caffeineBuilder .ttl (ttl );
135
+ return caffeineBuilder .build ();
136
+ }
137
+
138
+ return null ; // null (default) when not defined
139
+ }
140
+
73
141
public static boolean isValid (URI uri ) {
74
142
if (isEmpty (uri .getScheme ()) || isEmpty (uri .getHost ()) || uri .getPort () == -1 ) {
75
143
return false ;
0 commit comments