package org.eco.common.tenant.manager;

import org.eco.common.core.constant.GlobalConstants;
import org.eco.common.core.utils.StringUtils;
import org.eco.common.redis.manager.FlexSpringCacheManager;
import org.eco.common.tenant.helper.TenantHelper;
import org.springframework.cache.Cache;

/**
 * 重写 cacheName 处理方法 支持多租户
 *
 * @author wgk
 */
public class TenantSpringCacheManager extends FlexSpringCacheManager {

    public TenantSpringCacheManager() {
    }

    @Override
    public Cache getCache(String name) {
        if (StringUtils.contains(name, GlobalConstants.GLOBAL_REDIS_KEY)) {
            return super.getCache(name);
        }
        Long tenantId = TenantHelper.getTenantId();
        if (StringUtils.startsWith(name, tenantId + "")) {
            // 如果存在则直接返回
            return super.getCache(name);
        }
        return super.getCache(tenantId + ":" + name);
    }

}
