package cn.sikey.websocket.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Component;

/**
 * @Author: nelson
 * @Date: 2025/5/29
 * @Description: 使用 Redis 构建的消息ID递增适配器
 */
@Slf4j
@Component
public class MessageIdIncrementingServiceImpl implements MessageIdIncrementingService<Long> {
    /**
     * Redis incr 的 key <br/>
     * 消息ID通过 Redis 生成
     */
    private static final String MESSAGE_ID_INCREMENTING_KEY = "message.mcdisk.id";

    private final RedisOperations<String, String> redisOperations;

    @Autowired
    public MessageIdIncrementingServiceImpl(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);
    }

}
