From da23ae3d6ae290e38e8031b530fef515b442365b Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Wed, 3 May 2023 14:38:57 +0300 Subject: [PATCH] Feature - MapPutListener, MapRemoveListener added to RMap object. #220 --- .../main/java/org/redisson/RedissonMap.java | 26 ++++++++++++++ .../src/main/java/org/redisson/api/RMap.java | 15 +++++++- .../main/java/org/redisson/api/RMapAsync.java | 13 +++++++ .../java/org/redisson/api/RMapReactive.java | 13 +++++++ .../main/java/org/redisson/api/RMapRx.java | 14 ++++++++ .../redisson/api/listener/MapPutListener.java | 36 +++++++++++++++++++ .../api/listener/MapRemoveListener.java | 36 +++++++++++++++++++ 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 redisson/src/main/java/org/redisson/api/listener/MapPutListener.java create mode 100644 redisson/src/main/java/org/redisson/api/listener/MapRemoveListener.java diff --git a/redisson/src/main/java/org/redisson/RedissonMap.java b/redisson/src/main/java/org/redisson/RedissonMap.java index 2f17dc05d..a43fa87d0 100644 --- a/redisson/src/main/java/org/redisson/RedissonMap.java +++ b/redisson/src/main/java/org/redisson/RedissonMap.java @@ -19,6 +19,8 @@ import io.netty.buffer.ByteBuf; import io.netty.util.ReferenceCountUtil; import org.redisson.api.*; import org.redisson.api.MapOptions.WriteMode; +import org.redisson.api.listener.MapPutListener; +import org.redisson.api.listener.MapRemoveListener; import org.redisson.api.mapreduce.RMapReduce; import org.redisson.client.RedisClient; import org.redisson.client.codec.Codec; @@ -1888,4 +1890,28 @@ public class RedissonMap extends RedissonExpirable implements RMap { writeBehindService.stop(getRawName()); } } + + @Override + public int addListener(ObjectListener listener) { + if (listener instanceof MapPutListener) { + return addListener("__keyevent@*:hset", (MapPutListener) listener, MapPutListener::onPut); + } + if (listener instanceof MapRemoveListener) { + return addListener("__keyevent@*:hdel", (MapRemoveListener) listener, MapRemoveListener::onRemove); + } + return super.addListener(listener); + } + + @Override + public RFuture addListenerAsync(ObjectListener listener) { + if (listener instanceof MapPutListener) { + return addListenerAsync("__keyevent@*:hset", (MapPutListener) listener, MapPutListener::onPut); + } + if (listener instanceof MapRemoveListener) { + return addListenerAsync("__keyevent@*:hdel", (MapRemoveListener) listener, MapRemoveListener::onRemove); + } + return super.addListenerAsync(listener); + } + + } diff --git a/redisson/src/main/java/org/redisson/api/RMap.java b/redisson/src/main/java/org/redisson/api/RMap.java index 6dbe19c11..391167bf8 100644 --- a/redisson/src/main/java/org/redisson/api/RMap.java +++ b/redisson/src/main/java/org/redisson/api/RMap.java @@ -622,5 +622,18 @@ public interface RMap extends ConcurrentMap, RExpirable, RMapAsync> entrySet(int count); - + + /** + * Adds object event listener + * + * @see org.redisson.api.listener.MapPutListener + * @see org.redisson.api.listener.MapRemoveListener + * @see org.redisson.api.ExpiredObjectListener + * @see org.redisson.api.DeletedObjectListener + * + * @param listener object event listener + * @return listener id + */ + int addListener(ObjectListener listener); + } diff --git a/redisson/src/main/java/org/redisson/api/RMapAsync.java b/redisson/src/main/java/org/redisson/api/RMapAsync.java index 48f6e8cee..0e15a41fa 100644 --- a/redisson/src/main/java/org/redisson/api/RMapAsync.java +++ b/redisson/src/main/java/org/redisson/api/RMapAsync.java @@ -418,4 +418,17 @@ public interface RMapAsync extends RExpirableAsync { */ RFuture clearAsync(); + /** + * Adds object event listener + * + * @see org.redisson.api.listener.MapPutListener + * @see org.redisson.api.listener.MapRemoveListener + * @see org.redisson.api.ExpiredObjectListener + * @see org.redisson.api.DeletedObjectListener + * + * @param listener - object event listener + * @return listener id + */ + RFuture addListenerAsync(ObjectListener listener); + } diff --git a/redisson/src/main/java/org/redisson/api/RMapReactive.java b/redisson/src/main/java/org/redisson/api/RMapReactive.java index 0ae790e8f..39356eaa7 100644 --- a/redisson/src/main/java/org/redisson/api/RMapReactive.java +++ b/redisson/src/main/java/org/redisson/api/RMapReactive.java @@ -619,4 +619,17 @@ public interface RMapReactive extends RExpirableReactive { */ RLockReactive getLock(K key); + /** + * Adds object event listener + * + * @see org.redisson.api.listener.MapPutListener + * @see org.redisson.api.listener.MapRemoveListener + * @see org.redisson.api.ExpiredObjectListener + * @see org.redisson.api.DeletedObjectListener + * + * @param listener object event listener + * @return listener id + */ + Mono addListener(ObjectListener listener); + } diff --git a/redisson/src/main/java/org/redisson/api/RMapRx.java b/redisson/src/main/java/org/redisson/api/RMapRx.java index 27076ef20..399fd0767 100644 --- a/redisson/src/main/java/org/redisson/api/RMapRx.java +++ b/redisson/src/main/java/org/redisson/api/RMapRx.java @@ -622,4 +622,18 @@ public interface RMapRx extends RExpirableRx { */ RLockRx getLock(K key); + /** + * Adds object event listener + * + * @see org.redisson.api.listener.MapPutListener + * @see org.redisson.api.listener.MapRemoveListener + * @see org.redisson.api.ExpiredObjectListener + * @see org.redisson.api.DeletedObjectListener + * + * @param listener object event listener + * @return listener id + */ + Single addListener(ObjectListener listener); + + } diff --git a/redisson/src/main/java/org/redisson/api/listener/MapPutListener.java b/redisson/src/main/java/org/redisson/api/listener/MapPutListener.java new file mode 100644 index 000000000..f03dc88bf --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/listener/MapPutListener.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2013-2022 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.api.listener; + +import org.redisson.api.ObjectListener; + +/** + * Redisson Object Event listener for hset event published by Redis. + *

+ * Redis notify-keyspace-events setting should contain Eh letters + * + * @author Nikita Koksharov + */ +public interface MapPutListener extends ObjectListener { + + /** + * Invoked when entry added to RMap object + * + * @param name object name + */ + void onPut(String name); + +} diff --git a/redisson/src/main/java/org/redisson/api/listener/MapRemoveListener.java b/redisson/src/main/java/org/redisson/api/listener/MapRemoveListener.java new file mode 100644 index 000000000..7ac509d64 --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/listener/MapRemoveListener.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2013-2022 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.api.listener; + +import org.redisson.api.ObjectListener; + +/** + * Redisson Object Event listener for hdel event published by Redis. + *

+ * Redis notify-keyspace-events setting should contain Eh letters + * + * @author Nikita Koksharov + */ +public interface MapRemoveListener extends ObjectListener { + + /** + * Invoked when entry removed from RMap object + * + * @param name object name + */ + void onRemove(String name); + +}