我正在使用MemoryCache在我的MVC应用程序中缓存值。是否可以将MemoryCache集合值保存到文件中,然后加载它们?
发布于 2012-03-27 22:17:09
我认为这在理论上是可行的,使用反射将所有数据复制到某个简单的json对象,然后将其序列化为字符串并存储在磁盘上。当需要检索对象时,你需要再次借助反射从json构建新的MemoryCache实例。
也许它也有一些二进制序列化的变体,或者将对象转换为字节流,然后保存。
发布于 2019-08-01 16:09:53
你可以使用Microsoft docs
这是一个样本
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);
}
}https://stackoverflow.com/questions/9887156
复制相似问题