/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.common.saas.cache;

import org.springframework.data.redis.core.RedisTemplate;

/**
 * 缓存租户、应用、数据源集群三者的关联关系
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
public class SaaSAppDataSourceClusterRedisRepository implements SaaSAppDataSourceClusterCache
{
  /**
   * Redis工具类
   */
  private final RedisTemplate<String, Object> redisTemplate;

  public SaaSAppDataSourceClusterRedisRepository(RedisTemplate<String, Object> redisTemplate)
  {
    this.redisTemplate = redisTemplate;
  }

  @Override
  public String findDataSourceClusterId(String tenantId, String appName)
  {
    String key = createKey(tenantId, appName);
    return (String) redisTemplate.opsForValue().get(key);
  }

  @Override
  public void save(String tenantId, String appName, String clusterName)
  {
    String key = createKey(tenantId, appName);
    redisTemplate.opsForValue().set(key, clusterName);
  }

  private String createKey(String tenantId, String appName)
  {
    return "UITEST-CCTP-T-A-DSC:" + tenantId + ":" + appName;
  }
}
