package com.yunyan.business.common.redis;



import com.yunyan.common.constant.CacheConstants;
import com.yunyan.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主要作用就是:接收过期的redis消息,获取到key,key就是内容信息,然后去更新内容的审核状态
 */
@Slf4j
@Transactional
//@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {



    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    @Override
    public void onMessage(Message message, byte[] pattern) {
        String messageStr = message.toString();

        if(StringUtils.isBlank(messageStr)) {
            log.debug("过期 redis key " + messageStr);
            return;
        }
        log.debug("过期的是key: " + messageStr);

        String expiredKey = message.toString();
        String[] str = expiredKey.split(CacheConstants.CACHE_KEY_SEPARATE);
        if(str.length == CacheConstants.AUDIT_EXPIRE_KEY_LENGTH && CacheConstants.AUDIT_EXPIRE_KEY.equals(str[0])){
            log.info("TopicMessageListener#onMessage audit expire type {}, id {}", str[1], str[2]);
        }
    }
}
