c# - ObjectCache 在一致设置后不会立即包含项目

标签 c# objectcache

所有- 我正在尝试使用新的 ObjectCache 对象创建服务器端缓存。无论出于何种原因,以下内容都会引发异常...

_cache.Set(CacheKey.LOCATIONS, cachedLocations, DateTimeOffset.Now.AddHours(2));
                    if (!_cache.Contains(CacheKey.LOCATIONS))
                        throw new Exception("Did not add location to the cache.");

_cache在服务层通过以下方式实例化...

private ObjectCache _cache = MemoryCache.Default;

每次设置缓存时都会发生此异常,第一次除外。异常是代码中显示的那个……“没有将位置添加到缓存。”

---编辑笔记。 ObjectCache 是静态的,但不需要它是静态的,因为 MemoryCache.Default 是静态的。

最佳答案

您应该使用 Add() 方法在缓存中创建一个新条目。 Set() 方法涉及的键已经存在。

关于c# - ObjectCache 在一致设置后不会立即包含项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7208902/

相关文章:

c# - 窗体/C# : Store/Retrieve (Persist) Content of TextBoxes between 2 Runs

c# - 我可以找到上次访问 MemoryCache 项目的时间吗?

c# - .NET 4 ObjectCache - 我们可以连接到 "Cache Expired"事件吗?

asp.net - 缓存数据.Net 4.0 (Asp.NET)

c# - 试图更好地理解 'using' 语句

c# - 使用c#将文件从一个IP地址发送到另一个IP地址

c# - 在 Windows 窗体中进行验证的最佳方法是什么

c# - SQL 命令结果到字典 C# .NET 2.0

entity-framework - Entity Framework 6 和 ObjectCache : saving issues