Skip to content

Commit cea6c5b

Browse files
committed
Refactor - tidy the near cache sending messages
1 parent 47e2410 commit cea6c5b

3 files changed

Lines changed: 58 additions & 49 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>io.ebean</groupId>
3030
<artifactId>ebean</artifactId>
31-
<version>11.31.1-SNAPSHOT</version>
31+
<version>11.31.1</version>
3232
<scope>provided</scope>
3333
</dependency>
3434

src/main/java/io/ebean/redis/RedisCacheFactory.java

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -273,74 +273,79 @@ private class DNearCacheNotify implements NearCacheNotify {
273273

274274
@Override
275275
public void invalidateKeys(String cacheKey, Set<Object> keySet) {
276-
277-
long nanos = System.nanoTime();
278-
try (Jedis resource = jedisPool.getResource()) {
279-
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
280-
ObjectOutputStream os = new ObjectOutputStream(ba);
281-
os.writeUTF(serverId);
282-
os.writeInt(MSG_NEARCACHE_KEYS);
283-
os.writeUTF(cacheKey);
284-
os.writeInt(keySet.size());
285-
for (Object key : keySet) {
286-
os.writeObject(key);
287-
}
288-
os.flush();
289-
os.close();
290-
291-
resource.publish(CHANNEL_NEAR_BYTES, ba.toByteArray());
292-
276+
try {
277+
sendMessage(messageInvalidateKeys(cacheKey, keySet));
293278
} catch (IOException e) {
294279
logger.error("failed to transmit invalidateKeys() message", e);
295-
} finally {
296-
metricMsgOut.add((System.nanoTime() - nanos) / 1000);
297280
}
298281
}
299282

300283
@Override
301284
public void invalidateKey(String cacheKey, Object id) {
302-
303-
long nanos = System.nanoTime();
304-
try (Jedis resource = jedisPool.getResource()) {
305-
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
306-
ObjectOutputStream os = new ObjectOutputStream(ba);
307-
os.writeUTF(serverId);
308-
os.writeInt(MSG_NEARCACHE_KEY);
309-
os.writeUTF(cacheKey);
310-
os.writeObject(id);
311-
os.flush();
312-
os.close();
313-
314-
resource.publish(CHANNEL_NEAR_BYTES, ba.toByteArray());
315-
285+
try {
286+
sendMessage(messageInvalidateKey(cacheKey, id));
316287
} catch (IOException e) {
317288
logger.error("failed to transmit invalidateKeys() message", e);
318-
} finally {
319-
metricMsgOut.addSinceNanos(nanos);
320289
}
321290
}
322291

323292
@Override
324293
public void invalidateClear(String cacheKey) {
325-
326-
long nanos = System.nanoTime();
327-
try (Jedis resource = jedisPool.getResource()) {
328-
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
329-
ObjectOutputStream os = new ObjectOutputStream(ba);
330-
os.writeUTF(serverId);
331-
os.writeInt(MSG_NEARCACHE_CLEAR);
332-
os.writeUTF(cacheKey);
333-
os.flush();
334-
os.close();
335-
336-
resource.publish(CHANNEL_NEAR_BYTES, ba.toByteArray());
337-
294+
try {
295+
sendMessage(messageInvalidateClear(cacheKey));
338296
} catch (IOException e) {
339297
logger.error("failed to transmit invalidateKeys() message", e);
298+
}
299+
}
300+
301+
private void sendMessage(byte[] message) {
302+
long nanos = System.nanoTime();
303+
try {
304+
try (Jedis resource = jedisPool.getResource()) {
305+
resource.publish(CHANNEL_NEAR_BYTES, message);
306+
}
340307
} finally {
341308
metricMsgOut.addSinceNanos(nanos);
342309
}
343310
}
311+
312+
private byte[] messageInvalidateKeys(String cacheKey, Set<Object> keySet) throws IOException {
313+
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
314+
ObjectOutputStream os = new ObjectOutputStream(ba);
315+
os.writeUTF(serverId);
316+
os.writeInt(MSG_NEARCACHE_KEYS);
317+
os.writeUTF(cacheKey);
318+
os.writeInt(keySet.size());
319+
for (Object key : keySet) {
320+
os.writeObject(key);
321+
}
322+
os.flush();
323+
os.close();
324+
return ba.toByteArray();
325+
}
326+
327+
private byte[] messageInvalidateKey(String cacheKey, Object id) throws IOException {
328+
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
329+
ObjectOutputStream os = new ObjectOutputStream(ba);
330+
os.writeUTF(serverId);
331+
os.writeInt(MSG_NEARCACHE_KEY);
332+
os.writeUTF(cacheKey);
333+
os.writeObject(id);
334+
os.flush();
335+
os.close();
336+
return ba.toByteArray();
337+
}
338+
339+
private byte[] messageInvalidateClear(String cacheKey) throws IOException {
340+
ByteArrayOutputStream ba = new ByteArrayOutputStream(100);
341+
ObjectOutputStream os = new ObjectOutputStream(ba);
342+
os.writeUTF(serverId);
343+
os.writeInt(MSG_NEARCACHE_CLEAR);
344+
os.writeUTF(cacheKey);
345+
os.flush();
346+
os.close();
347+
return ba.toByteArray();
348+
}
344349
}
345350

346351
/**

src/test/java/io/ebean/redis/ClusterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ebean.redis;
22

33
import domain.EFoo;
4+
import io.ebean.Ebean;
45
import io.ebean.EbeanServer;
56
import io.ebean.EbeanServerFactory;
67
import io.ebean.config.ServerConfig;
@@ -26,6 +27,9 @@ private EbeanServer createOther() {
2627
@Test
2728
public void test() throws InterruptedException {
2829

30+
// ensure the default server exists first
31+
Ebean.getDefaultServer();
32+
2933
EbeanServer other = createOther();
3034

3135
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)