package com.bootdo.system.service.impl;

import com.bootdo.system.service.TokenCachService;
import org.springframework.stereotype.Service;

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

/**
 * sso token cach impl
 *
 * @author Mr.xiBel
 * @date 2022/4/4
 **/
@Service
public class TokenCachServiceImpl implements TokenCachService {

    private static final Map<String, String> TOKEN_MAP = new HashMap<String, String>();

    @Override
    public String saveTokenCach(String jsessionid, String token) {
        TOKEN_MAP.put(jsessionid, token);
        return token;
    }

    @Override
    public void deleteTokenCach(String jsessionid) {
        processMap(TOKEN_MAP, jsessionid);
    }

    @Override
    public String getTokenCach(String jsessionid) {
        String token = TOKEN_MAP.get(jsessionid);
        deleteTokenCach(jsessionid);
        return token;
    }

    public static void processMap(Map<String, String> tokenMap, String jsessionid) {
        Set<String> strings = tokenMap.keySet();
        strings.removeIf(next -> next.equals(jsessionid));
    }
}
