Tag: memorycache

MemoryCache在第一次到期后始终返回“null”

我有以下问题,我有这种缓存管理: public class CacheManager : ICacheManager { private readonly ObjectCache cache; public CacheManager() { this.cache = MemoryCache.Default; } public void AddItem(string key, object itemToCache, double duration) { var end = DateTime.Now.AddSeconds(duration); this.cache.Add(key, itemToCache, end); } public T Get(string key) where T : class { try { return (T)this.cache[key]; } catch (Exception ex) { return null; […]

自我更新MemoryCache

我已经实现了一个缓存,当它们到期时更新包含值。 我的代码是这样的: class MyCache { private static readonly MemoryCache Cache = MemoryCache.Default; private CacheItemPolicy _errorpolicy; private CacheItemPolicy _warnPolicy; private CacheEntryRemovedCallback _warnCacheEntryRemovedCallback; private CacheEntryRemovedCallback _errorCacheEntryRemovedCallback; public void AddErrors(string key, object errors) { _errorCacheEntryRemovedCallback = ErrorItemRemovedCallback; _errorpolicy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(7.00), RemovedCallback = _errorCacheEntryRemovedCallback }; Cache.Set(key, errors, _errorpolicy); } public void AddWarnings(string key, object […]

为什么在ASP.Net Core中获取IMemoryCache的多个实例?

我认为我的ASP.NET核心应用程序中的IMemoryCache的标准用法。 在startup.cs我有: services.AddMemoryCache(); 在我的控制器中,我有: private IMemoryCache memoryCache; public RoleService(IMemoryCache memoryCache) { this.memoryCache = memoryCache; } 然而,当我进行调试时,我最终得到了多个内存缓存,每个缓存中包含不同的项目。 我以为内存缓存会是单例吗? 更新了代码示例: public List GetFunctionRoles() { var cacheKey = “RolesList”; var functionRoles = this.memoryCache.Get(cacheKey) as List; if (functionRoles == null) { functionRoles = this.functionRoleDAL.ListData(orgId); this.memoryCache.Set(cacheKey, functionRoles, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(1))); } } 如果我在两个不同的浏览器中运行两个客户端,当我点击第二行时,我可以看到this.memoryCache包含不同的条目。

MemoryCache是​​否支持区域?

我需要添加缓存function,并找到一个名为MemoryCache的新shiny类。 但是,我发现MemoryCache有点瘫痪(我需要区域function)。 除了其他东西,我需要添加像ClearAll(区域)这样的东西。 作者努力保持这个类没有区域支持,代码如下: if (regionName != null) { throw new NotSupportedException(R.RegionName_not_supported); } 几乎每种方法都有。 我没有看到一种简单的方法来覆盖这种行为。 我能想到的添加区域支持的唯一方法是添加一个新类作为MemoryCache的包装,而不是作为一个inheritance自MemoryCache的类。 然后在这个新类中创建一个Dictionary并让每个方法“缓冲”区域调用。 听起来很讨厌和错误,但最终…… 您知道向MemoryCache添加区域的更好方法吗?

Asp.Net Core:在控制器外部使用内存缓存

在ASP.NET Core中,它很容易从控制器访问您的内存缓存 在您的初创公司中添加: public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); } 然后从你的控制器 [Route(“api/[controller]”)] public class MyExampleController : Controller { private IMemoryCache _cache; public MyExampleController(IMemoryCache memoryCache) { _cache = memoryCache; } [HttpGet(“{id}”, Name = “DoStuff”)] public string Get(string id) { var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(1)); _cache.Set(“key”, “value”, cacheEntryOptions); } } 但是,如何在控制器外部访问相同的内存缓存。 例如。 我有一个由HangFire发起的计划任务,如何从我的代码中通过HangFire计划任务访问内存缓存? public class ScheduledStuff { […]

如何在MemoryCache上分离对象引用

我目前正在试用.Net 4中的新MemoryCache来缓存我们的一个应用程序中的一些数据。 我遇到的麻烦是对象被更新,缓存似乎是持久的变化,例如 public IEnumerable GetFromDatabase(){ const string _cacheKeyGetDisplayTree = “SomeKey”; ObjectCache _cache = MemoryCache.Default; var objectInCache = _cache.Get(_cacheKeyGetDisplayTree) as IEnumerable; if (objectInCache != null) return objectInCache.ToList(); // Do something to get the items _cache.Add(_cacheKeyGetDisplayTree, categories, new DateTimeOffset(DateTime.UtcNow.AddHours(1))); return categories.ToList(); } public IEnumerable GetWithIndentation(){ var categories = GetFromDatabase(); foreach (var c in categories) { c.Name […]

.NET 4.0 MemoryCache性能计数器在哪里?

.NET 4.0 MemoryCache性能计数器在哪里? 我正在寻找他们的名字,我找不到任何名字。 谢谢,

内存缓存.Net 4.0性能测试:惊人的结果

此性能测试是错误的还是系统缓存正在以卓越的性能运行? 这是我的结果: [13]交互次数100000:63毫秒 [14]交互次数100000:139毫秒 [12]交互次数100000:47毫秒 [15]交互次数100000:44毫秒 测试结束。 硬件:x86 Family 6 Model 23 Stepping GenuineIntel~2992 Mhz 3.327 MB,5.1.2600 Service Pack 3 using System; using System.Collections.Generic; using System.Runtime.Caching; using System.Diagnostics; using System.Threading; namespace CacheNet40 { public class CacheTest { private ObjectCache cache; public CacheTest() { cache = MemoryCache.Default; } public void AddItem(CacheItem item, double span) { CacheItemPolicy […]