org.jetlinks.supports.cluster.ClusterLocalCache Maven / Gradle / Ivy
The newest version!
package org.jetlinks.supports.cluster;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.jetlinks.core.cluster.ClusterCache;
import org.jetlinks.core.cluster.ClusterManager;
import org.jetlinks.core.cluster.ClusterTopic;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Collection;
public class ClusterLocalCache extends AbstractLocalCache {
private final ClusterTopic clearTopic;
public ClusterLocalCache(String name, ClusterManager clusterManager) {
this(name, clusterManager, clusterManager.createCache(name), CacheBuilder.newBuilder().build());
}
public ClusterLocalCache(String name,
ClusterManager clusterManager,
ClusterCache clusterCache,
Cache localCache) {
super(clusterCache, localCache);
this.clearTopic = clusterManager.getTopic("_local_cache_modify:".concat(name));
}
@Override
protected Mono onUpdate(K key, V value) {
return clearTopic
.publish(Mono.just(key))
.then();
}
@Override
protected Mono onRemove(K key) {
return clearTopic
.publish(Mono.just(key))
.then();
}
@Override
protected Mono onRemove(Collection extends K> key) {
return clearTopic
.publish(Flux.fromIterable(key))
.then();
}
@Override
protected Mono onClear() {
return clearTopic
.publish(Mono.just((K) "__all"))
.then();
}
}