package com.huimv.video.dhicc.test.token;

import lombok.ToString;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * token存储方式为本地缓存
 */
@Component
@ToString
public class LocalTokenStore implements TokenStore {

    public static Map<String, Token> tokenMap;
    public static final Object lock = new Object();

    public LocalTokenStore(){}

    public static Map<String, Token> getTokenMap(){
        if(CollectionUtils.isEmpty(tokenMap)){
            synchronized (lock){
                if(CollectionUtils.isEmpty(tokenMap)){
                    tokenMap = new HashMap<String, Token>();
                }
            }
        }
        return tokenMap;
    }

    /**
     * 存储token
     * @param:key 键
     * @param:token 值
     * **/
    public void tokenStore(String key, Token token){
        tokenMap.put(key, token);
    }

    /**
     * 获取token
     * **/
    public Map<String, Token> getToken(){
        return tokenMap;
    }
}
