@@ -102,27 +102,6 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
102102 this .batchStrategy = batchStrategy ;
103103 }
104104
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-
126105 @ Override
127106 public byte [] get (String name , byte [] key ) {
128107 return get (name , key , null );
@@ -135,8 +114,8 @@ public byte[] get(String name, byte[] key, @Nullable Duration ttl) {
135114 Assert .notNull (key , "Key must not be null" );
136115
137116 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 ));
140119
141120 statistics .incGets (name );
142121
@@ -149,6 +128,28 @@ public byte[] get(String name, byte[] key, @Nullable Duration ttl) {
149128 return result ;
150129 }
151130
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+
152153 @ Override
153154 public byte [] putIfAbsent (String name , byte [] key , byte [] value , @ Nullable Duration ttl ) {
154155
@@ -167,17 +168,17 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
167168 boolean put ;
168169
169170 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 () ));
171172 } else {
172- put = connection .setNX (key , value );
173+ put = isTrue ( connection .stringCommands (). setNX (key , value ) );
173174 }
174175
175176 if (put ) {
176177 statistics .incPuts (name );
177178 return null ;
178179 }
179180
180- return connection .get (key );
181+ return connection .stringCommands (). get (key );
181182
182183 } finally {
183184 if (isLockingCacheWriter ()) {
@@ -193,7 +194,7 @@ public void remove(String name, byte[] key) {
193194 Assert .notNull (name , "Name must not be null" );
194195 Assert .notNull (key , "Key must not be null" );
195196
196- execute (name , connection -> connection .del (key ));
197+ execute (name , connection -> connection .keyCommands (). del (key ));
197198 statistics .incDeletes (name );
198199 }
199200
@@ -257,6 +258,16 @@ void lock(String name) {
257258 execute (name , connection -> doLock (name , name , null , connection ));
258259 }
259260
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+
260271 /**
261272 * Explicitly remove a write lock from a cache.
262273 *
@@ -266,20 +277,13 @@ void unlock(String name) {
266277 executeLockFree (connection -> doUnlock (name , connection ));
267278 }
268279
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
277281 private Long doUnlock (String name , RedisConnection connection ) {
278- return connection .del (createCacheLockKey (name ));
282+ return connection .keyCommands (). del (createCacheLockKey (name ));
279283 }
280284
281285 boolean doCheckLock (String name , RedisConnection connection ) {
282- return connection .exists (createCacheLockKey (name ));
286+ return isTrue ( connection .keyCommands (). exists (createCacheLockKey (name ) ));
283287 }
284288
285289 /**
@@ -291,24 +295,16 @@ private boolean isLockingCacheWriter() {
291295
292296 private <T > T execute (String name , Function <RedisConnection , T > callback ) {
293297
294- RedisConnection connection = connectionFactory .getConnection ();
295-
296- try {
298+ try (RedisConnection connection = connectionFactory .getConnection ()) {
297299 checkAndPotentiallyWaitUntilUnlocked (name , connection );
298300 return callback .apply (connection );
299- } finally {
300- connection .close ();
301301 }
302302 }
303303
304304 private void executeLockFree (Consumer <RedisConnection > callback ) {
305305
306- RedisConnection connection = connectionFactory .getConnection ();
307-
308- try {
306+ try (RedisConnection connection = connectionFactory .getConnection ()) {
309307 callback .accept (connection );
310- } finally {
311- connection .close ();
312308 }
313309 }
314310
@@ -337,11 +333,15 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
337333 }
338334 }
339335
340- private static boolean shouldExpireWithin (@ Nullable Duration ttl ) {
341- return ttl != null && !ttl .isZero () && !ttl .isNegative ();
342- }
343-
344336 private static byte [] createCacheLockKey (String name ) {
345337 return (name + "~lock" ).getBytes (StandardCharsets .UTF_8 );
346338 }
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+ }
347347}
0 commit comments