• 总访问量,日访问量,周访问量统计代码


    这个方法用于实现页面访问统计, 不足之处还望各位原油指正

    总访问量,日访问量,周访问量

    代码中使用了Linq

            /// <summary>
            
    /// 更新案例访问量
            
    /// </summary>
            
    /// <param name="ID">案例ID</param>
            public static void UpdateViewCount(int ID)
            {

                var ctx 
    = LinqDB.LQProvider.GetLQDB();
                LinqDB.Photo_Categories pc 
    = ctx.Photo_Categories.SingleOrDefault(p => p.ID == ID);
                
    //如果没有取到对象则跳出
                if (pc==null)
                {
                    
    return;
                }
                
    //获取当前Session 
                HttpSessionState session = HttpContext.Current.Session;

                
    string key = "ViewCount_Photo_Categories";

                List
    <int> ids = new List<int>();
                
    //创建一个列表对象保存当前用户浏览过的相册ID集合
                
    //从Session中读取列表
                if (session[key] is List<int>)
                {
                    ids 
    = session[key] as List<int>;
                }
                
    //如果列表中不存在则添加并保存至session 并更新访问量
                if (!ids.Contains(ID))
                {
                    ids.Add(ID);
                    session[key] 
    = ids;
                    
    //最后访问时间
                    DateTime lastViewTime = pc.LastViewTime ?? DateTime.Now;
                    
    //当前时间与最后访问时间的差
                    TimeSpan deffer = DateTime.Now - lastViewTime;
                    
    //访问总数
                    int ViewCount = pc.ViewCount ?? 0;
                    
    //周访问
                    int WeekViewCount = pc.WeekViewCount ?? 0;
                    
    //日访问
                    int DayViewCount = pc.DayViewCount ?? 0;
                    
    //如果相差天数大于7天
                    if (deffer.TotalDays > 7)
                    {
                        
    //重设周访问量
                        WeekViewCount = 1;
                    }

                    
    else //不大于7天 
                    {
                        
    //如果相差时间大于24小时
                        if (deffer.TotalHours > 24d)
                        {
                            DayViewCount 
    = 1;
                        }
                        
    else//如果在24小时之内
                        {
                            
    //日访问增加
                            DayViewCount++;
                        }
                        
    //周访问量增加
                        WeekViewCount++;
                    }
                    
    //总访问量增加
                    ViewCount++;

                    pc.ViewCount 
    = ViewCount;
                    pc.WeekViewCount 
    = WeekViewCount;
                    pc.DayViewCount 
    = DayViewCount;
                    pc.LastViewTime 
    = lastViewTime;
                    ctx.SubmitChanges();
    //提交数据库
                }
            }
  • 相关阅读:
    hdu 1312 ( Red and Black )
    hdu 1429 ( 胜利大逃亡(续) )
    zjut 小X的苹果
    hdu 1253 ( 胜利大逃亡 )
    许多事
    1198 ( Farm Irrigation )
    hdu 1241 Oil Deposits
    hdu 1242 ( Rescue )
    hdu 1240 ( Asteroids! )
    zoj2966 build the electric system
  • 原文地址:https://www.cnblogs.com/Qbit/p/1765773.html
Copyright © 2020-2023  润新知