Self-write cache class

Source: Internet
Author: User
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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.