缓存限定登录次数

package com.yjf.borncrm.web.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.yjf.borncrm.common.util.DateUtil;
import com.yjf.borncrm.common.util.StringUtil;

@Component("cacheUtil")
public class CacheUtil {

public static Map<String,Map<String,String>> loginCacheMap=new HashMap<String, Map<String,String>>();
/**
* 写登录记录到缓存,返回登录剩余次数
* @param name
* @return
*/
public long writeCache(String name){
if(null==loginCacheMap.get(name)){//第一次登录
Map<String,String> map=new HashMap<String,String>();
map.put("count", 2+"");
map.put("loginTime", DateUtil.formatDateTime(new Date()));
map.put("loginFlag", "Y");
loginCacheMap.put(name, map);
return 2;
}else{//非第一次登录
Map<String,String> map=loginCacheMap.get(name);
Date date=DateUtil.parseDateTime(map.get("loginTime"));
long count=Long.valueOf(map.get("count"));
String flag=map.get("loginFlag");
if("N".equals(flag)){//锁状态:判断时间,超过一个小时,重新计算count,没超过,不让登录
if(addOneHour(date)){//超过一个小时
map.put("count", 2+"");
map.put("loginTime", DateUtil.formatDateTime(new Date()));
map.put("loginFlag", "Y");
loginCacheMap.put(name, map);
return 2;
}else{
return 0;
}
}else if("Y".equals(flag)){//没锁状态:判断时间,超过一个小时,从头计算count,未超过一个小时,继续计算count
if(addOneHour(date)){//超过一个小时
map.put("count", 2+"");
map.put("loginTime", DateUtil.formatDateTime(new Date()));
map.put("loginFlag", "Y");
loginCacheMap.put(name, map);
return 2;
}else{
if(count<=0){//三次登录失败,帐号标记为锁定
map.put("loginFlag", "N");
map.put("count", count+"");
map.put("loginTime", DateUtil.formatDateTime(new Date()));
loginCacheMap.put(name, map);
return count;
}else{
map.put("count", count-1+"");
if(count-1==0){
map.put("loginFlag", "N");
}else{
map.put("loginFlag", "Y");
}

map.put("loginTime", DateUtil.formatDateTime(new Date()));
loginCacheMap.put(name, map);
return count-1;
}
}
}


}
return 0;
}

//检查状态,返回true,允许登录,false不允许登录
public boolean checkStatus(String name){
if(!StringUtil.isEmpty(name)){
if(null!=loginCacheMap.get(name)){
Map<String,String> map=loginCacheMap.get(name);
Date date=DateUtil.parseDateTime(map.get("loginTime"));
long count=Long.valueOf(map.get("count"));
String flag=map.get("loginFlag");
if(addOneHour(date)){
return true;
}else{
if("Y".equals(flag)){
return true;
}else{
return false;
}
}
}
}
return true;
}
//清登录记录
public void clearLoginCache(String name){
if(!StringUtil.isEmpty(name)){
loginCacheMap.remove(name);
}
}
//获取用户上次登录时间
public Date findLastLoginTime(String name){
if(!StringUtil.isEmpty(name)){
if(null!=loginCacheMap.get(name)){
return DateUtil.parseDateTime(loginCacheMap.get(name).get("loginTime"));
}
}
return null;
}
//获取用户登录次数
public long findLoginCount(String name){
if(!StringUtil.isEmpty(name)){
if(null==loginCacheMap.get(name)){
return 2;
}else{
return Long.valueOf( loginCacheMap.get(name).get("count"));
}
}
return 0;
}
/**
* 判断date加一个小时后与当前时间的大小
* 小于当前时间,返回true
* 大于当前时间,返回false
* @param date
* @return
*/
public boolean addOneHour(Date date){
if(null!=date){
Calendar ca=Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.HOUR_OF_DAY, 1);
//ca.add(Calendar.MINUTE,1);//加一分钟,测试用的
Date dateNow=new Date();
if(ca.getTime().getTime()<dateNow.getTime()){
return true;
}else{
return false;
}
}
return true;
}
// @Autowired
// private YshPurchaseCategoryInfoService yshPurchaseCategoryInfoService;
// @Autowired
// private CrmYshCustomService crmYshCustomService;
// //缓存
// public static Map<String,List<String>> cacheMap=new HashMap<String, List<String>>();
//
// public void LoadAreaCache(){
// List<String> areaList= crmYshCustomService.searchDistinctArea();//crmYshCustomService
// List<String> catgroyList=yshPurchaseCategoryInfoService.searchBigCatgroy();
// List<String> teamNameList= crmYshCustomService.searchDistinctWholseSaleTeamName();
// cacheMap.put("areaList", areaList);
// cacheMap.put("catgroyList", catgroyList);
// cacheMap.put("teamNameList", teamNameList);
// }
// //地区
// public List<String> getAreaCache(){
// List<String> areaList=cacheMap.get("areaList");
// if(null==areaList || areaList.isEmpty()){
// LoadAreaCache();
// areaList=cacheMap.get("areaList");
// }
//
// return areaList;
// }
// //采购大类
// public List<String> getCatgroyCache(){
// List<String> catgroyList=cacheMap.get("catgroyList");
// if(null==catgroyList || catgroyList.isEmpty()){
// LoadAreaCache();
// catgroyList=cacheMap.get("catgroyList");
// }
//
// return catgroyList;
//
//
// }
// //团队所属
// public List<String> getTeamNameCache(){
// List<String> teamNameList=cacheMap.get("teamNameList");
// if(null==teamNameList || teamNameList.isEmpty()){
// LoadAreaCache();
// teamNameList=cacheMap.get("teamNameList");
// }
//
// return teamNameList;
// }
//
// //清楚缓存指定缓存,key为null 则清除所有缓存
// public void clearCache(String key){
// if(null==key){
// cacheMap.remove("teamNameList");
// cacheMap.remove("catgroyList");
// cacheMap.remove("areaList");
// }else{
// cacheMap.remove(key);
// }
//
// }
}

posted @ 2015-09-23 17:54  萧雪痕  阅读(546)  评论(0编辑  收藏  举报