Public class MyCache
{
/// <Summary>
/// Insert a cache
/// </Summary>
/// <Param name = "name"> cache key </param>
/// <Param name = "value"> cache value </param>
/// <Param name = "isAbsoulute"> whether to use absolute expiration time; true if yes; false if not </param>
/// <Param name = "isOverride"> If the cache exists, whether to overwrite the cache value </param>
/// <Param name = "absoluteDate"> absolute expiration time </param>
/// <Param name = "spacing"> relative expiration interval </param>
/// <Param name = "dep"> cache dependency </param>
/// <Param name = "removeddCallback"> delegate called when the cache is removed </param>
/// <Param name = "priority"> cache priority </param>
Public static void Insert (string name, object value, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, cacheitempriorpriority)
{
Cache cache = HttpContext. Current. Cache;
// If the cache for this key already exists
If (cache [name]! = Null)
{
// If the cache value is overwritten
If (isOverride)
{
Cache. Remove (name );
InsertInternal (name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority );
}
Else
{
Return;
}
}
InsertInternal (name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority );
}
/// <Summary>
/// Obtain the cache based on the key name. If the cache does not exist, call the delegate that returns the cache value.
/// </Summary>
Public static object GetCache (string name, Func <object> func, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, revoke removeddCallback, CacheItemPriority priority)
{
Cache cache = HttpContext. Current. Cache;
If (cache [name]! = Null)
{
Return cache [name];
}
Else
{
Object obj = func ();
Insert (name, obj, isAbsoulute, isOverride, absoluteDate, spacing, dep, removeddCallback, priority );
Return obj;
}
}
/// <Summary>
/// Internal Method
/// </Summary>
Static void InsertInternal (string name, object value, bool isAbsoulute, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback callback, CacheItemPriority priority)
{
Cache cache = HttpContext. Current. Cache;
If (isAbsoulute)
{
Cache. Insert (name, value, dep, absoluteDate, TimeSpan. Zero, priority, callback );
}
Else
{
Cache. Insert (name, value, dep, DateTime. MaxValue, spacing, priority, callback );
}
}
}