Domanda

Now my responsibility in the project is a access count module. If user login in two hours repeatly ,it should be treat as once.

I use a concurrentHashMap to put the user id and access time.

private static Map<String,Date> loginTimeMap = new ConcurrentHashMap<String, Date>();

Every time the user access the index page , program will compare the time.

Date date = loginTimeMap.get(user.getSuUserId());
if(date==null||DateUtil.getHourInterval(new Date(),date)>=DefinedValue.LIMIT_TIME){
    accessCount=accessCount+1;
    loginTimeMap.put(user.getSuUserId(), new Date());
}

in the code LIMIT_TIME is a constant that refers two hours.

Will the loginTimeMap slow the server if the size of the map exceed 10000?

Really sorry for my poor English!

È stato utile?

Soluzione

Will the loginTimeMap slow the server if the size of the map exceed 10000?

A HashMap has a time complexity of O(1). That is, it hashes, then goes straight to the value. It does not search for the value. That means that it's performance is not proportional to the amount of elements in the array, although with 10,000 entries it might be somewhat memory heavy!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top