package com.sptg.third.biz.listener;

import com.sptg.third.api.feigns.grid.GridAuthFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    //网格token缓存名字
    public static String GRID_TOKEN_CACHE = "sptg:third:grid:token";
    @Autowired
    private GridAuthFeign gridAuthFeign;

    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String keyStr = message.toString();
        if(keyStr.startsWith(GRID_TOKEN_CACHE)) {
            this.gridAuthFeign.gridAuth();
        }
    }
}
