结合redis实现后台统计(今日访客/总访客)

时间:2024-04-16 21:14:02

一、实现今日访客

1、本项目redis的配置

  #redis相关的信息
  redis:
    host: 127.0.0.1
    port: 6381

2、controller实现数据的记录

//获取访客数量
    @RequestMapping("selectIp")
    public String test(HttpServletRequest request){
    //获取ip
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        System.out.println("-----"+ip);
        /*获取当前日期*/
        Calendar calendar = new GregorianCalendar();
        String day = (calendar.get(Calendar.HOUR_OF_DAY)*60+ calendar.get(Calendar.MINUTE))+"";

        /*获取总访问次数*/
        String num = redisTemplate.opsForValue().get("ipAdress");
        //如果ip不为空
        if(ip != null){
            //redis中没有这个值 或者 今日访问时间小于当前时间说明已访问
            if(!redisTemplate.hasKey("iptime_" + ip) ||
                   Integer.parseInt(day) < Integer.parseInt(redisTemplate.opsForValue().get("iptime_"+ip)))
            {
                //添加一个iptime_本机ip,值为当前分钟时间,设置生命周期为一天
                redisTemplate.opsForValue().set("iptime_" + ip, day, 1,TimeUnit.DAYS);
                //重新赋值访问次数
                num = redisTemplate.opsForValue().increment("ipAdress")+"";
            }
        }
        //返回总访客
        return num;
    }

3、前端页面加载事件调用接口(在不同的服务需要通过feignCilent来调用方法)

<script>
$(function(){
/*获取访问网页ip*/
  $.post("/admin/selectIp",function(rs){
      $("#num").html(rs);
   },"json");
});
</script>

用户访问:访问量加1,记录ip和访问时间

二、实现总访客

1、创建一张记录总访客的表

 2、service接口的方法

public interface ICountvisitorsService extends IService<T_countvisitors> {
    //更新网站访客数量
    boolean updateVisitors(T_countvisitors countvisitors) throws Exception;
    //查询网站的访问量
    T_countvisitors findCount(T_countvisitors countvisitors) throws  Exception;
}

3、serviceimpl实现类的代码

@Service
public class CountvisitorsServiceImpl extends ServiceImpl<ICountvisitorsMapper, T_countvisitors> 
implements ICountvisitorsService { //更新网站访客数量 @Override public boolean updateVisitors(T_countvisitors countvisitors) throws Exception { //添加修改时间 countvisitors.setUpdatetime(new Date()); return getBaseMapper().updateById(countvisitors) > 0 ? true : false; } //查询网站的访问量 @Override public T_countvisitors findCount(T_countvisitors countvisitors) throws Exception { return getBaseMapper().selectById(countvisitors.getCid()); }

4、编写一个定时任务,在每天凌晨一点的时候执行(将redis中的今日访客加上总访客)

@Configuration
@EnableScheduling
public class MyCountTask {
    @Autowired
    private ICountvisitorsService countvisitorsService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //设置每天凌晨1点执行
    @Scheduled(cron="0 0 1 * * ?")
    public void CountVisitors() throws Exception {

        //清空当天数据
        if(redisTemplate.hasKey("ipAdress")){
            T_countvisitors countvisitors = new T_countvisitors();
            //修改更新时间
            countvisitors.setUpdatetime(new Date());
            //获取数据库中的对象
            T_countvisitors countvisitors2 = countvisitorsService.findCount(countvisitors);

            //从redis中获取数据,存放在数据库
            countvisitors.setNum(Integer.parseInt(redisTemplate.opsForValue().get("ipAdress"))+countvisitors2.getNum());
            //删除redis中的今日访客
            redisTemplate.delete("ipAdress");
            //执行修改数据,保存总访客
            countvisitorsService.updateVisitors(countvisitors);
        }
    }

5、返回今日访客与总访客的controller

//显示今日访问页面的人数、总访问页面的人数
    @RequestMapping("pageCount")
    public JSONResult pageCount() throws Exception {
        JSONResult<String> jsonResult = new JSONResult<>();
        T_countvisitors countvisitors = countvisitorsService.findCount(new T_countvisitors());
        List<String> listNum = new ArrayList<>();
        listNum.add(redisTemplate.opsForValue().get("ipAdress"));
        listNum.add(countvisitors.getNum() + "");
        jsonResult.setRecords(listNum);
        jsonResult.setCode(20000);
        return jsonResult;
    }

实现每周、每月、访客地区数据--需要创建多一些表