Feature - add MapEntryListener in RedissonSpringCacheManager

Signed-off-by: seakider <seakider@gmail.com>
pull/6374/head
seakider 4 weeks ago
parent 046fca66bf
commit 989ddf7552

@ -15,11 +15,15 @@
*/
package org.redisson.spring.cache;
import org.redisson.api.map.event.MapEntryListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
@ -35,6 +39,8 @@ public class CacheConfig {
private long maxIdleTime;
private int maxSize;
private final List<MapEntryListener> listeners = new ArrayList<>();
/**
* Creates config object with
@ -103,6 +109,24 @@ public class CacheConfig {
this.maxIdleTime = maxIdleTime;
}
/**
* listener will invoke if one of the ttl,maxIdleTime,maxSize is set
* listener Is one of the following implementations:
* EntryCreatedListener
* EntryExpiredListener
* EntryRemovedListener
* EntryUpdatedListener
*
* @param listener listener
*/
public void addListener(MapEntryListener listener) {
listeners.add(listener);
}
protected List<MapEntryListener> getListeners() {
return listeners;
}
/**
* Read config objects stored in JSON format from <code>String</code>
*

@ -15,6 +15,14 @@
*/
package org.redisson.spring.cache;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.redisson.api.map.event.MapEntryListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -22,10 +30,6 @@ import java.io.Reader;
import java.net.URL;
import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
/**
*
* @author Nikita Koksharov
@ -33,9 +37,20 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
*/
public class CacheConfigSupport {
ObjectMapper jsonMapper = new ObjectMapper();
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
ObjectMapper jsonMapper = createMapper(null);
ObjectMapper yamlMapper = createMapper(new YAMLFactory());
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class")
public static class ClassMixIn {
}
private ObjectMapper createMapper(JsonFactory mapping) {
ObjectMapper mapper = new ObjectMapper(mapping);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.addMixIn(MapEntryListener.class, ClassMixIn.class);
return mapper;
}
public Map<String, CacheConfig> fromJSON(String content) throws IOException {
return jsonMapper.readValue(content, new TypeReference<Map<String, CacheConfig>>() {});
}

@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.api.map.event.MapEntryListener;
import org.redisson.client.codec.Codec;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.InitializingBean;
@ -269,6 +270,9 @@ public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderA
cache = oldCache;
} else {
map.setMaxSize(config.getMaxSize());
for (MapEntryListener listener : config.getListeners()) {
map.addListener(listener);
}
}
return cache;
}

Loading…
Cancel
Save