C# 内存缓存帮助类


using System;
using System.Runtime.Caching;

namespace MyTools { /// /// 缓存类,此类型是线程安全的 /// public class CacheHelper { private static ObjectCache cache = MemoryCache.Default; /// /// 读取缓存 /// /// /// public static object GetCache(string key) { var obj = cache.Get(key); return obj; } /// /// 写入缓存 /// /// /// /// 过期时间,默认7200秒 public static void SetCache(string key, object obj, int timeout = 7200) { cache.Set(key, obj, DateTimeOffset.Now.AddSeconds(timeout)); } /// /// 删除缓存 /// /// public static void RemoveCache(string key) { cache.Remove(key); } } }