package com.jfinal.club.util;

import com.jfinal.club.vo.UserSessionVo;

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

/**
 * @descriptions: session 缓存
 * @author: lc
 * @date: 2020-05-09 19:30:44
 * @Version: 1.0
 **/
public class CacheSessionUtil {

    private static CacheSessionUtil instance;
    private HashMap< String, UserSessionVo > sessionMap;

    private CacheSessionUtil( ) {
        sessionMap = new HashMap<>( );
    }

    public static CacheSessionUtil getInstance( ) {
        if (instance == null) {
            instance = new CacheSessionUtil( );
        }
        return instance;
    }

    public synchronized void setSession(String sessionId, UserSessionVo session) {
        if (session != null) {
            sessionMap.put(sessionId, session);
        }
    }

    public synchronized void delSession(String sessionId) {
        if (sessionId != null) {
            sessionMap.remove(sessionId);
        }
    }

    public synchronized UserSessionVo getSession(String sessionID) {
        if (sessionID == null) {
            return null;
        }
        return sessionMap.get(sessionID);
    }

}
