Caching - Caching enables you to store data in memory for rapid access. When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. This can improve performance and scalability.
So First step to create cache class like below .
public static class CacheManager
{
// its used clear cache depend on key value .
public static void ClearCacheOf<T>(string key)
{
if (!string.IsNullOrEmpty(key) && HttpContext.Current != null)
HttpContext.Current.Cache.Remove(typeof(T) + key);
}
/// Check if the key correponds to an object saved in cache.
/// <param name="timeToLive">The time to live in minutes (how long the cache will keep the data).</param>
/// <param name="identifier">The object's identifier.</param>
/// <param name="function">The function to apply if the object is not found in cache wich will do the external call.</param>
public static T CacheVerification<T>(int timeToLive, string identifier, Func<T> function)
{
if (HttpContext.Current == null)
return function();
string key = typeof(T) + identifier;
var requestedCachedItem = GetFromCache<T>(key);
T response;
if (requestedCachedItem != null)
{
response = requestedCachedItem;
}
else
{
response = function();
AddToCache(key, response, timeToLive);
}
return response;
}
private static void AddToCache<T>(string key, T objectToAdd, int ttl)
{
if (objectToAdd != null)
{
HttpContext.Current.Cache.Insert(key, objectToAdd, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(ttl));
}
}
// use existing cache depend on key value
private static T GetFromCache<T>(string key)
{
return (T)HttpContext.Current.Cache[key];
}
}
So we can use above class method like this .
1-> case first we are going to save cache in memory .
public IDictionary<int, string> GetMyId(string id)
{
return CacheManager.CacheVerification(12, id, () => return CacheManager.CacheVerification(12, id, () => new Dictionary<int, string>());
// or use call your method , i just return empty dictionary for example.
}
2-> remove cache
CacheManager.ClearCacheOf<List<yourclassname>>("KeyValue");
So First step to create cache class like below .
public static class CacheManager
{
// its used clear cache depend on key value .
public static void ClearCacheOf<T>(string key)
{
if (!string.IsNullOrEmpty(key) && HttpContext.Current != null)
HttpContext.Current.Cache.Remove(typeof(T) + key);
}
/// Check if the key correponds to an object saved in cache.
/// <param name="timeToLive">The time to live in minutes (how long the cache will keep the data).</param>
/// <param name="identifier">The object's identifier.</param>
/// <param name="function">The function to apply if the object is not found in cache wich will do the external call.</param>
public static T CacheVerification<T>(int timeToLive, string identifier, Func<T> function)
{
if (HttpContext.Current == null)
return function();
string key = typeof(T) + identifier;
var requestedCachedItem = GetFromCache<T>(key);
T response;
if (requestedCachedItem != null)
{
response = requestedCachedItem;
}
else
{
response = function();
AddToCache(key, response, timeToLive);
}
return response;
}
private static void AddToCache<T>(string key, T objectToAdd, int ttl)
{
if (objectToAdd != null)
{
HttpContext.Current.Cache.Insert(key, objectToAdd, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(ttl));
}
}
// use existing cache depend on key value
private static T GetFromCache<T>(string key)
{
return (T)HttpContext.Current.Cache[key];
}
}
So we can use above class method like this .
1-> case first we are going to save cache in memory .
public IDictionary<int, string> GetMyId(string id)
{
return CacheManager.CacheVerification(12, id, () => return CacheManager.CacheVerification(12, id, () => new Dictionary<int, string>());
// or use call your method , i just return empty dictionary for example.
}
2-> remove cache
CacheManager.ClearCacheOf<List<yourclassname>>("KeyValue");
No comments:
Post a Comment