package com.gyee.frame.util.ticket;

import com.gyee.frame.model.ticket.TokenUser;
import org.springframework.stereotype.Component;

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

@Component
public class TokenCache {

    private Map<String, TokenUser> tokenMap = new HashMap<>();

    public TokenUser getToken(String tokenId) {
        TokenUser data = null;
        if (tokenMap.containsKey(tokenId)) {
            data = tokenMap.get(tokenId);
        }
        return data;
    }

    public boolean saveToken(String tokenId, TokenUser token) {
        if (tokenMap.containsKey(tokenId)){
            return false;
        }
        tokenMap.put(tokenId, token);

        return  true;
    }

    public boolean removeToken(String tokenId){
       if (tokenMap.containsKey(tokenId)){
           tokenMap.remove(tokenId);
           return true;
       }
       return false;
    }

    public TokenUser findToken(String tokenId){
        TokenUser token = null;
        if (tokenMap.containsKey(tokenId))
            token = tokenMap.get(tokenId);

        return token;
    }
}
