在 Visual Studio 中重新编译时将我的对象保留在缓存内存中
我有一个 MVC 3 站点,它使用内存中缓存的对象。
当网站第一次受到攻击时,需要大约一分钟的时间来构建缓存,一旦构建后,每个人的速度都会非常快。
当我开发时,我必须减少缓存对象的数量,因为每次我重新完成我的项目时,它都会删除缓存并必须重建它。
有没有一种方法可以设置 Visual Studio,以便在我重新完成时保留内存缓存?
这是我用于缓存的一些代码....
/// <summary>
/// Method to get all the prices
/// </summary>
public static List<DB2011_PriceRange> AllPrices
{
get
{
lock(_PriceLock)
{
if (HttpRuntime.Cache["Prices"] != null)
{
return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
}
else
{
PopulatePrices();
return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
}
}
}
}
/// <summary>
/// Poplate the Cache containing the prices
/// </summary>
private static void PopulatePrices()
{
// clear the cache and the list object
ClearCacheAndList("Trims", ref ListAllPrices);
using(var DB = DataContext.Get_DataContext)
{
ListAllPrices = (from p in DB.DB2011_PriceRange
select p).ToList();
}
// add the list object to the Cache
HttpRuntime.Cache.Add("Prices", ListAllPrices, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
任何帮助总是appricaied
Truegilly
I have an MVC 3 site that uses cached in memory objects.
when the site first gets hit, it takes around a minute to build the cache, once built its very fast for everyone then on.
when im developing, ive had to reduce the number of cached objects as everytime i recomple my project it drops the cache and has to rebuild it.
Is there a way i can set Visual studio so it keeps the in memory cache when i recomple ?
here is some of my code i use for caching....
/// <summary>
/// Method to get all the prices
/// </summary>
public static List<DB2011_PriceRange> AllPrices
{
get
{
lock(_PriceLock)
{
if (HttpRuntime.Cache["Prices"] != null)
{
return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
}
else
{
PopulatePrices();
return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
}
}
}
}
/// <summary>
/// Poplate the Cache containing the prices
/// </summary>
private static void PopulatePrices()
{
// clear the cache and the list object
ClearCacheAndList("Trims", ref ListAllPrices);
using(var DB = DataContext.Get_DataContext)
{
ListAllPrices = (from p in DB.DB2011_PriceRange
select p).ToList();
}
// add the list object to the Cache
HttpRuntime.Cache.Add("Prices", ListAllPrices, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
any help is always appricaiated
Truegilly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重新编译您的应用程序会导致托管该应用程序的 AppDomain 重新启动,这会释放您的缓存。您可以:
Recompiling your application causes the AppDomain that is hosting it to be restarted which is what is disposing of your cache. You could:
我不相信是这样。当您发布新的 DLL 时,将创建运行应用程序的新进程。由于您使用的是内存缓存,因此所有对象都将被删除。
您可以使用一种特殊的方法“预热缓存”,该方法会在您发布新代码时预先填充所有缓存。
I don't believe so. When you publish new DLLs the a new process that runs the application is created. Since you're using an in-memory cache, all objects would be removed.
You could "warm the caches" with a special method that would pre-populate them all when you publish new code.