【问题标题】:Should I still see the query hit in SQL Profiler?我还应该在 SQL Profiler 中看到查询命中吗?
【发布时间】:2010-09-19 07:59:56
【问题描述】:

我目前正在构建一个网站,我刚刚使用 LinqToSQL 实现了 SqlCacheDependency。

   public IQueryable<VictoryList> GetVictoryList()
   {
                string cacheKey = HttpContext.Current.User.Identity.Name + "victoryCacheKey";
                IQueryable<VictoryList> cachednews = (IQueryable<VictoryList>)HttpContext.Current.Cache.Get(cacheKey);

                if (cachednews == null)
                {

                    var results = from v in _datacontext.ViewVictoryLists
                                  orderby _datacontext.GetNewId()
                                  select new VictoryList
                                  {
                                      MemberID = v.MemberID,
                                      Username = v.Aspnetusername,
                                      Location = v.Location,
                                      DaimokuGoal = v.DaimokuGoal,
                                      PreviewImageID = v.PreviewImageID,
                                      TotalDaimoku = v.TotalDaimoku,
                                      TotalDeterminations = v.TotalDeterminations,
                                      DeterminationID = v.DeterminationID,
                                      DeterminationName = v.DeterminationName
                                  };
                    SqlCacheDependency dependency =
                     new SqlCacheDependency(_datacontext.GetCommand(results) as SqlCommand);

                    HttpContext.Current.Cache.Add(cacheKey, results, dependency, DateTime.MaxValue,
                                  TimeSpan.Zero, CacheItemPriority.Normal, null); 

                    return results.ToList().AsQueryable();
                }
                return cachednews;
   }

它似乎正在工作,因为事情明显更快,尤其是在一些复杂的查询上,但是在 SQLProfiler 中查看内容时,我仍然看到查询运行,我使用的是 SqlCacheDependency 的 CommandBroker 模式。即使数据显然来自缓存对象,我是否仍然可以看到查询?

【问题讨论】:

    标签: sql-server asp.net-mvc linq-to-sql caching


    【解决方案1】:

    我认为问题在于您将 IQueryable 存储在缓存中,然后 cachednews 包含一个命中数据库的 IQueryable。

    尝试以下更改。

    public IQueryable<VictoryList> GetVictoryList() {
                // ...
                if (cachednews == null)
                {
    
                    var results = from // ...
                    results = results.ToList().AsQueryable(); // force query execution
    
                    SqlCacheDependency dependency = // ...;
                    HttpContext.Current.Cache.Add(cacheKey, 
                        results, // now just the result are stored
                        dependency, 
                        DateTime.MaxValue,
                        TimeSpan.Zero, 
                        CacheItemPriority.Normal, 
                        null); 
    
                    return results;
                }
                return cachednews;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多