public interface ICacheProvider
{
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if
/// item doesn't exist.</param>
/// <returns>Cached item as type</returns>
bool Get<T>(string key, out T value);
/// <summary>
/// Insert value into the cache using appropriate name/value pairs
/// </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);
}
public class CacheProvider : ICacheProvider
{
private readonly MemoryCache _memoryCache;
private readonly CacheItemPolicy _cacheItemPolicy;
public CacheProvider(CacheItemPolicy cacheItemPolicy, string configurationName, NameValueCollection config = null)
{
_memoryCache = ( config != null )? new MemoryCache(configurationName, config) : new MemoryCache(configurationName);
_cacheItemPolicy = cacheItemPolicy;
}
public bool Get<T>(string key, out T value)
{
bool result = false;
value = default(T);
object item = _memoryCache.Get(key);
if (item != null)
{
value = (T)item;
result = true;
}
return result;
}
public void Set<T>(string key, T value)
{
_memoryCache.Set(key, value, _cacheItemPolicy);
}
}
class Program
{
static void Main(string[] args)
{
//Fund fund = new Fund() { Id =1, Code = "MSREF1", Name = "MS REF I"};
IList<Fund> funds = new List<Fund>()
{
new Fund() { Id =1, Code = "MSREF1", Name = "MS REF I"},
new Fund() { Id =1, Code = "MSREF2", Name = "MS REF II"},
new Fund() { Id =1, Code = "MSREF3", Name = "MS REF III"},
new Fund() { Id =1, Code = "MSREF4", Name = "MS REF IV"},
new Fund() { Id =1, Code = "MSREF5", Name = "MS REF V"},
new Fund() { Id =1, Code = "MSREF6", Name = "MS REF VI"}
};
var policy = new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60.0)};
var cachingProvider = new CacheProvider( policy, "FundCacheConfig");
cachingProvider.Set("funds", funds);
IList<Fund> cachedFunds = null;
bool status = cachingProvider.Get("funds", out cachedFunds);
Console.WriteLine(String.Format("Funds => status {0} ", status ));
if (status)
{
foreach (var fund in cachedFunds)
Console.WriteLine(String.Format("Funds => Id {0} Code : {1} , Name : {2} ", fund.Id, fund.Code, fund.Name));
}
Console.ReadLine();
}
}
public class Fund
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
{
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if
/// item doesn't exist.</param>
/// <returns>Cached item as type</returns>
bool Get<T>(string key, out T value);
/// <summary>
/// Insert value into the cache using appropriate name/value pairs
/// </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);
}
public class CacheProvider : ICacheProvider
{
private readonly MemoryCache _memoryCache;
private readonly CacheItemPolicy _cacheItemPolicy;
public CacheProvider(CacheItemPolicy cacheItemPolicy, string configurationName, NameValueCollection config = null)
{
_memoryCache = ( config != null )? new MemoryCache(configurationName, config) : new MemoryCache(configurationName);
_cacheItemPolicy = cacheItemPolicy;
}
public bool Get<T>(string key, out T value)
{
bool result = false;
value = default(T);
object item = _memoryCache.Get(key);
if (item != null)
{
value = (T)item;
result = true;
}
return result;
}
public void Set<T>(string key, T value)
{
_memoryCache.Set(key, value, _cacheItemPolicy);
}
}
class Program
{
static void Main(string[] args)
{
//Fund fund = new Fund() { Id =1, Code = "MSREF1", Name = "MS REF I"};
IList<Fund> funds = new List<Fund>()
{
new Fund() { Id =1, Code = "MSREF1", Name = "MS REF I"},
new Fund() { Id =1, Code = "MSREF2", Name = "MS REF II"},
new Fund() { Id =1, Code = "MSREF3", Name = "MS REF III"},
new Fund() { Id =1, Code = "MSREF4", Name = "MS REF IV"},
new Fund() { Id =1, Code = "MSREF5", Name = "MS REF V"},
new Fund() { Id =1, Code = "MSREF6", Name = "MS REF VI"}
};
var policy = new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60.0)};
var cachingProvider = new CacheProvider( policy, "FundCacheConfig");
cachingProvider.Set("funds", funds);
IList<Fund> cachedFunds = null;
bool status = cachingProvider.Get("funds", out cachedFunds);
Console.WriteLine(String.Format("Funds => status {0} ", status ));
if (status)
{
foreach (var fund in cachedFunds)
Console.WriteLine(String.Format("Funds => Id {0} Code : {1} , Name : {2} ", fund.Id, fund.Code, fund.Name));
}
Console.ReadLine();
}
}
public class Fund
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
No comments:
Post a Comment