我是 C# 中泛型的新手,我正在尝试创建一个存储,我的程序的其他部分可以请求模型对象。 这个想法是,如果我的缓存类有对象,它会检查它的日期并在对象不早于 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<Person>(p);
}
}
public class Cache
{
struct DatedObject<T>
{
public DateTime Time { get; set; }
public T Obj { get; set; }
}
List<DatedObject<T>> objects;
public Cache()
{
objects = new List<DatedObject<T>>();
}
public T Get<T>(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<T>("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<T>() { 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<T>(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 获取的项目是否为空。 请注意,但是 MemoryCache 仅在 .NET 4.0+ 中可用
如果您的应用程序是 Web 应用程序,则使用 System.Web.Caching 而不是 MemoryCache。 System.Web.Caching 从 .NET 1.1 开始可用,您无需向项目添加额外的引用。这是用于网络的相同帮助程序类。
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<T>(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 server for changes),滑动过期或者你可以构建你自己的。它们非常方便。
关于c# - 如何创建一个缓存对象的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18181273/