package com.sikey.wa04.business.infrastructure.adapter;

import com.sikey.wa04.business.domain.adapter.MessageIdIncrementingAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Component;

/**
 * 使用 Redis 构建的消息ID递增适配器
 *
 * @author luoyangwei
 */
@Slf4j
@Component
public class RedisMessageIdIncrementingAdapter implements MessageIdIncrementingAdapter<Long> {
    /**
     * Redis incr 的 key <br/>
     * 消息ID通过 Redis 生成
     */
    private static final String MESSAGE_ID_INCREMENTING_KEY = "message.id";

    private final RedisOperations<String, String> redisOperations;

    @Autowired
    public RedisMessageIdIncrementingAdapter(RedisOperations<String, String> redisOperations) {
        this.redisOperations = redisOperations;
    }

    @Override
    public Long incr() {
        return redisOperations.opsForValue().increment(MESSAGE_ID_INCREMENTING_KEY);
    }

    @Override
    public Long incr(String key) {
        return redisOperations.opsForValue().increment(key);
    }

}
