ASP.NET WebFormsキャッシュは、一時的に運用データを維持するために非常に便利に実装されました。 .NETプラットフォームの初期には、開発者はWinFormsアプリケーションであっても、 System.Webネームスペースの操作によく使用されます。
ASP.NETキャッシュに関する記事で常に提供されているコードテンプレートは、非常にシンプルで実用的です。
// try to get an instance of object from cache
DataSet ds = HttpRuntime . Cache [ "KeyName" ] as DataSet ;
// check the result and recreate it, if it is null
if ( ds == null )
{
ds = QueryDataFromDatabase ( ) ;
HttpRuntime . Cache . Insert ( "KeyName" , ds ) ;
}
// using the instance of object that has been populated from cache or from storage
DataRow dr = ds . Tables [ 0 ] . Rows [ 0 ] ;ただし、ASP.NETキャッシュの実装には、望ましい機能は含まれていません。
キャッシュに値を追加するcache.insert(string、object)メソッドは、ほとんどの場合に適していますが、Web.configファイルに保存されているWebアプリケーションの設定に基づいてデータをキャッシュするために異なる設定を塗りつぶすことが望ましいことがよくあります。この場合、ソースコードを変更してはならず、再コンパイルなしでアプリケーションを実行する必要があります。
ASP.NETキャッシュは、インスタンスの実際のタイプに関係なく、常にobjectクラスのインスタンスを返します。ほとんどの場合、これは問題ではありません。これは、 Value types代わりにNullable typesを使用できるためです。おそらく、キャッシュからデータを取得するための一般的な方法が必要です。
// this is a dafault extraction data from cache
myClassName item = HttpRuntime . Cache [ "Key1" ] as myClassName ;
// this is a desired extraction data with generic methods
myClassName item = DataCache . Get < myClassName > ( "Key1" ) ; 一般的な方法が満たされていない場合は、デフォルトバージョンを使用することができます。実際、これらの2つのオプションは同一です。しかし、一般的な方法は追加機能を提供します。たとえば、デフォルト値は、キャッシュに取得する値が含まれていないかどうかを定義できます。
myClassName defaultValue = new myClassName ( /* init properties */ ) ;
myClassName item = DataCache . Get < myClassName > ( "Key1" , defaultValue ) ;
/* if there is nothing in the cache, then the item will be defaultValue */ASP.NETキャッシュは、常にキャッシュに保存されているオブジェクトを返します。これは、抽出されたオブジェクトの任意のプロパティを変更すると、これらは同じオブジェクトであるため、キャッシュに格納されているオブジェクトも変更されることを意味します。場合によっては、取得したオブジェクトを変更することもできますが、キャッシュのオブジェクトを変更せずに残します。便利な機能の1つは、キャッシュ内のオブジェクトを心配することなく変更できるキャッシュからオブジェクトのコピーを取得することです。
ASP.NETキャッシュは、 NameValueCollectionに似ています。小さなWebアプリケーションにはシンプルでエレガントなソリューションですが、アプリケーションが成長するとキーを生成することは困難になります。たとえば、ベンダーやモデルなど、2つの関連テーブルをキャッシュすることを検討してください。この場合、Webアプリケーションは、モデルテーブルの関連するレコード用の1つのキャッシュオブジェクトと多くのキャッシュされたオブジェクトのベンダーテーブルからすべてのデータを保持します。ベンダーの鍵は、 Vendorsであると思われるベンダーであり、 Models.[VendorID] 。ベンダーテーブルが変更された場合、ベンダーのインスタンスをキャッシュし、モデルの関連インスタンスのみをキャッシュから削除する必要があります。これは、データをキャッシュし、地域ごとにキャッシュから削除する必要があることを意味します。
説明されている機能はオプションであり、Webアプリケーションで簡単に実装できます。プロキシテンプレートは、説明されているすべての機能を実装するための適切なオプションです。結果のライブラリは、バージョン2.0を開始するすべての.NETフレームワークで使用できます。 .NET Framework 4.0は、 System.Runtime.Cachingネームスペースと新しいキャッシュモデルを備えたいくつかのクラスを導入します。したがって、標準のASP.NETキャッシュとその改善を使用する必要はありません。
悲しいが本当。
/// <summary>
/// Is cache used or not
/// </summary>
public static bool IsCacheEnable ;
/// <summary>
/// How to store objects in the cache
/// </summary>
public static CacheExpirationType ExpirationType ;
/// <summary>
/// How long objects should be stored in the cache
/// </summary>
public static TimeSpan ExpirationTime ; enum CacheExpirationType 、オブジェクトをキャッシュに保存する方法を定義します
/// <summary>
/// How to store objects in the cache
/// </summary>
public enum CacheExpirationType
{
/// <summary>
/// Without time limit, the value of the <seealso cref = "DataCache.ExpirationTime" /> property will be ignored
/// </summary>
NoExpiration ,
/// <summary>
/// The <seealso cref="DataCache.ExpirationTime"/> at which the inserted object expires and is removed from the cache
/// </summary>
AbsoluteExpiration ,
/// <summary>
/// The interval <seealso cref="DataCache.ExpirationTime"/> between the time the inserted object is last accessed and the time at which that object expires
/// </summary>
SlidingExpiration
} /// <summary>
/// Retrieves the specified item from the Cache object
/// </summary>
/// <typeparam name="T">The type for the cache item to retrieve</typeparam>
/// <param name="key">The identifier for the cache item to retrieve</param>
/// <param name="defaultValue">The default value if the object is not in the cache</param>
/// <returns>The retrieved cache item, or <paramref name="defaultValue"/> if the key is not found</returns>
public static T GetData < T > ( string key , T defaultValue = default ( T ) )
/// <summary>
/// Retrieves the deep copied of specified item from the Cache object
/// </summary>
/// <typeparam name="T">The type for the cache item to retrieve</typeparam>
/// <param name="key">The identifier for the cache item to retrieve</param>
/// <param name="defaultValue">The default value if the object is not in the cache</param>
/// <returns>The retrieved cache item, or <paramref name="defaultValue"/> if the key is not found</returns>
public static T GetDeepCopiedData < T > ( string key , T defaultValue = default ( T ) ) /// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using default values provided by the settings
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
public static void InsertData ( string key , object value )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the absolute expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertAbsoluteExpirationData ( string key , object value , TimeSpan expirationTime )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the sliding expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertSlidingExpirationData ( string key , object value , TimeSpan expirationTime )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and default value for expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationType">How to store objects in the cache</param>
public static void InsertExpirationData ( string key , object value , CacheExpirationType expirationType )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationType">How to store objects in the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertExpirationData ( string key , object value , CacheExpirationType expirationType , TimeSpan expirationTime ) /// <summary>
/// Removes the specified item from the application's cache
/// </summary>
/// <param name="key">An identifier for the cache item to remove</param>
public static void RemoveDataByKey ( string key )
/// <summary>
/// Removes all items from the application's cache that starts with key
/// </summary>
/// <param name="keyStartsWith">An starts with identifier for the cache item to remove</param>
public static void RemoveAllDataByKey ( string keyStartsWith )
/// <summary>
/// Removes all items from the application's cache
/// </summary>
public static void RemoveAllData ( ) public class Global : System . Web . HttpApplication
{
protected void Application_Start ( object sender , EventArgs e )
{
Settings settings = Settings . Default ;
DataCache . IsCacheEnable = settings . IsCacheEnable ;
DataCache . ExpirationType = settings . ExpirationType ;
DataCache . ExpirationTime = settings . ExpirationTime ;
}
} public partial class DefaultPage : System . Web . UI . Page
{
public const string STR_CACHENAME = "something" ;
protected void Page_Load ( object sender , EventArgs e )
{
if ( ! this . IsPostBack )
{
SomethingDataModel val = DataCache . GetData < SomethingDataModel > ( STR_CACHENAME ) ;
if ( val == null )
{
SomethingDataModel val = new SomethingDataModel ( ) ; // get data from ...
DataCache . InsertData ( STR_CACHENAME , val ) ;
}
DataBind ( val ) ; // use data on the page
}
}
}キャッシュでデフォルトで指定されていることとは異なる時間設定を定義する場合は、オーバーライドされたInsert{Which}Dataメソッドの1つを使用します。
public partial class DefaultPage : System . Web . UI . Page
{
public const string STR_CACHENAME = "something" ;
protected void Page_Load ( object sender , EventArgs e )
{
if ( this . IsPostBack )
{
DAL . Update ( ) ; // update the database ...
DataCache . RemoveAllDataByKey ( STR_CACHENAME ) ; // clear cache
Response . Redirect ( "Default.aspx" ) ; // proccessing the request
}
}
} 質問がありますか?私に連絡してください、そして私はあなたがそれを整理するのを手伝います。
<style> .inner {min-width:800px!fality;最大幅:60%!重要;} </style>