I am using MemoryCache to cache values in my MVC web app. Is it possible to save MemoryCache collection values to file and latter load them?
问问题
1129 次
2 回答
0
认为理论上可行,使用反射将所有数据复制到一些简单的 json 对象,然后将其序列化为字符串并存储在磁盘上。当需要检索对象时,您需要再次借助反射从 json 构建新的 MemoryCache 实例。
也许它还有一些带有二进制序列化或将对象转换为字节流然后保存的变体。
于 2012-03-27T14:17:08.650 回答
-1
你可以微软文档
这是一个样本
public static class Cashing
{
public static void SetData<T>(string CacheKey, T data)
{
ObjectCache cache = MemoryCache.Default;
if (cache.Contains(CacheKey))
cache.Remove(CacheKey);
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);
cache.Add(CacheKey, data, cacheItemPolicy);
}
public static object GetData<T>(string CacheKey)
{
ObjectCache cache = MemoryCache.Default;
if (cache.Contains(CacheKey))
return cache.Get(CacheKey);
return default(T);
}
}
于 2019-08-01T08:09:53.000 回答