@@ -37,7 +37,7 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
37
37
.getBytes (Charset .forName ("UTF-8" ));
38
38
39
39
private final RedisConnectionFactory connectionFactory ;
40
- private final Duration lockTimeout ;
40
+ private final Duration sleepTime ;
41
41
42
42
/**
43
43
* @param connectionFactory must not be {@literal null}.
@@ -48,20 +48,34 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
48
48
49
49
/**
50
50
* @param connectionFactory must not be {@literal null}.
51
- * @param lockTimeout must not be {@literal null}. Use {@link Duration#ZERO} to disable locking.
51
+ * @param sleepTime sleep time between lock request attempts. Must not be {@literal null}. Use {@link Duration#ZERO}
52
+ * to disable locking.
52
53
*/
53
- DefaultRedisCacheWriter (RedisConnectionFactory connectionFactory , Duration lockTimeout ) {
54
+ DefaultRedisCacheWriter (RedisConnectionFactory connectionFactory , Duration sleepTime ) {
54
55
55
56
Assert .notNull (connectionFactory , "ConnectionFactory must not be null!" );
56
- Assert .notNull (lockTimeout , "LockTimeout must not be null!" );
57
+ Assert .notNull (sleepTime , "SleepTime must not be null!" );
57
58
58
59
this .connectionFactory = connectionFactory ;
59
- this .lockTimeout = lockTimeout ;
60
+ this .sleepTime = sleepTime ;
61
+ }
62
+
63
+ public static DefaultRedisCacheWriter nonLockingRedisCacheWriter (RedisConnectionFactory connectionFactory ) {
64
+ return new DefaultRedisCacheWriter (connectionFactory );
65
+ }
66
+
67
+ public static DefaultRedisCacheWriter lockingRedisCacheWriter (RedisConnectionFactory connectionFactory ) {
68
+ return new DefaultRedisCacheWriter (connectionFactory , Duration .ofMillis (50 ));
60
69
}
61
70
62
71
@ Override
63
72
public void put (String name , byte [] key , byte [] value , Duration ttl ) {
64
73
74
+ Assert .notNull (name , "Name must not be null!" );
75
+ Assert .notNull (key , "Key must not be null!" );
76
+ Assert .notNull (value , "Value must not be null!" );
77
+ Assert .notNull (ttl , "Ttl must not be null!" );
78
+
65
79
execute (name , connection -> {
66
80
67
81
if (shouldExpireWithin (ttl )) {
@@ -76,12 +90,21 @@ public void put(String name, byte[] key, byte[] value, Duration ttl) {
76
90
77
91
@ Override
78
92
public byte [] get (String name , byte [] key ) {
93
+
94
+ Assert .notNull (name , "Name must not be null!" );
95
+ Assert .notNull (key , "Key must not be null!" );
96
+
79
97
return execute (name , connection -> connection .get (key ));
80
98
}
81
99
82
100
@ Override
83
101
public byte [] putIfAbsent (String name , byte [] key , byte [] value , Duration ttl ) {
84
102
103
+ Assert .notNull (name , "Name must not be null!" );
104
+ Assert .notNull (key , "Key must not be null!" );
105
+ Assert .notNull (value , "Value must not be null!" );
106
+ Assert .notNull (ttl , "Ttl must not be null!" );
107
+
85
108
return execute (name , connection -> {
86
109
87
110
if (connection .setNX (key , value )) {
@@ -98,11 +121,15 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, Duration ttl) {
98
121
99
122
@ Override
100
123
public void remove (String name , byte [] key ) {
124
+
125
+ Assert .notNull (name , "Name must not be null!" );
126
+ Assert .notNull (key , "Key must not be null!" );
127
+
101
128
execute (name , connection -> connection .del (key ));
102
129
}
103
130
104
131
public void lock (String name ) {
105
- executeWithoutLockCheck ( connection -> doLock (name , connection ));
132
+ execute ( name , connection -> doLock (name , connection ));
106
133
}
107
134
108
135
private Boolean doLock (String name , RedisConnection connection ) {
@@ -128,35 +155,40 @@ private boolean doCheckLock(String name, RedisConnection connection) {
128
155
@ Override
129
156
public void clean (String name , byte [] pattern ) {
130
157
131
- RedisConnection connection = connectionFactory .getConnection ();
158
+ Assert .notNull (name , "Name must not be null!" );
159
+ Assert .notNull (pattern , "Pattern must not be null!" );
132
160
133
- if (isLockingCacheWriter ()) {
134
- doLock (name , connection );
135
- }
136
-
137
- try {
138
- if (connection instanceof RedisClusterConnection ) {
161
+ execute (name , connection -> {
139
162
140
- byte [][] keys = connection .keys (pattern ).stream ().toArray (size -> new byte [size ][]);
141
- connection .del (keys );
142
- } else {
143
- connection .eval (CLEAN_SCRIPT , ReturnType .INTEGER , 0 , pattern );
163
+ if (isLockingCacheWriter ()) {
164
+ doLock (name , connection );
144
165
}
145
- } finally {
146
166
147
- if (isLockingCacheWriter ()) {
148
- doUnlock (name , connection );
167
+ try {
168
+ if (connection instanceof RedisClusterConnection ) {
169
+
170
+ byte [][] keys = connection .keys (pattern ).stream ().toArray (size -> new byte [size ][]);
171
+ connection .del (keys );
172
+ } else {
173
+ connection .eval (CLEAN_SCRIPT , ReturnType .INTEGER , 0 , pattern );
174
+ }
175
+ } finally {
176
+
177
+ if (isLockingCacheWriter ()) {
178
+ doUnlock (name , connection );
179
+ }
149
180
}
150
- connection .close ();
151
- }
181
+
182
+ return "OK" ;
183
+ });
152
184
}
153
185
154
186
public <T > T execute (String name , ConnectionCallback <T > callback ) {
155
187
156
188
RedisConnection connection = connectionFactory .getConnection ();
157
189
try {
158
190
159
- checkAndPotentiallyWaitForLock (name , connection );
191
+ checkAndPotentiallyWaitUntilUnlocked (name , connection );
160
192
return callback .doWithConnection (connection );
161
193
} finally {
162
194
connection .close ();
@@ -175,14 +207,14 @@ private <T> T executeWithoutLockCheck(ConnectionCallback<T> callback) {
175
207
}
176
208
177
209
public boolean isLockingCacheWriter () {
178
- return !lockTimeout .isZero () && !lockTimeout .isNegative ();
210
+ return !sleepTime .isZero () && !sleepTime .isNegative ();
179
211
}
180
212
181
- private void checkAndPotentiallyWaitForLock (String name , RedisConnection connection ) {
213
+ private void checkAndPotentiallyWaitUntilUnlocked (String name , RedisConnection connection ) {
182
214
183
215
if (isLockingCacheWriter ()) {
184
216
185
- long timeout = lockTimeout .toMillis ();
217
+ long timeout = sleepTime .toMillis ();
186
218
187
219
while (doCheckLock (name , connection )) {
188
220
try {
0 commit comments