I want to create cache in java to store user sessions. It’s something like a cache which will store for example 5 elements for every user. I need some kind of java data structure which must be able to remember this data. So far I created this Java code:
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class SessionCache {
public SessionCache() {
}
/* Create object to store sessions */
private List<ActiveSessionsObj> dataList = new ArrayList<>();
public static class ActiveSessionsObj {
private int one;
private int two;
private int three;
private int four;
private int five;
private ActiveSessionsObj(int one, int two, int three, int four, int five) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
public List<ActiveSessionsObj> addCache(int one, int two, int three, int four, int five){
dataList.add(new ActiveSessionsObj(
one,
two,
three,
four,
five));
return dataList;
}
}
I’m new to java and I need a help how I can add data to the structure and how I can remove data from the structure. I need to do this using a key. Is this possible? Or is there more appropriate data structure to store the data according to mu needs?
Best wishes
Presumably each user has a unique id, so a
Mapimplementation seems like a sensible choice where the key is the user id and the value isActiveSessionsObj:See Javadoc for adding (
put()) and removing (remove()) elements from aMap: