⬆️ ⬇️

Caching data in LINQ-to-SQL, incl. and null

I want to share with the community about a couple of classes that I use in my ASP.NET projects. One is a wrapper for the Cache object, the other contains extensible methods for caching the results of LINQ-to-SQL queries.



So, the task: to cache LINQ-to-SQL queries.



What should be the result:

1) an expanding method for caching a collection of records (after performing ToList ())

2) an expanding method for caching one record (FirstOrDefault ()),

3) a generalizing method for working with a cache, which allows to get records and lead to the specified type

3) NULL caching capability.

')





Problems:

1) it is impossible to write NULL in Cache

2) FirstOrDefault () can return not only Nullable types, but also value types (int, double, etc.)



So, let's write a class for working with the cache:

public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  1. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  2. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  3. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  4. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  5. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  6. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  7. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  8. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  9. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  10. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  11. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  12. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  13. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  14. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  15. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  16. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  17. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  18. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  19. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  20. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  21. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  22. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  23. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  24. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  25. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  26. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  27. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  28. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  29. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  30. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  31. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  32. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  33. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  34. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  35. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  36. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
  37. public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public static class CacheStorage { internal static object Null = new Object(); public static object Get( string index) { return HttpContext .Current.Cache.Get(index); } public static CacheItem<T> Get<T>( string key) { CacheItem<T> result = new CacheItem<T> { Cached = false , Item = default (T) }; object obj = Get(key); if (obj != null ) { result.Cached = true ; result.Item = obj == CacheStorage.Null ? default (T) : (T)obj; } return result; } public static void Put( string key, object value ) { if ( value == null ) { value = CacheStorage.Null; } int expirationMinutes = 2; HttpContext .Current.Cache.Insert(key, value , null , DateTime .Now.AddMinutes(expirationMinutes), TimeSpan .Zero); } } public class CacheItem<T> { public T Item { get ; set ; } public bool Cached { get ; set ; } } * This source code was highlighted with Source Code Highlighter .


The simplest methods are getting the data from the cache and writing:

  1. public static object Get ( string index)
  2. {
  3. return HttpContext .Current.Cache.Get (index);
  4. }
  5. public static void Put ( string key, object value )
  6. {
  7. if ( value == null )
  8. {
  9. value = CacheStorage.Null;
  10. }
  11. int expirationMinutes = 2;
  12. HttpContext .Current.Cache.Insert (key, value , null , DateTime .Now. AddMinutes (expirationMinutes), TimeSpan. Zero);
  13. }
* This source code was highlighted with Source Code Highlighter .


The recording method, of course, needs some work on your application, since I did it as much as possible (the settings need to be brought out in the configuration, to make support for various caching policies). You probably noticed that in the Put method there is a condition that checks for the null value that we want to put in the cache. When this condition is met, we substitute this value for the stub,

  1. internal static object Null = new Object ();
* This source code was highlighted with Source Code Highlighter .


which is not null, but in our context it would be null :)



More interesting is the generalized method for retrieving data from the cache. It returns not just an object of the type we need, but a CacheItem object, which contains, in fact, the return value and the caching flag:

  1. public static CacheItem <T> Get <T> ( string key)
  2. {
  3. CacheItem <T> result = new CacheItem <T>
  4. {
  5. Cached = false ,
  6. Item = default (T)
  7. };
  8. object obj = Get (key);
  9. if (obj! = null )
  10. {
  11. result.Cached = true ;
  12. result.Item = obj == CacheStorage.Null? default (T): (T) obj;
  13. }
  14. return result;
  15. }
* This source code was highlighted with Source Code Highlighter .


where CacheItem is

  1. public class CacheItem <T>
  2. {
  3. public T Item { get ; set ; }
  4. public bool Cached { get ; set ; }
  5. }
* This source code was highlighted with Source Code Highlighter .


That is, if the cache is null, then we return the caching flag false, and the object is returned as the default value (default (T)). If the cache is not null, then the caching flag will be true, and the object will be the default if the cache stores our null stub, or an object that has been cast to the desired type.

In general, I somehow wonderfully described the work, in the code, I think, it is easier to understand.



Now the class with methods for caching LINQ-to-SQL queries:

  1. public static class QueryResultCache
  2. {
* This source code was highlighted with Source Code Highlighter .


The first method caches the ToList () call:

  1. public static List <T> FromCache <T> ( this IQueryable <T> query,
  2. params object [] parameters)
  3. {
  4. string key = QueryResultCache.GetKey (parameters);
  5. List <T> result = CacheStorage.Get (key) as List <T>;
  6. if (result == null )
  7. {
  8. result = query.ToList ();
  9. CacheStorage.Put (key, result);
  10. }
  11. return result;
  12. }
* This source code was highlighted with Source Code Highlighter .


The second method caches the call to FirstOrDefault ():

  1. public static T FirstOrDefaultFromCache <T> ( this IQueryable <T> query,
  2. params object [] parameters)
  3. {
  4. string key = QueryResultCache.GetKey (parameters);
  5. CacheItem <T> result = CacheStorage.Get <T> (key);
  6. if (! result.Cached)
  7. {
  8. result.Item = query.FirstOrDefault ();
  9. CacheStorage.Put (key, result.Item);
  10. }
  11. return result.Item;
  12. }
* This source code was highlighted with Source Code Highlighter .


In this method, shamanism is used with the CacheItem class (note: for ToList () this is not necessary, because this method always returns a collection; in the absence of data, it will be of zero length)

For placement in the cache, you need a unique key that can be generated in MD5, for example, by gluing the parameters passed to the caching method:

  1. private static string GetKey ( object [] parameters)
  2. {
  3. return GetMD5 ( string .Join ( string .Empty, parameters.Select (x => x.ToString ()). ToArray ()));
  4. }
  5. private static string GetMD5 ( string input)
  6. {
  7. MD5 md5Hasher = MD5.Create ();
  8. byte [] data = md5Hasher.ComputeHash ( Encoding .Default.GetBytes (input));
  9. StringBuilder sBuilder = new StringBuilder ();
  10. for ( int i = 0; i <data.Length; i ++)
  11. {
  12. sBuilder.Append (data [i] .ToString ( "x2" ));
  13. }
  14. return sBuilder.ToString ();
  15. }
  16. }
* This source code was highlighted with Source Code Highlighter .


Now you can try these classes in action. Let's create a simple application, and cache the List, string and int values. We start the SQL profiler and see that within 2 minutes the SQL server is resting.



All source codes are available here (.net 4.0, you need to register your connectionString in web.config. SQL to create the database is in the DB.sql file).



Comments and criticism is welcome.

Source: https://habr.com/ru/post/106958/



All Articles