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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
-
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 .
最も簡単な方法は、キャッシュからデータを取得して書き込むことです。
- パブリック 静的 オブジェクト Get( 文字列インデックス)
- {
- return HttpContext .Current.Cache.Get(インデックス);
- }
- public static void Put( 文字列キー、 オブジェクト 値 )
- {
- if ( value == null )
- {
- 値 = CacheStorage.Null;
- }
- int expirationMinutes = 2;
- HttpContext .Current.Cache.Insert(key、 value 、 null 、 DateTime .Now.AddMinutes(expirationMinutes)、 TimeSpan .Zero);
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
もちろん、記録方法にはアプリケーションの改良が必要です。なぜなら、 可能な限り簡略化しました(さまざまなキャッシュポリシーをサポートするために、構成で設定を取り出す必要があります)。 おそらく、Putメソッドには、キャッシュするNULL値をチェックする条件があることに気づいたでしょう。 この条件が満たされると、この値をスタブに置き換えます。
- 内部 静的 オブジェクト Null = new Object();
*このソースコードは、 ソースコードハイライターで強調表示されました。
nullではありませんが、このコンテキストではnullのようなものです:)
キャッシュからデータを取得するための一般化された方法は、より興味深いようです。 必要なタイプのオブジェクトだけでなく、実際には戻り値とキャッシュの符号を含むCacheItemオブジェクトを返します。
- public static CacheItem <T> Get <T>( 文字列キー)
- {
- CacheItem <T>結果= 新しい CacheItem <T>
- {
- キャッシュ= false
- アイテム= デフォルト (T)
- };
- オブジェクト obj = Get(キー);
- if (obj!= null )
- {
- result.Cached = true ;
- result.Item = obj == CacheStorage.Null? デフォルト (T):(T)obj;
- }
- 結果を返す ;
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
cacheitemはどこですか
- パブリック クラス CacheItem <T>
- {
- パブリック Tアイテム{ get ; セット ; }
- パブリック boolキャッシュ{ get ; セット ; }
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
つまり、キャッシュにnullがある場合、キャッシュフラグfalseを返し、オブジェクトをデフォルト値(デフォルト(T))として返します。 キャッシュがnullでない場合、キャッシュフラグはtrueになり、nullスタブがキャッシュに保存されている場合、または目的の型にキャストされたオブジェクトが保存されている場合、オブジェクトはデフォルトになります。
一般に、コード内で作業をなんとなくトリッキーに説明しましたが、理解しやすいと思います。
LINQ-to-SQLクエリをキャッシュするメソッドを持つクラス:
- パブリック 静的 クラス QueryResultCache
- {
*このソースコードは、 ソースコードハイライターで強調表示されました。
最初のメソッドは、ToList()の呼び出しをキャッシュします。
- public static List <T> FromCache <T>( この IQueryable <T>クエリ、
- params オブジェクト []パラメーター)
- {
- string key = QueryResultCache.GetKey(パラメーター);
- List <T> result = CacheStorage.Get(key) as List <T>;
- if (結果== null )
- {
- result = query.ToList();
- CacheStorage.Put(キー、結果);
- }
- 結果を返す ;
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
2番目のメソッドは、FirstOrDefault()への呼び出しをキャッシュします。
- public static T FirstOrDefaultFromCache <T>( この IQueryable <T>クエリ、
- params オブジェクト []パラメーター)
- {
- string key = QueryResultCache.GetKey(パラメーター);
- CacheItem <T> result = CacheStorage.Get <T>(キー);
- if (!result.Cached)
- {
- result.Item = query.FirstOrDefault();
- CacheStorage.Put(key、result.Item);
- }
- return result.Item;
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
このメソッドは、CacheItemクラスでシャーマニズムを使用します(注:ToList()の場合、このメソッドは常にコレクションを返すため、これは不要です。データがない場合、長さはゼロになります)
キャッシュに配置するには、たとえばキャッシュメソッドに渡されたパラメーターを接着することにより、MD5で生成できる一意のキーが必要です。
- プライベート 静的 文字列 GetKey( オブジェクト []パラメーター)
- {
- return GetMD5( string .Join( string .Empty、parameters.Select(x => x.ToString())。ToArray()));
- }
- プライベート 静的 文字列 GetMD5( 文字列入力)
- {
- MD5 md5Hasher = MD5.Create();
- byte [] data = md5Hasher.ComputeHash( Encoding .Default.GetBytes(入力));
- StringBuilder sBuilder = new StringBuilder ();
- for ( int i = 0; i <data.Length; i ++)
- {
- sBuilder.Append(data [i] .ToString( "x2" ));
- }
- return sBuilder.ToString();
- }
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
これで、これらのクラスを実際に試すことができます。 単純なアプリケーションを作成して、List、string、intの値をキャッシュしましょう。 SQLプロファイラーを起動して、SQLサーバーが2分間停止していることを確認します。
すべてのソースは
ここから入手でき
ます (.net 4.0、web.configではconnectionStringを登録する必要があります。データベース作成用のSQLはDB.sqlファイルにあります)。
コメントと批判を歓迎します。