1、访问对象(VisitDto):
private String ip; // 访问IP
private String visitDate; // 访问时间
private String visitMode; //访问方式:post get ...
private String visitUrl; //访问路径
private String userBroset; //访问用户的浏览器
private String userOs; //访问用户的操作系统
2、统计方法,并将数据放到缓存中:
public static void counts(HttpServletRequest request){
try{
VisitDto dto = new VisitDto();
String ip = IpUtil.getIpAddr(request);// ip地址
String key = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
key = UP_COUNT_KEY + key;
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());// 访问时间
String mode = request.getMethod();// 访问方式 Post Get
String agent = request.getHeader("user-agent").toLowerCase();
//得到用户的浏览器名
String userbrowser = getBrowserName(agent);
//得到用户的操作系统名
String useros = getOsName(agent);
String url = request.getRequestURI();// 访问路径
String id = request.getSession().getId();
dto.setIp(ip);
dto.setVisitDate(date);
dto.setVisitMode(mode);
dto.setVisitUrl(url);
dto.setUserBroset(userbrowser);
dto.setUserOs(useros);
if(StringUtils.isNotBlank(id)){
JSONObject jsonObject = JSONObject.fromObject(dto);
mm.put(id, jsonObject.toString());
JedisSessionClient.hmset(key, mm, (int) TimeUnit.DAYS.toSeconds(30));
}
}catch(Exception e){
log.error(e.getMessage(),e);
}
}
/**
* 获取浏览器版本信息
* @Title: getBrowserName
* >>Browser:Chrome > FF > IE > ...
* @param agent
* @return
*/
public static String getBrowserName(String agent) {
if(StringUtils.isBlank(agent)) {
return null;
}
if(agent.indexOf("msie 7")>0){
return "ie7";
}else if(agent.indexOf("msie 8")>0){
return "ie8";
}else if(agent.indexOf("msie 9")>0){
return "ie9";
}else if(agent.indexOf("msie 10")>0){
return "ie10";
}else if(agent.indexOf("msie")>0){
return "ie";
}else if(agent.indexOf("opera")>0){
return "opera";
}else if(agent.indexOf("chrome")>0){
return "chrome";
}else if(agent.indexOf("firefox")>0){
return "firefox";
}else if(agent.indexOf("webkit")>0){
return "webkit";
}else if(agent.indexOf("gecko")>0 && agent.indexOf("rv:11")>0){
return "ie11";
}else{
return "Others";
}
}
/**
* 获取操作系统信息
* @Title: getOsName
* >>操作系统:Windows > 苹果 > 安卓 > Linux > ...
* @param agent
* @return
*/
public static String getOsName(String userAgent){
if (StringUtils.isBlank(userAgent)) {
return null;
}
if (userAgent.indexOf("windows")>0) {//主流应用靠前
return "Windows";
} else if (userAgent.indexOf("mac os x") >0) {
return "Mac OS X";
}else if(userAgent.indexOf("android") >0){
return "Android";
}else if(userAgent.indexOf("liunx") >0){
return "Liunx";
}else{
return "Others";
}
}
3、缓存方法:
public class JedisSessionClient {
private static JedisPool poolMaster;
private JedisSessionClient() {
}
private static Logger log = LoggerFactory.getLogger(JedisSessionClient.class);
static{
try {
Properties p = new Properties();
// String filePath = new JedisClient().getClass().getResource("/").getPath() + "redisConfig.properties";
// log.info("filePath:" + filePath);
// InputStream is = new BufferedInputStream(new FileInputStream(filePath));
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("/redisConfig.properties");
p.load(is);
String redisSession = p.getProperty("redisSession").trim();
// password = p.getProperty("password");
int redisPort = Integer.parseInt(p.getProperty("redisPort").trim());
int timeout = Integer.parseInt(p.getProperty("timeout").trim());
JedisPoolConfig configMaster = new JedisPoolConfig();
configMaster.setMaxTotal(30000);// 设置最大连接数
configMaster.setMaxIdle(100); // 设置最大空闲数
configMaster.setMinIdle(10);
configMaster.setMaxWaitMillis(10000);// 设置超时时间
configMaster.setTestWhileIdle(false);
configMaster.setTestOnBorrow(false);
poolMaster = new JedisPool(configMaster, redisSession,redisPort, timeout);
log.info(">>>>>>>>>redisSession ip:" + redisSession);
log.info(">>>>>>>>>redisSession port:"+redisPort);
log.info(">>>>>>>>>redisSession timeout:"+timeout);
log.info(">>>>>>>>>会话管理redis master连接池初始化成功...");
} catch (Exception e) {
log.info(">>>>>>>>>会话管理redis master连接池初始化失败,系统无法正常使用!!!");
e.printStackTrace();
}
}
private static Jedis getJedis() throws Exception {
return poolMaster.getResource();
}
private static void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.disconnect();
poolMaster.returnResourceObject(jedis);
}
}
public static void hmset(String key,Map<String,String> map,int seconds) throws Exception {
if (null == key || "".equals(key.trim())) {
throw new Exception("the key can not be null.");
}
Jedis jedisMaster = null;
try {
jedisMaster = getJedis();
jedisMaster.hmset(key, map);
if(seconds > 0){
jedisMaster.expire(key, seconds);
}
} finally {
returnResource(jedisMaster);
}
}
public static Set<String> hkeys(String key)throws Exception {
if (null == key || "".equals(key.trim())) {
throw new Exception("the key can not be null.");
}
Jedis jedisMaster = null;
Set<String> set = null;
try {
jedisMaster = getJedis();
set = jedisMaster.hkeys(key);
} finally {
returnResource(jedisMaster);
}
return set;
}
public static List<String> hvals(String key)throws Exception {
if (null == key || "".equals(key.trim())) {
throw new Exception("the key can not be null.");
}
Jedis jedisMaster = null;
List<String> list = null;
try {
jedisMaster = getJedis();
list = jedisMaster.hvals(key);
} finally {
returnResource(jedisMaster);
}
return list;
}
}
4、从缓存中获取对应key,value:
List<VisitDto> vList = new ArrayList<VisitDto>();
List<String> list = new ArrayList<String>();
Set<String> set = JedisSessionClient.hkeys(key);:获取所有key
if(!set.isEmpty()){
for (String str : set) {
VisitDto dto = new VisitDto();
dto.setIp(str);
dto.setVisitDate(date);
vList.add(dto);
}
}
JedisSessionClient.hvals(kye) 获取所有value
list = JedisSessionClient.hvals(key);
if(null !=list && list.size()>0){
for(int i=0;i<list.size();i++){
VisitDto dto = new VisitDto();
JSONObject jsonObj = JSONObject.fromObject(list.get(i));
dto = (VisitDto)JSONObject.toBean(jsonObj,VisitDto.class);
vList.add(dto);
}
}