package com.red.circle.live.infra.database.cache.service.impl;

import com.red.circle.component.redis.service.RedisService;
import com.red.circle.live.infra.database.cache.key.LiveUserAgoraKey;
import com.red.circle.live.infra.database.cache.service.UserAgoraTokenCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * UserAgoraToken缓存服务.
 *
 * @author lixiaofei on 2024/9/4
 */
@Service
@RequiredArgsConstructor
public class UserAgoraTokenCacheServiceImpl implements UserAgoraTokenCacheService {

  private final RedisService redisService;
  private static final long CACHE_TIME = 3600;

  @Override
  public void remove(Long userId, Long roomId) {
    redisService.delete(getUserAgoraTokenKey(userId, roomId));
  }

  @Override
  public void save(String agoraToken, Long userId, Long roomId) {
    redisService.setIfAbsent(getUserAgoraTokenKey(userId, roomId), agoraToken, CACHE_TIME, TimeUnit.SECONDS);
  }

  @Override
  public String getUserAgoraToken(Long userId, Long roomId) {
    return redisService.getString(getUserAgoraTokenKey(userId, roomId));
  }

  private String getUserAgoraTokenKey(Long userId, Long roomId) {
    return LiveUserAgoraKey.USER_AGORA_TOKEN.getKey(userId, roomId);
  }

}
