All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.yourinspiration.jexpresso.session.InMemoryStore Maven / Gradle / Ivy

package de.yourinspiration.jexpresso.session;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Simple implementation for {@link SessionStore} that stores the session data
 * in memory. The data will be lost after restarting the application.
 * 
 * @author Marcel Härle
 *
 */
public class InMemoryStore implements SessionStore {

    private final ConcurrentHashMap> sessions = new ConcurrentHashMap<>();

    @SuppressWarnings("unchecked")
    @Override
    public  T get(final String key, final String sessionId, final Class clazz) {
        Map sessionData = sessions.get(sessionId);
        if (sessionData == null) {
            sessionData = new HashMap<>();
            sessions.put(sessionId, sessionData);
        }
        return (T) sessionData.get(key);
    }

    @Override
    public void set(final String key, final Serializable value, final String sessionId) {
        Map sessionData = sessions.get(sessionId);
        if (sessionData == null) {
            sessionData = new HashMap<>();
        }
        sessionData.put(key, value);
        sessions.put(sessionId, sessionData);
    }

    @Override
    public long size() {
        return sessions.size();
    }

    @Override
    public void clear() {
        sessions.clear();
    }

    @Override
    public void clear(String sessionId) {
        sessions.remove(sessionId);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy