using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Runtime.Caching; namespace SPC.Kiosk.Common { /// /// Concurrent Dictionary Cache /// 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)); } } /// /// Add Cache a Item /// /// Item Type /// Store Key /// Store Item /// IsSuccess public static bool AddCache(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; } /// /// Get Stored Item /// /// Item Type /// Store Key /// Item public static T GetCache(this string _key) { var result = default(T); try { if (_cache.Contains(_key)) { result = (T)_cache[_key]; } } catch { result = default(T); } return result; } /// /// Check Cache Contain Item /// /// /// /// public static bool CacheContain(this string _key) { bool result = false; try { result = _cache.Contains(_key); } catch { result = false; } return result; } #region Disposable Support /// /// Clear Dictionaly /// public static void Dispose() { _cache.Dispose(); _cache = null; } #endregion } }