summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionCache.java
blob: b7d78f112015a70f6bcc8bf3a8555eee8ea1c65b (plain) (blame)
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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.session;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A session cache that can store any type of {@link Session}.
 *
 * @author Ulf Lilleengen
 * @author hmusum
 */
public class SessionCache<SESSIONTYPE extends Session> {

    private final HashMap<Long, SESSIONTYPE> sessions = new HashMap<>();

    public synchronized void addSession(SESSIONTYPE session) {
        sessions.putIfAbsent(session.getSessionId(), session);
    }

    synchronized void removeSession(long id) {
        sessions.remove(id);
    }

    /**
     * Gets a Session
     *
     * @param id session id
     * @return a session belonging to the id supplied, or null if no session with the id was found
     */
    public synchronized SESSIONTYPE getSession(long id) {
        return sessions.get(id);
    }

    public synchronized List<SESSIONTYPE> getSessions() {
        return new ArrayList<>(sessions.values());
    }
    
}