I have this code which will store user session credentials:
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class SessionCache
{
/**
* Default Constructor
*/
public SessionCache()
{
}
/**
* Map which which will store Connections Objects by connection_id
*/
private static final Map<String, ActiveConnections> cache = new LinkedHashMap<>();
/**
* Object which will store connection credentials
*/
public class ActiveConnections
{
private int one;
private int two;
private int three;
public ActiveConnections(int one, int two, int three)
{
this.one = one;
this.two = two;
this.three = three;
}
public int getOne()
{
return one;
}
public void setOne(int one)
{
this.one = one;
}
public int getTwo()
{
return two;
}
public void setTwo(int two)
{
this.two = two;
}
public int getThree()
{
return three;
}
public void setThree(int three)
{
this.three = three;
}
}
/**
* Get Connection credentials by connection_id
*
* @param connection_id
* @return
*/
public Object getCache(String connection_id)
{
return cache.get(connection_id);
}
/**
* Insert new Connection Object into the cache
*
* @param connection_id
* @param one
* @param two
* @param three
*/
public void addCache(String connection_id, int one, int two, int three)
{
/**
* @ You may want to check if entry already exists for connection depends on logic in your application.
* @ Otherwise this will replace any previous entry for connection_id
*/
if (!cache.containsKey(connection_id))
{
cache.put(connection_id, new ActiveConnections(one, two, three));
}
}
/**
* Remove Connection from the cache based on connection_id
*
* @param connection_id
*/
public void removeCache(String connection_id)
{
cache.remove(connection_id);
}
/**
* Get the number of all connections
*
* @return
*/
public int countCacheSize()
{
return cache.size();
}
/**
* Clear all cache
*/
public void flushCache()
{
cache.clear();
}
public List<String> sendAllKeys()
{
List<String> list = new ArrayList<>(cache.keySet());
return list;
}
public Object sendKeyValue(int key)
{
Object mapKey = cache.keySet().toArray()[key];
return mapKey;
}
}
Can you help me to improve this code? I would like to make a while cycle which searches for a value into the Map every 5 seconds for example expired session. Could you please help me to improve the code and make it lightweight as much as possible.