如何创建一个缓存对象的类?

我是c#中的generics的新手,我正在尝试创建一个存储,我的程序的其他部分可以请求模型对象。 我的想法是,如果我的缓存类具有该对象,它会检查其日期并返回该对象,如果该对象不是10分钟。 如果它比较旧,那么10分钟它会从服务器在线下载更新的模型。 它没有对象是下载它并返回它。

但我遇到一些问题,将我的对象与DateTime配对,使其全部通用。

// model public class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { Person p = new Person(); Cache c = new Cache(); p = c.Get(p); } } public class Cache { struct DatedObject { public DateTime Time { get; set; } public T Obj { get; set; } } List<DatedObject> objects; public Cache() { objects = new List<DatedObject>(); } public T Get(T obj) { bool found = false; // search to see if the object is stored foreach(var elem in objects) if( elem.ToString().Equals(obj.ToString() ) ) { // the object is found found = true; // check to see if it is fresh TimeSpan sp = DateTime.Now - elem.Time; if( sp.TotalMinutes <= 10 ) return elem; } // object was not found or out of date // download object from server var ret = JsonConvert.DeserializeObject("DOWNLOADED JSON STRING"); if( found ) { // redate the object and replace it in list foreach(var elem in objects) if( elem.Obj.ToString().Equals(obj.ToString() ) ) { elem.Obj = ret; elem.Time = DateTime.Now; } } else { // add the object to the list objects.Add( new DatedObject() { Time = DateTime.Now, Obj = ret }); } return ret; } } 

查看作为.NET框架一部分提供的内存缓存类http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

您需要添加System.RunTime.Caching程序集作为应用程序的参考。 以下是一个辅助类,用于添加项目并从缓存中删除它们。

 using System; using System.Runtime.Caching; public static class CacheHelper { public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration) { MemoryCache.Default.Add(cacheKey, savedItem, absoluteExpiration); } public static T GetFromCache(string cacheKey) where T : class { return MemoryCache.Default[cacheKey] as T; } public static void RemoveFromCache(string cacheKey) { MemoryCache.Default.Remove(cacheKey); } public static bool IsIncache(string cacheKey) { return MemoryCache.Default[cacheKey] != null; } } 

关于这一点的好处是它是线程安全的,它会自动为你过期缓存。 因此,基本上您所要做的就是检查从MemoryCache获取项目是否为null。 但请注意 ,MemoryCache仅适用于.NET 4.0+

如果您的应用程序是Web应用程序,则使用System.Web.Caching而不是MemoryCache。 自.NET 1.1以来,System.Web.Caching已经可用,并且您不必在项目中添加其他参考。 下面是Web的帮助类。

 using System.Web; public static class CacheHelper { public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration) { if (IsIncache(cacheKey)) { HttpContext.Current.Cache.Remove(cacheKey); } HttpContext.Current.Cache.Add(cacheKey, savedItem, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.Default, null); } public static T GetFromCache(string cacheKey) where T : class { return HttpContext.Current.Cache[cacheKey] as T; } public static void RemoveFromCache(string cacheKey) { HttpContext.Current.Cache.Remove(cacheKey); } public static bool IsIncache(string cacheKey) { return HttpContext.Current.Cache[cacheKey] != null; } } 

还有其他缓存过期策略可用于这两种模式,例如基于文件路径的缓存,以便在文件更改时缓存自动过期,SQL缓存依赖性(定期轮询SQL服务器以进行更改),滑动过期或您可以建立自己的。 它们非常方便。