引用一下这个
using System.Runtime.Caching;
/// <summary> /// 获取缓存数据 /// </summary> /// <param name="key">key的规则 key_方法名_标识字段</param> public static object GetCache(string key) { ObjectCache cache = MemoryCache.Default; var value = cache[key]; return value; } /// <summary> /// 缓存值 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <param name="ExpirationTime">过期时间</param> public static void SetCache(string key, object value, int ExpirationTime = 6000) { if (GetCache(key) != null) { return; } ObjectCache cache = MemoryCache.Default; CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddSeconds(ExpirationTime))//设置过期时间 }; cache.Set(key, value, policy); }