package com.gyee.frame.shiro.config;


import com.gyee.frame.model.custom.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 token, TokenUser user) {
        if (tokenMap.containsKey(token)){
            return false;
        }
        tokenMap.put(token, user);

        return  true;
    }

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

    public TokenUser findUserByToken(String token){
        TokenUser user = null;
        if (tokenMap.containsKey(token))
            user = tokenMap.get(token);

        return user;
    }
}
