如何缓存服务器端变量并使其在 MVC 中过期
How to Cache a server-side variable and have it expire in MVC
我在 google 上搜索过,但没有找到合适的内容。谁能提供有关如何在 MVC 中缓存服务器端变量并使其在 5 分钟后过期的示例代码。我想在控制器中使用它 class.
非常感谢。
更新:我试过 MemoryCache,但它会在我每次刷新浏览器时更新,而不是每 5 分钟更新一次。我在浏览器中显示ViewBag.IsTime。
private bool IsTimeForCountUpdate()
{
var cacheKey = $"MyCacheKeyName";
MemoryCache cache = MemoryCache.Default;
int myCache = 0;
if (cache.Contains(cacheKey))
{
//myCache = (int)cache[cacheKey];
return true;
}
else
{
myCache = 1;
CacheItem cacheItem = new CacheItem(cacheKey, myCache);
cache.Add(cacheItem, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5) });
return false;
}
}
public async Task<ActionResult> Dashboard(int t = 1)
{
ViewBag.IsTime = IsTimeForCountUpdate() ? "YES" : "NO";
...
您可以简单地使用 ASP.Net 内置的 OutputCache。
持续时间以秒为单位,您可以使用VaryByParam按参数存储不同的缓存。
[OutputCache(Duration = 300, VaryByParam = "id")]
public ActionResult Index(int? id)
{
return Content(DateTime.Now.ToString());
}
内存缓存
您声明要缓存服务器端变量而不是内容结果。在这种情况下,您可以查看使用 MemoryCache.
示例:
假设您想缓存一些经常从数据库中获取的数据:
var cacheKey = "MyCacheKeyName";
MemoryCache cache = MemoryCache.Default;
XYZType myCache = null;
if (cache.Contains(cacheKey))
{
myCache = (XYZType)cache[cacheKey];
}
else
{
myCache = GetDataIWantToCache();
CacheItem cacheItem = new CacheItem(cacheKey, myCache);
cache.Add(cacheItem, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5) });
}
说明
if
块检查您的项目是否存在,如果存在则 returns 检查。
else
将创建您的项目(将 GetDataIWantToCache 替换为任何内容),因为找不到它并将其插入缓存五分钟。
OutputCache
如果要缓存整个控制器或单个操作的内容结果,请使用 OutputCache 属性。它具有用于控制多长时间以及是否要使用 VaryByParam 属性 对每个参数 and/or 某些参数进行缓存的设置。
上为您阅读更多内容
我在 google 上搜索过,但没有找到合适的内容。谁能提供有关如何在 MVC 中缓存服务器端变量并使其在 5 分钟后过期的示例代码。我想在控制器中使用它 class.
非常感谢。
更新:我试过 MemoryCache,但它会在我每次刷新浏览器时更新,而不是每 5 分钟更新一次。我在浏览器中显示ViewBag.IsTime。
private bool IsTimeForCountUpdate()
{
var cacheKey = $"MyCacheKeyName";
MemoryCache cache = MemoryCache.Default;
int myCache = 0;
if (cache.Contains(cacheKey))
{
//myCache = (int)cache[cacheKey];
return true;
}
else
{
myCache = 1;
CacheItem cacheItem = new CacheItem(cacheKey, myCache);
cache.Add(cacheItem, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5) });
return false;
}
}
public async Task<ActionResult> Dashboard(int t = 1)
{
ViewBag.IsTime = IsTimeForCountUpdate() ? "YES" : "NO";
...
您可以简单地使用 ASP.Net 内置的 OutputCache。
持续时间以秒为单位,您可以使用VaryByParam按参数存储不同的缓存。
[OutputCache(Duration = 300, VaryByParam = "id")]
public ActionResult Index(int? id)
{
return Content(DateTime.Now.ToString());
}
内存缓存
您声明要缓存服务器端变量而不是内容结果。在这种情况下,您可以查看使用 MemoryCache.
示例:
假设您想缓存一些经常从数据库中获取的数据:
var cacheKey = "MyCacheKeyName";
MemoryCache cache = MemoryCache.Default;
XYZType myCache = null;
if (cache.Contains(cacheKey))
{
myCache = (XYZType)cache[cacheKey];
}
else
{
myCache = GetDataIWantToCache();
CacheItem cacheItem = new CacheItem(cacheKey, myCache);
cache.Add(cacheItem, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5) });
}
说明
if
块检查您的项目是否存在,如果存在则 returns 检查。
else
将创建您的项目(将 GetDataIWantToCache 替换为任何内容),因为找不到它并将其插入缓存五分钟。
OutputCache
如果要缓存整个控制器或单个操作的内容结果,请使用 OutputCache 属性。它具有用于控制多长时间以及是否要使用 VaryByParam 属性 对每个参数 and/or 某些参数进行缓存的设置。
上为您阅读更多内容