@@ -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 /**
0 commit comments