| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 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");
- /// <summary>
- /// 在缓存中新增数据
- /// </summary>
- /// <param name="errinfo"></param>
- 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;
- }
- /// <summary>
- /// 查询缓存中是否包含指定键
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static bool Contains(string key)
- {
- return cache.Contains(key);
- }
- /// <summary>
- /// 获取缓存中数据
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string Get(string key)
- {
- try
- {
- if (Contains(key))
- {
- return cache[key].ToString();
- }
- return null;
- }
- catch (Exception)
- {
- return null;
- }
- }
- /// <summary>
- /// 移除缓存中的数据
- /// </summary>
- /// <param name="key"></param>
- public static void Remove(string key)
- {
- if (Contains(key))
- {
- MemoryCache.Default.Remove(key);
- }
- }
- }
- }
|