- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
根据 MSDN 文档 here :
Do not create MemoryCache instances unless it is required. If you create cache instances in client and Web applications, the MemoryCache instances should be created early in the application life cycle. You must create only the number of cache instances that will be used in your application, and store references to the cache instances in variables that can be accessed globally. For example, in ASP.NET applications, you can store the references in application state. If you create only a single cache instance in your application, use the default cache and get a reference to it from the Default property when you need to access the cache.
它并没有完全解释这背后的原因和其他几个关于清除 MemoryCache
的问题,但似乎还没有得出可靠的结论。据我所知,我的选择是处置 MemoryCache
并创建一个新实例,根据上述文档不建议这样做,或者使用枚举器,由于性能问题也不建议这样做. Trim()
已被证明是不可靠的,因此我正在研究为什么在我的应用程序生命周期中间实例化一个 MemoryCache
被认为是一个坏主意。
为了深入了解我好奇的原因,这里手头的主要问题是我希望我的 MemoryCache
仅在一系列方法调用的范围内工作。这些方法调用是在同一对象的不同实例之间进行的,这些实例共享通过数据库检索的数据,尽管可能性很小,但可能会在我分配给过期窗口的短短几秒钟内发生变化。我的想法是每当我开始一系列方法调用时创建一个新的 MemoryCache
实例,以确保我拥有最新的数据,同时不会为每个实例多次访问数据库。但是,MSDN 也建议不要创建新实例,并建议将所有实例都放在全局变量中。
所以简而言之,为什么建议在应用程序生命周期开始时实例化一个MemoryCache
,为什么我应该全局引用MemoryCache
我用什么?
最佳答案
因为它需要连续的内存。你想在内存碎片化之前完成它。
我不知道全局。
关于c# - 为什么我应该只在应用程序生命周期的早期创建 MemoryCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788204/
我读了MSDN documentation但并没有真正理解它。 我相信Set的行为是“替换现有的,或添加”(原子地)。 正确吗? 最佳答案 如果该键已存在值, Add 不会执行任何操作(返回 fals
我试图找到一种方法让 .net 4.0 MemoryCache.Default 实例使用不区分大小写的比较器。 那可能吗? var bob = new object(); MemoryCache.De
我试图弄清楚应该如何使用 MemoryCache 以避免出现内存不足异常。我来自 ASP.Net 背景,缓存管理它自己的内存使用,所以我希望 MemoryCache 会做同样的事情。正如我制作的波纹管
在 Controller 类中,我有 using Microsoft.Extensions.Caching.Memory; private IMemoryCache _cache; private r
Memcached API 有一个 Touch() 方法,它可以更新给定 key 的过期策略。如何使用 .Net ObjectCache 类最好地完成此任务? 我能看到的最好的办法是删除对象并重新添加
当使用 MemoryCache 时,可以设置 AbsoluteExpiration AbsoluteExpirationRelativeToNow 例子: cache.GetOrCreate(
根据 MSDN 文档 here : Do not create MemoryCache instances unless it is required. If you create cache ins
我需要添加缓存功能并找到了一个名为 MemoryCache 的新类。但是,我发现 MemoryCache 有点残缺(我需要区域功能)。除其他事项外,我需要添加类似 ClearAll(region) 的
在我的应用程序中,我使用 MemoryCache,但我不希望项目过期。因此,项目将使用默认策略插入到缓存中,而无需设置 AbsoulteExpiration 或 SlidingExpiration。
MemoryCache类公开了一个名为 .AddOrGetExisting 的方法这是一种线程安全的方法,如果存在则获取,如果不存在则添加。 如果缓存对象不存在,此方法返回 NULL。我想我理解它的值
MemoryCache 是否具有缓存固定数量项目的功能? 例如我们只对从数据库中缓存 2000 个项目感兴趣。在不断向缓存中添加项目的同时,如果超过指定的项目数,则可以删除最旧的项目。 如果不是,我们
我正在使用 .NET 4.0 MemoryCache应用程序中的类并试图限制最大缓存大小,但在我的测试中,缓存似乎并没有真正遵守限制。 我正在使用设置,according to MSDN ,应该限制缓
我将 MemoryCache 与 Sql 依赖项一起使用。我注意到当使用 MemoryCache.Set() 时,如果集合中的某个项目被覆盖,则会发生内存泄漏。考虑以下场景: key=A 的项被插入到
我一直在阅读有关从 .Net Framework 4.0 开始的新 MemoryCache 类的所有地方。根据我的阅读,您可以跨不同的 .Net 应用程序访问 MemoryCache。我正在尝试在 A
我收到这条消息: System.ObjectDisposedException: Cannot access a disposed object. A common cause of this err
应用程序需要加载数据并缓存一段时间。我希望如果应用程序的多个部分想要同时访问同一个缓存键,缓存应该足够智能,只加载一次数据并将该调用的结果返回给所有调用者。然而, MemoryCache 不是这样做的
MemoryCache 是一个线程安全类,根据 this文章。但我不明白它在特定情况下会如何表现。例如我有代码: static private MemoryCache _cache = MemoryC
我不明白在 .NET 4.0 的 System.Runtime.Caching.MemoryCache 中滑动过期应该如何工作。 根据文档,过期时间跨度是“在从缓存中逐出缓存条目之前必须访问缓存条目的
在 .NET 4 MemoryCache 中,有没有办法找到上次访问项目的时间?我确定它在内部被跟踪,因为 CacheItemPolicy 具有 SlidingExpiration 属性。但是我找不到
我正在使用 .NET 4.0 MemoryCache类,我想以线程安全的方式添加或替换缓存中的现有项,但我还想知道我是否替换了现有项或添加了新项。 据我所知,Set方法旨在原子地替换缓存中的项目(如果
我是一名优秀的程序员,十分优秀!