All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.babyfish.jimmer.sql.cache.redisson.RedissonCacheTracker Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
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
                )
        );
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy