package com.red.circle.order.infra.database.rds.service.order.impl;

import com.red.circle.component.redis.service.RedisService;
import com.red.circle.order.infra.database.rds.key.OrderKey;
import com.red.circle.order.infra.database.rds.service.order.OrderCacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 订单锁缓存服务.
 *
 * @author lixiaofei on 2024/09/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderCacheServiceImpl implements OrderCacheService {

  private final RedisService redisService;

  @Override
  public boolean checkLock(String orderId) {
    return redisService.hash(OrderKey.LOCK.getKey(orderId));
  }

  @Override
  public boolean lock(String orderId) {
    return redisService.setIfAbsent(OrderKey.LOCK.getKey(orderId), 1, 20, TimeUnit.MINUTES);
  }

  @Override
  public void unLock(String orderId) {
    redisService.delete(OrderKey.LOCK.getKey(orderId));
  }

}
