caching – memory

public interface ICache
{
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”key”>Name of cached item</param>
/// <returns>Cached value. Default(T) if item doesn’t exist.</returns>
T Get<T>(string key);

/// <summary>
/// Insert value into the cache using appropriate name/value pairs
/// Use predefined cache duration with absolute expiration
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”value”>Item to be cached</param>
/// <param name=”key”>Name of item</param>
void Set<T>(string key, T value);

/// <summary>
/// Insert value into the cache using appropriate name/value pairs
/// Use predefined cache duration with sliding expiration
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”value”>Item to be cached</param>
/// <param name=”key”>Name of item</param>
void SetSliding<T>(string key, T value);

/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs WITH a cache duration set in minutes
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”key”>Item to be cached</param>
/// <param name=”value”>Name of item</param>
/// <param name=”duration”>Cache duration in minutes with absolute expiration</param>
void Set<T>(string key, T value, int duration);

/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs WITH a cache duration set in minutes
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”key”>Item to be cached</param>
/// <param name=”value”>Name of item</param>
/// <param name=”duration”>Cache duration in minutes with sliding expiration</param>
void SetSliding<T>(string key, T value, int duration);

/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs WITH a cache duration set in minutes
/// </summary>
/// <typeparam name=”T”>Type of cached item</typeparam>
/// <param name=”key”>Item to be cached</param>
/// <param name=”value”>Name of item</param>
/// <param name=”expiration”>Cache expiration time with absolute expiration</param>
void Set<T>(string key, T value, DateTimeOffset expiration);

/// <summary>
/// Returns a value indicating if the key exists in at least one cache layer configured in CacheManger,
/// without actually retrieving it from the cache.
/// </summary>
/// <param name=”key”>Item to be cached</param>
/// <returns>True if the key exists, False otherwise.</returns>
bool Exists(string key);

/// <summary>
/// Remove item from cache
/// </summary>
/// <param name=”key”>Name of cached item</param>
void Remove(string key);

/// <summary>
/// Remove all from cache
/// </summary>
/// <param name=”key”>Name of cached item</param>
void RemoveAll();
}

 

cacheprovider.cs

public abstract class CacheProviderBase<TCache> : ICache
{
public int CacheDuration { get; set; }

protected readonly TCache Cache;

private const int DefaultCacheDurationMinuts = 30;

protected readonly string KeyPrefix;

public CacheProviderBase()
{
int result;
CacheDuration = int.TryParse(ConfigurationManager.AppSettings[“CacheDefaultDurationMinutes”], out result) ? result : DefaultCacheDurationMinuts;
KeyPrefix = !string.IsNullOrEmpty(ConfigurationManager.AppSettings[“CacheKeyPrefix”]) ? ConfigurationManager.AppSettings[“CacheKeyPrefix”] : string.Empty;
Cache = InitCache();
}

protected abstract TCache InitCache();

public abstract T Get<T>(string key);

public virtual void Set<T>(string key, T value)
{
Set<T>(key, value, CacheDuration);
}

public virtual void SetSliding<T>(string key, T value)
{
SetSliding<T>(key, value, CacheDuration);
}

public abstract void Set<T>(string key, T value, int duration);

public abstract void SetSliding<T>(string key, T value, int duration);

public abstract void Set<T>(string key, T value, DateTimeOffset expiration);

public abstract bool Exists(string key);

public abstract void Remove(string key);

public abstract void RemoveAll();
}

MemoryProvider.cs

public class MemoryProvider : CacheProviderBase<MemoryCache>
{
protected override MemoryCache InitCache()
{
return MemoryCache.Default;
}

public override T Get<T>(string key)
{
try
{
if (!Exists(key))
{
return default(T);
}

return (T)Cache[KeyPrefix + key];
}
catch
{
return default(T);
}
}

 

public override void Set<T>(string key, T value, int duration)
{
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(duration) };
Cache.Set(KeyPrefix + key, value, policy);
}

public override void SetSliding<T>(string key, T value, int duration)
{
var policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, duration, 0) };
Cache.Set(KeyPrefix + key, value, policy);
}

public override void Set<T>(string key, T value, DateTimeOffset expiration)
{
var policy = new CacheItemPolicy { AbsoluteExpiration = expiration.DateTime };
Cache.Set(KeyPrefix + key, value, policy);
}

public override bool Exists(string key)
{
return Cache[KeyPrefix + key] != null;
}

public override void Remove(string key)
{
Cache.Remove(KeyPrefix + key);
}

public override void RemoveAll()
{
Cache.Dispose();
}
}

call –

var client = new HttpClient()

var resposnse = await client.PostAsync(“/my/mydemo”, content);
data= JsonConvert.DeserializeObject<MYCLASS>(await resposnse.Content.ReadAsStringAsync());

_memoryProvider.Set(“community_service”, new MYCLASS
{
username= data.username;
}, 60);

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s