code_junkie

Should I still see the query hit in SQL Profiler?

Nov 11th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public IQueryable<VictoryList> GetVictoryList()
  2. {
  3. string cacheKey = HttpContext.Current.User.Identity.Name + "victoryCacheKey";
  4. IQueryable<VictoryList> cachednews = (IQueryable<VictoryList>)HttpContext.Current.Cache.Get(cacheKey);
  5.  
  6. if (cachednews == null)
  7. {
  8.  
  9. var results = from v in _datacontext.ViewVictoryLists
  10. orderby _datacontext.GetNewId()
  11. select new VictoryList
  12. {
  13. MemberID = v.MemberID,
  14. Username = v.Aspnetusername,
  15. Location = v.Location,
  16. DaimokuGoal = v.DaimokuGoal,
  17. PreviewImageID = v.PreviewImageID,
  18. TotalDaimoku = v.TotalDaimoku,
  19. TotalDeterminations = v.TotalDeterminations,
  20. DeterminationID = v.DeterminationID,
  21. DeterminationName = v.DeterminationName
  22. };
  23. SqlCacheDependency dependency =
  24. new SqlCacheDependency(_datacontext.GetCommand(results) as SqlCommand);
  25.  
  26. HttpContext.Current.Cache.Add(cacheKey, results, dependency, DateTime.MaxValue,
  27. TimeSpan.Zero, CacheItemPriority.Normal, null);
  28.  
  29. return results.ToList().AsQueryable();
  30. }
  31. return cachednews;
  32. }
  33.  
  34. public IQueryable<VictoryList> GetVictoryList() {
  35. // ...
  36. if (cachednews == null)
  37. {
  38.  
  39. var results = from // ...
  40. results = results.ToList().AsQueryable(); // force query execution
  41.  
  42. SqlCacheDependency dependency = // ...;
  43. HttpContext.Current.Cache.Add(cacheKey,
  44. results, // now just the result are stored
  45. dependency,
  46. DateTime.MaxValue,
  47. TimeSpan.Zero,
  48. CacheItemPriority.Normal,
  49. null);
  50.  
  51. return results;
  52. }
  53. return cachednews;
  54. }
Add Comment
Please, Sign In to add comment