Coo*_*der 244 database asp.net-mvc caching
我在MVC应用程序中阅读了大量有关页面缓存和部分页面缓存的信息.但是,我想知道如何缓存数据.
在我的场景中,我将使用LINQ to Entities(实体框架).在第一次调用GetNames(或任何方法)时,我想从数据库中获取数据.我想将结果保存在缓存中,并在第二次调用时使用缓存版本(如果存在).
任何人都可以展示一个如何工作的例子,应该在哪里实现(模型?)以及它是否可行.
我已经在传统的ASP.NET应用程序中看到了这一点,通常用于非常静态的数据.
Hrv*_*udo 393
这是我使用的一个很好的简单缓存助手类/服务:
using System.Runtime.Caching;
public class InMemoryCache: ICacheService
{
public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
{
T item = MemoryCache.Default.Get(cacheKey) as T;
if (item == null)
{
item = getItemCallback();
MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(10));
}
return item;
}
}
interface ICacheService
{
T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class;
}
Run Code Online (Sandbox Code Playgroud)
cacheProvider.GetOrSet("cache key", (delegate method if cache is empty));
Run Code Online (Sandbox Code Playgroud)
缓存提供程序将检查缓存中是否存在名称为"cache id"的内容,如果没有,它将调用委托方法来获取数据并将其存储在缓存中.
var products=cacheService.GetOrSet("catalog.products", ()=>productRepository.GetAll())
Run Code Online (Sandbox Code Playgroud)
ter*_*tyl 74
在模型中引用System.Web dll并使用System.Web.Caching.Cache
public string[] GetNames()
{
string[] names = Cache["names"] as string[];
if(names == null) //not in cache
{
names = DB.GetNames();
Cache["names"] = names;
}
return names;
}
Run Code Online (Sandbox Code Playgroud)
有点简化,但我想这会奏效.这不是MVC特定的,我一直使用这种方法来缓存数据.
Oli*_*Oli 43
我指的是TT的帖子,并建议采用以下方法:
在模型中引用System.Web dll并使用System.Web.Caching.Cache
public string[] GetNames()
{
var noms = Cache["names"];
if(noms == null)
{
noms = DB.GetNames();
Cache["names"] = noms;
}
return ((string[])noms);
}
Run Code Online (Sandbox Code Playgroud)
您不应该返回从缓存中重新读取的值,因为您永远不会知道在该特定时刻它是否仍在缓存中.即使你之前在声明中插入它,它可能已经消失或者从未被添加到缓存中 - 你只是不知道.
因此,您添加从数据库读取的数据并直接返回,而不是从缓存中重新读取.
juF*_*uFo 34
适用于.NET 4.5+框架
添加参考: System.Runtime.Caching
添加使用声明:
using System.Runtime.Caching;
public string[] GetNames()
{
var noms = System.Runtime.Caching.MemoryCache.Default["names"];
if(noms == null)
{
noms = DB.GetNames();
System.Runtime.Caching.MemoryCache.Default["names"] = noms;
}
return ((string[])noms);
}
Run Code Online (Sandbox Code Playgroud)
在.NET Framework 3.5和更早版本中,ASP.NET在System.Web.Caching命名空间中提供了内存中缓存实现.在以前版本的.NET Framework中,缓存仅在System.Web命名空间中可用,因此需要依赖ASP.NET类.在.NET Framework 4中,System.Runtime.Caching命名空间包含为Web和非Web应用程序设计的API.
更多信息:
https://msdn.microsoft.com/en-us/library/dd997357(v=vs.110).aspx
https://docs.microsoft.com/en-us/dotnet/framework/performance/caching-in-net-framework-applications
Bre*_*ick 26
史蒂夫史密斯做了两篇很棒的博客文章,演示了如何在ASP.NET MVC中使用他的CachedRepository模式.它有效地使用存储库模式,允许您在不必更改现有代码的情况下获得缓存.
http://ardalis.com/Introducing-the-CachedRepository-Pattern
http://ardalis.com/building-a-cachedrepository-via-strategy-pattern
在这两篇文章中,他向您展示了如何设置此模式,并解释了它为何有用.通过使用此模式,您可以获得缓存,而无需现有代码查看任何缓存逻辑.实际上,您使用缓存的存储库就像它是任何其他存储库一样.
小智 8
我已经以这种方式使用它并且它对我有用。 https://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(v=vs.110).aspx system.web.caching.cache.add 的参数信息。
public string GetInfo()
{
string name = string.Empty;
if(System.Web.HttpContext.Current.Cache["KeyName"] == null)
{
name = GetNameMethod();
System.Web.HttpContext.Current.Cache.Add("KeyName", name, null, DateTime.Noew.AddMinutes(5), Cache.NoSlidingExpiration, CacheitemPriority.AboveNormal, null);
}
else
{
name = System.Web.HttpContext.Current.Cache["KeyName"] as string;
}
return name;
}
Run Code Online (Sandbox Code Playgroud)