LINQ-to-SQLでのデータのキャッシュ、含む。 ヌル

ASP.NETプロジェクトで使用するいくつかのクラスをhabrasocietyと共有したいと思います。 1つはCacheオブジェクトのラッパーで、もう1つはLINQ-to-SQLクエリ結果をキャッシュするための拡張メソッドを含んでいます。



そのため、タスク:LINQ-to-SQLクエリをキャッシュします。



結果はどうなりますか:

1)レコードのコレクションをキャッシュするための拡張メソッド(ToList()の実行後)

2)1つのレコードをキャッシュするための拡張メソッド(FirstOrDefault())、

3)キャッシュを操作するための一般化された方法。レコードを取得し、指定されたタイプに導くことができます。

3)NULLをキャッシュする機能。







問題は次のとおりです。

1)キャッシュにNULLを書き込むことはできません

2)FirstOrDefault()は、Nullable型だけでなく、値型(int、doubleなど)も返すことができます。



そこで、キャッシュを操作するためのクラスを作成しましょう。

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 .





最も簡単な方法は、キャッシュからデータを取得して書き込むことです。





  1. パブリック 静的 オブジェクト Get( 文字列インデックス)
  2. {
  3. return HttpContext .Current.Cache.Get(インデックス);
  4. }
  5. public static void Put( 文字列キー、 オブジェクト
  6. {
  7. ifvalue == null
  8. {
  9. = CacheStorage.Null;
  10. }
  11. int expirationMinutes = 2;
  12. HttpContext .Current.Cache.Insert(key、 valuenullDateTime .Now.AddMinutes(expirationMinutes)、 TimeSpan .Zero);
  13. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


もちろん、記録方法にはアプリケーションの改良が必要です。なぜなら、 可能な限り簡略化しました(さまざまなキャッシュポリシーをサポートするために、構成で設定を取り出す必要があります)。 おそらく、Putメソッドには、キャッシュするNULL値をチェックする条件があることに気づいたでしょう。 この条件が満たされると、この値をスタブに置き換えます。





  1. 内部 静的 オブジェクト Null = new Object();
*このソースコードは、 ソースコードハイライターで強調表示されました。


nullではありませんが、このコンテキストではnullのようなものです:)



キャッシュからデータを取得するための一般化された方法は、より興味深いようです。 必要なタイプのオブジェクトだけでなく、実際には戻り値とキャッシュの符号を含むCacheItemオブジェクトを返します。





  1. public static CacheItem <T> Get <T>( 文字列キー)
  2. {
  3. CacheItem <T>結果= 新しい CacheItem <T>
  4. {
  5. キャッシュ= false
  6. アイテム= デフォルト (T)
  7. };
  8. オブジェクト obj = Get(キー);
  9. if (obj!= null
  10. {
  11. result.Cached = true ;
  12. result.Item = obj == CacheStorage.Null? デフォルト (T):(T)obj;
  13. }
  14. 結果を返す ;
  15. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


cacheitemはどこですか





  1. パブリック クラス CacheItem <T>
  2. {
  3. パブリック Tアイテム{ get ; セット ; }
  4. パブリック boolキャッシュ{ get ; セット ; }
  5. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


つまり、キャッシュにnullがある場合、キャッシュフラグfalseを返し、オブジェクトをデフォルト値(デフォルト(T))として返します。 キャッシュがnullでない場合、キャッシュフラグはtrueになり、nullスタブがキャッシュに保存されている場合、または目的の型にキャストされたオブジェクトが保存されている場合、オブジェクトはデフォルトになります。

一般に、コード内で作業をなんとなくトリッキーに説明しましたが、理解しやすいと思います。



LINQ-to-SQLクエリをキャッシュするメソッドを持つクラス:





  1. パブリック 静的 クラス QueryResultCache
  2. {
*このソースコードは、 ソースコードハイライターで強調表示されました。


最初のメソッドは、ToList()の呼び出しをキャッシュします。





  1. public static List <T> FromCache <T>( この IQueryable <T>クエリ、
  2. params オブジェクト []パラメーター)
  3. {
  4. string key = QueryResultCache.GetKey(パラメーター);
  5. List <T> result = CacheStorage.Get(key) as List <T>;
  6. if (結果== null
  7. {
  8. result = query.ToList();
  9. CacheStorage.Put(キー、結果);
  10. }
  11. 結果を返す ;
  12. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


2番目のメソッドは、FirstOrDefault()への呼び出しをキャッシュします。





  1. public static T FirstOrDefaultFromCache <T>( この IQueryable <T>クエリ、
  2. params オブジェクト []パラメーター)
  3. {
  4. string key = QueryResultCache.GetKey(パラメーター);
  5. CacheItem <T> result = CacheStorage.Get <T>(キー);
  6. if (!result.Cached)
  7. {
  8. result.Item = query.FirstOrDefault();
  9. CacheStorage.Put(key、result.Item);
  10. }
  11. return result.Item;
  12. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


このメソッドは、CacheItemクラスでシャーマニズムを使用します(注:ToList()の場合、このメソッドは常にコレクションを返すため、これは不要です。データがない場合、長さはゼロになります)

キャッシュに配置するには、たとえばキャッシュメソッドに渡されたパラメーターを接着することにより、MD5で生成できる一意のキーが必要です。





  1. プライベート 静的 文字列 GetKey( オブジェクト []パラメーター)
  2. {
  3. return GetMD5( string .Join( string .Empty、parameters.Select(x => x.ToString())。ToArray()));
  4. }
  5. プライベート 静的 文字列 GetMD5( 文字列入力)
  6. {
  7. MD5 md5Hasher = MD5.Create();
  8. byte [] data = md5Hasher.ComputeHash( Encoding .Default.GetBytes(入力));
  9. StringBuilder sBuilder = new StringBuilder ();
  10. forint i = 0; i <data.Length; i ++)
  11. {
  12. sBuilder.Append(data [i] .ToString( "x2" ));
  13. }
  14. return sBuilder.ToString();
  15. }
  16. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


これで、これらのクラスを実際に試すことができます。 単純なアプリケーションを作成して、List、string、intの値をキャッシュしましょう。 SQLプロファイラーを起動して、SQLサーバーが2分間停止していることを確認します。



すべてのソースはここから入手できます (.net 4.0、web.configではconnectionStringを登録する必要があります。データベース作成用のSQLはDB.sqlファイルにあります)。



コメントと批判を歓迎します。



All Articles