@@ -102,27 +102,6 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
102
102
this .batchStrategy = batchStrategy ;
103
103
}
104
104
105
- @ Override
106
- public void put (String name , byte [] key , byte [] value , @ Nullable Duration ttl ) {
107
-
108
- Assert .notNull (name , "Name must not be null" );
109
- Assert .notNull (key , "Key must not be null" );
110
- Assert .notNull (value , "Value must not be null" );
111
-
112
- execute (name , connection -> {
113
-
114
- if (shouldExpireWithin (ttl )) {
115
- connection .set (key , value , Expiration .from (ttl .toMillis (), TimeUnit .MILLISECONDS ), SetOption .upsert ());
116
- } else {
117
- connection .set (key , value );
118
- }
119
-
120
- return "OK" ;
121
- });
122
-
123
- statistics .incPuts (name );
124
- }
125
-
126
105
@ Override
127
106
public byte [] get (String name , byte [] key ) {
128
107
return get (name , key , null );
@@ -135,8 +114,8 @@ public byte[] get(String name, byte[] key, @Nullable Duration ttl) {
135
114
Assert .notNull (key , "Key must not be null" );
136
115
137
116
byte [] result = shouldExpireWithin (ttl )
138
- ? execute (name , connection -> connection .getEx (key , Expiration .from (ttl )))
139
- : execute (name , connection -> connection .get (key ));
117
+ ? execute (name , connection -> connection .stringCommands (). getEx (key , Expiration .from (ttl )))
118
+ : execute (name , connection -> connection .stringCommands (). get (key ));
140
119
141
120
statistics .incGets (name );
142
121
@@ -149,6 +128,28 @@ public byte[] get(String name, byte[] key, @Nullable Duration ttl) {
149
128
return result ;
150
129
}
151
130
131
+ @ Override
132
+ public void put (String name , byte [] key , byte [] value , @ Nullable Duration ttl ) {
133
+
134
+ Assert .notNull (name , "Name must not be null" );
135
+ Assert .notNull (key , "Key must not be null" );
136
+ Assert .notNull (value , "Value must not be null" );
137
+
138
+ execute (name , connection -> {
139
+
140
+ if (shouldExpireWithin (ttl )) {
141
+ connection .stringCommands ()
142
+ .set (key , value , Expiration .from (ttl .toMillis (), TimeUnit .MILLISECONDS ), SetOption .upsert ());
143
+ } else {
144
+ connection .stringCommands ().set (key , value );
145
+ }
146
+
147
+ return "OK" ;
148
+ });
149
+
150
+ statistics .incPuts (name );
151
+ }
152
+
152
153
@ Override
153
154
public byte [] putIfAbsent (String name , byte [] key , byte [] value , @ Nullable Duration ttl ) {
154
155
@@ -167,17 +168,17 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
167
168
boolean put ;
168
169
169
170
if (shouldExpireWithin (ttl )) {
170
- put = connection .set (key , value , Expiration .from (ttl ), SetOption .ifAbsent ());
171
+ put = isTrue ( connection .stringCommands (). set (key , value , Expiration .from (ttl ), SetOption .ifAbsent () ));
171
172
} else {
172
- put = connection .setNX (key , value );
173
+ put = isTrue ( connection .stringCommands (). setNX (key , value ) );
173
174
}
174
175
175
176
if (put ) {
176
177
statistics .incPuts (name );
177
178
return null ;
178
179
}
179
180
180
- return connection .get (key );
181
+ return connection .stringCommands (). get (key );
181
182
182
183
} finally {
183
184
if (isLockingCacheWriter ()) {
@@ -193,7 +194,7 @@ public void remove(String name, byte[] key) {
193
194
Assert .notNull (name , "Name must not be null" );
194
195
Assert .notNull (key , "Key must not be null" );
195
196
196
- execute (name , connection -> connection .del (key ));
197
+ execute (name , connection -> connection .keyCommands (). del (key ));
197
198
statistics .incDeletes (name );
198
199
}
199
200
@@ -257,6 +258,16 @@ void lock(String name) {
257
258
execute (name , connection -> doLock (name , name , null , connection ));
258
259
}
259
260
261
+ @ Nullable
262
+ private Boolean doLock (String name , Object contextualKey , @ Nullable Object contextualValue ,
263
+ RedisConnection connection ) {
264
+
265
+ Expiration expiration = Expiration .from (this .lockTtl .getTimeToLive (contextualKey , contextualValue ));
266
+
267
+ return connection .stringCommands ()
268
+ .set (createCacheLockKey (name ), new byte [0 ], expiration , SetOption .SET_IF_ABSENT );
269
+ }
270
+
260
271
/**
261
272
* Explicitly remove a write lock from a cache.
262
273
*
@@ -266,20 +277,13 @@ void unlock(String name) {
266
277
executeLockFree (connection -> doUnlock (name , connection ));
267
278
}
268
279
269
- private Boolean doLock (String name , Object contextualKey , Object contextualValue , RedisConnection connection ) {
270
-
271
- Expiration expiration = lockTtl == null ? Expiration .persistent ()
272
- : Expiration .from (lockTtl .getTimeToLive (contextualKey , contextualValue ));
273
-
274
- return connection .set (createCacheLockKey (name ), new byte [0 ], expiration , SetOption .SET_IF_ABSENT );
275
- }
276
-
280
+ @ Nullable
277
281
private Long doUnlock (String name , RedisConnection connection ) {
278
- return connection .del (createCacheLockKey (name ));
282
+ return connection .keyCommands (). del (createCacheLockKey (name ));
279
283
}
280
284
281
285
boolean doCheckLock (String name , RedisConnection connection ) {
282
- return connection .exists (createCacheLockKey (name ));
286
+ return isTrue ( connection .keyCommands (). exists (createCacheLockKey (name ) ));
283
287
}
284
288
285
289
/**
@@ -291,24 +295,16 @@ private boolean isLockingCacheWriter() {
291
295
292
296
private <T > T execute (String name , Function <RedisConnection , T > callback ) {
293
297
294
- RedisConnection connection = connectionFactory .getConnection ();
295
-
296
- try {
298
+ try (RedisConnection connection = connectionFactory .getConnection ()) {
297
299
checkAndPotentiallyWaitUntilUnlocked (name , connection );
298
300
return callback .apply (connection );
299
- } finally {
300
- connection .close ();
301
301
}
302
302
}
303
303
304
304
private void executeLockFree (Consumer <RedisConnection > callback ) {
305
305
306
- RedisConnection connection = connectionFactory .getConnection ();
307
-
308
- try {
306
+ try (RedisConnection connection = connectionFactory .getConnection ()) {
309
307
callback .accept (connection );
310
- } finally {
311
- connection .close ();
312
308
}
313
309
}
314
310
@@ -337,11 +333,15 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
337
333
}
338
334
}
339
335
340
- private static boolean shouldExpireWithin (@ Nullable Duration ttl ) {
341
- return ttl != null && !ttl .isZero () && !ttl .isNegative ();
342
- }
343
-
344
336
private static byte [] createCacheLockKey (String name ) {
345
337
return (name + "~lock" ).getBytes (StandardCharsets .UTF_8 );
346
338
}
339
+
340
+ private boolean isTrue (@ Nullable Boolean value ) {
341
+ return Boolean .TRUE .equals (value );
342
+ }
343
+
344
+ private static boolean shouldExpireWithin (@ Nullable Duration ttl ) {
345
+ return ttl != null && !ttl .isZero () && !ttl .isNegative ();
346
+ }
347
347
}
0 commit comments