1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * 
 */
package com.vci.server.framework.Logon;
 
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
 
import com.vci.corba.framework.data.UserInfo;
 
/**
 * @author Jason
 * 登录信息Session管理
 */
public class LogonSessionManager {
 
    /**
     * 缓存Map对象
     */
    private static ConcurrentHashMap<String, LogonSession> mapSession = new ConcurrentHashMap<String, LogonSession>();
    
    /**
     * 登录失效时间,默认为3分钟
     */
    private static long _timeExpire = 3 * 60 * 1000;
 
    /**
     * 创建定时任务每分钟清理一次缓存
     */
    static{
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                refresh();
            }
        },0,60000);
    }
 
    /**
     * 缓存刷新,清除过期数据
     */
    public static void refresh(){
        for (String key : mapSession.keySet()) {
            if(isExpire(key)){
                remove(key);
            }
        }
    }
 
    /**
     * 加入缓存
     * @param key
     * @param value
     */
    public static boolean put(String token, UserInfo user){
        if(token.isEmpty()){
            return false;
        }
        LogonSession session = new LogonSession(user);
        mapSession.put(token, session);
        return true;
    }
 
    /**
     * 移除缓存数据
     * @param key
     */
    public static boolean remove(String token){
        if(token.isEmpty()){
            return false;
        }
        if(!mapSession.containsKey(token)){
            return true;
        }
        mapSession.remove(token);
        return true;
    }
 
    /**
     * 获取缓存数据
     * @param key
     * @return
     */
    public static Object get(String key){
        if(key.isEmpty()||isExpire(key)){
            return null;
        }
        LogonSession cacheEntity = mapSession.get(key);
        if(null == cacheEntity){
            return null;
        }
        return cacheEntity.getValue();
    }
 
    /**
     * 判断当前数据是否已过期
     * @param key
     * @return
     */
    private static boolean isExpire(String token){
        if(token.isEmpty()){
            return false;
        }
        if(mapSession.containsKey(token)){
            LogonSession cacheEntity = mapSession.get(token);
            long currentTime = System.currentTimeMillis();
            long accTime = cacheEntity.getAccessTime();
            if ((currentTime - accTime) > _timeExpire){
                return true;
            }
            return false;
        }
        return false;
    }
 
    /**
     * 获取当前缓存大小(包含已过期但未清理的数据)
     * @return
     */
    public static int getCacheSize(){
        return mapSession.size();
    }
}