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,12 @@ 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 (idx < 0 ) continue ;
62
+ if ("protocol" .equals (param .substring (0 , idx ))) {
63
+ String ver = param .substring (idx + 1 );
62
64
for (RedisProtocol proto : RedisProtocol .values ()) {
63
65
if (proto .version ().equals (ver )) {
64
66
return proto ;
@@ -70,6 +72,83 @@ public static RedisProtocol getRedisProtocol(URI uri) {
70
72
return null ; // null (default) when not defined
71
73
}
72
74
75
+ private static final Integer ZERO_INTEGER = 0 ;
76
+
77
+ public static ClientSideCache getClientSideCache (URI uri ) {
78
+ if (uri .getQuery () == null ) return null ;
79
+
80
+ boolean guava = false , caffeine = false ; // cache_lib
81
+ Integer maxSize = null ; // cache_max_size --> 0 = disbale
82
+ Integer ttl = null ; // cache_ttl --> 0 = no ttl
83
+ // cache-max-idle
84
+
85
+ String [] params = uri .getQuery ().split ("&" );
86
+ for (String param : params ) {
87
+ int idx = param .indexOf ("=" );
88
+ if (idx < 0 ) continue ;
89
+
90
+ String key = param .substring (0 , idx );
91
+ String val = param .substring (idx + 1 );
92
+
93
+ switch (key ) {
94
+
95
+ case "cache_lib" :
96
+ switch (val ) {
97
+ case "guava" :
98
+ guava = true ;
99
+ break ;
100
+ case "caffeine" :
101
+ caffeine = true ;
102
+ break ;
103
+ default :
104
+ throw new IllegalArgumentException ("Unsupported library " + val );
105
+ }
106
+ break ;
107
+
108
+ case "cache_max_size" :
109
+ try {
110
+ maxSize = Integer .parseInt (val );
111
+ } catch (NumberFormatException nfe ) {
112
+ throw new IllegalArgumentException ("Value of cache_max_size must be an integer." , nfe );
113
+ }
114
+ break ;
115
+
116
+ case "cache_ttl" :
117
+ try {
118
+ ttl = Integer .parseInt (val );
119
+ } catch (NumberFormatException nfe ) {
120
+ throw new IllegalArgumentException ("Value of cache_ttl must be an integer denoting seconds." , nfe );
121
+ }
122
+ break ;
123
+ }
124
+ }
125
+
126
+ // special cases
127
+ if (ZERO_INTEGER .equals (maxSize )) {
128
+ return null ;
129
+ }
130
+ if (!guava && !caffeine && (maxSize != null || ttl != null )) {
131
+ throw new IllegalArgumentException ("The cache library (guava OR caffeine) must be selected." );
132
+ }
133
+ if (ZERO_INTEGER .equals (ttl )) {
134
+ ttl = null ; // below, only null will be checked
135
+ }
136
+
137
+ if (guava ) {
138
+ GuavaCSC .Builder guavaBuilder = GuavaCSC .builder ();
139
+ if (maxSize != null ) guavaBuilder .maximumSize (maxSize );
140
+ if (ttl != null ) guavaBuilder .ttl (ttl );
141
+ return guavaBuilder .build ();
142
+ } else if (caffeine ) {
143
+ CaffeineCSC .Builder caffeineBuilder = CaffeineCSC .builder ();
144
+ if (maxSize != null ) caffeineBuilder .maximumSize (maxSize );
145
+ if (ttl != null ) caffeineBuilder .ttl (ttl );
146
+ return caffeineBuilder .build ();
147
+ }
148
+
149
+ return null ; // null (default) when not defined
150
+ }
151
+
73
152
public static boolean isValid (URI uri ) {
74
153
if (isEmpty (uri .getScheme ()) || isEmpty (uri .getHost ()) || uri .getPort () == -1 ) {
75
154
return false ;
0 commit comments