views:

183

answers:

1

I am currently building a web site and I just implemented SqlCacheDependency using LinqToSQL like so.

   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;
   }

It appears to be working as things are noticbly faster especially on some complex queries, however while looking at things in SQLProfiler I still see the query run through, I'm using the CommandBroker mode of SqlCacheDependency. Should I still see the query even though the data is obviously coming from a cached object?

+4  A: 

I think that the problem is that you are storing IQueryable's in your cache, and then cachednews contains an IQueryable that hits the database.

Try the following changes.

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;
}
Brian J Cardiff
Yep that worked thanks a bunch....
dswatik