using System;
using System.Collections.Generic;
using System.Runtime.Caching;
namespace SHJX.Service.Common.Extend
{
public class CancelCache
{
private static readonly MemoryCache cache = new("CancelCache");
///
/// 在缓存中新增数据
///
///
public static void Set(string key, object value)
{
var policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTime.Now.AddMinutes(15)
};
if (!Contains(key))
{
cache.Set(key, value, policy);
}
cache[key] = value;
}
///
/// 查询缓存中是否包含指定键
///
///
///
public static bool Contains(string key)
{
return cache.Contains(key);
}
///
/// 获取缓存中数据
///
///
///
public static string Get(string key)
{
try
{
if (Contains(key))
{
return cache[key].ToString();
}
return null;
}
catch (Exception)
{
return null;
}
}
///
/// 移除缓存中的数据
///
///
public static void Remove(string key)
{
if (Contains(key))
{
MemoryCache.Default.Remove(key);
}
}
}
}