109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Runtime.Caching;
|
|||
|
|
|||
|
namespace SPC.Kiosk.Common
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Concurrent Dictionary Cache
|
|||
|
/// </summary>
|
|||
|
public static class CacheManager
|
|||
|
{
|
|||
|
private static MemoryCache _cache = MemoryCache.Default;
|
|||
|
public static void CacheRemove(this string _key)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_cache.Remove(_key);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
CommonLog.ErrorLogWrite("SPC.Kiosk.Common", "CacheManager", "CacheRemove()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Add Cache a Item
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T">Item Type</typeparam>
|
|||
|
/// <param name="_key">Store Key</param>
|
|||
|
/// <param name="_content">Store Item</param>
|
|||
|
/// <returns>IsSuccess</returns>
|
|||
|
public static bool AddCache<T>(this string _key,T _content)
|
|||
|
{
|
|||
|
bool result = false;
|
|||
|
try
|
|||
|
{
|
|||
|
if (!_cache.Contains(_key) && _content !=null)
|
|||
|
{
|
|||
|
var policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(24, 0, 0) };
|
|||
|
_cache.Add(_key, _content, policy);
|
|||
|
result = true;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = false;
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Get Stored Item
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T">Item Type</typeparam>
|
|||
|
/// <param name="_key">Store Key</param>
|
|||
|
/// <returns>Item</returns>
|
|||
|
public static T GetCache<T>(this string _key)
|
|||
|
{
|
|||
|
var result = default(T);
|
|||
|
try
|
|||
|
{
|
|||
|
if (_cache.Contains(_key))
|
|||
|
{
|
|||
|
result = (T)_cache[_key];
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = default(T);
|
|||
|
}
|
|||
|
return result;
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Check Cache Contain Item
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="_key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static bool CacheContain<T>(this string _key)
|
|||
|
{
|
|||
|
bool result = false;
|
|||
|
try
|
|||
|
{
|
|||
|
result = _cache.Contains(_key);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = false;
|
|||
|
}
|
|||
|
return result;
|
|||
|
|
|||
|
}
|
|||
|
#region Disposable Support
|
|||
|
/// <summary>
|
|||
|
/// Clear Dictionaly
|
|||
|
/// </summary>
|
|||
|
public static void Dispose()
|
|||
|
{
|
|||
|
_cache.Dispose();
|
|||
|
_cache = null;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|