package com.qmth.exam.reserve.cache.impl;

import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.cache.RedisClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 登录用户会话管理
 */
@Component
public class LoginSessionManager {

    /**
     * 用户登录会话缓存过期时间（分钟）
     */
    public static final long SESSION_TIMEOUT = 60;

    /**
     * 用户登录会话缓存前缀
     */
    public static final String CACHE_SESSION_PREFIX = "$SESSION:";

    /**
     * 用户登录标识_{userId}
     */
    public static final String USER_LOGIN = "U_";

    /**
     * 学生登录标识_{studentId}
     */
    public static final String STUDENT_LOGIN = "S_";

    @Resource
    private RedisClient redisClient;

    public LoginUser getLoginSession(String sessionId) {
        return redisClient.get(CACHE_SESSION_PREFIX + sessionId, LoginUser.class, SESSION_TIMEOUT, TimeUnit.MINUTES);
    }

    public void addLoginSession(LoginUser session) {
        redisClient.set(CACHE_SESSION_PREFIX + session.getSessionId(), session, SESSION_TIMEOUT, TimeUnit.MINUTES);
    }

    public void removeLoginSession(String sessionId) {
        redisClient.delete(CACHE_SESSION_PREFIX + sessionId);
    }

}
