Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 我还应该在SQL分析器中看到查询命中吗?_Sql Server_Asp.net Mvc_Linq To Sql_Caching - Fatal编程技术网

Sql server 我还应该在SQL分析器中看到查询命中吗?

Sql server 我还应该在SQL分析器中看到查询命中吗?,sql-server,asp.net-mvc,linq-to-sql,caching,Sql Server,Asp.net Mvc,Linq To Sql,Caching,我目前正在构建一个网站,我刚刚使用LinqToSQL实现了SqlCacheDependency,就像这样 public IQueryable<VictoryList> GetVictoryList() { string cacheKey = HttpContext.Current.User.Identity.Name + "victoryCacheKey"; IQueryable<VictoryList

我目前正在构建一个网站,我刚刚使用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;
   }
public IQueryable GetVictoryList()
{
字符串cacheKey=HttpContext.Current.User.Identity.Name+“victoryCacheKey”;
IQueryable cachednews=(IQueryable)HttpContext.Current.Cache.Get(cacheKey);
if(cachednews==null)
{
var results=来自_datacontext.viewVictoryList中的v
orderby_datacontext.GetNewId()
选择新的胜利列表
{
MemberID=v.MemberID,
用户名=v.Aspnetusername,
位置=v.位置,
DaimokuGoal=v.DaimokuGoal,
PreviewImageID=v.PreviewImageID,
TotalDaimoku=v.TotalDaimoku,
总测定值=v.总测定值,
DeterminationID=v.DeterminationID,
DeterminationName=v.DeterminationName
};
SqlCacheDependency依赖项=
新的SqlCacheDependency(_datacontext.GetCommand(results)作为SqlCommand);
HttpContext.Current.Cache.Add(cacheKey、结果、依赖项、DateTime.MaxValue、,
TimeSpan.Zero,CacheItemPriority.Normal,null);
返回results.ToList().AsQueryable();
}
返回缓存新闻;
}

它似乎在工作,因为速度明显加快,特别是在一些复杂的查询上。然而,当我在SQLProfiler中查看时,我仍然看到查询在运行,我使用的是SqlCacheDependency的CommandBroker模式。即使数据显然来自缓存对象,我是否仍应该看到该查询?

我认为问题在于您正在缓存中存储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;
}
public IQueryable GetVictoryList(){
// ...
if(cachednews==null)
{
var results=from/。。。
results=results.ToList().AsQueryable();//强制执行查询
SqlCacheDependency=/。。。;
HttpContext.Current.Cache.Add(cacheKey,
results,//现在只存储结果
附属国,
DateTime.MaxValue,
时间跨度0,
缓存项优先级,正常,
无效);
返回结果;
}
返回缓存新闻;
}

我认为问题在于您将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;
}
public IQueryable GetVictoryList(){
// ...
if(cachednews==null)
{
var results=from/。。。
results=results.ToList().AsQueryable();//强制执行查询
SqlCacheDependency=/。。。;
HttpContext.Current.Cache.Add(cacheKey,
results,//现在只存储结果
附属国,
DateTime.MaxValue,
时间跨度0,
缓存项优先级,正常,
无效);
返回结果;
}
返回缓存新闻;
}