org.babyfish.jimmer.sql.cache.redisson.RedissonCacheTracker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.cache.redisson;
import org.babyfish.jimmer.sql.cache.spi.AbstractCacheTracker;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.BaseStatusListener;
import org.redisson.api.listener.MessageListener;
import java.util.UUID;
public class RedissonCacheTracker extends AbstractCacheTracker {
private static final String CHANNEL = "_jimmer_:invalidate";
private final UUID trackerId = UUID.randomUUID();
private final RTopic topic;
public RedissonCacheTracker(RedissonClient redissonClient) {
topic = redissonClient.getTopic(CHANNEL);
topic.addListener(InvalidateMessage.class, new MessageListener() {
@Override
public void onMessage(CharSequence channel, InvalidateMessage msg) {
if (!msg.trackerId.equals(trackerId)) { // "Eq" means same JVM
firer().invalidate(msg.toEvent());
}
}
});
topic.addListener(new BaseStatusListener() {
@Override
public void onSubscribe(String channel) {
firer().reconnect();
}
});
}
@Override
protected void publishInvalidationEvent(InvalidateEvent event) {
topic.publish(
new InvalidateMessage(
trackerId,
event
)
);
}
}