.NET Framework 4.0 MemoryCache
与 ObjectCache
有什么区别?
在哪里使用哪个对象?
最佳答案
ObjectCache 是一个抽象类,它演示了您应该如何构建一个遵循编写 ObjectCache 的人希望您遵守的规则的缓存。您不能直接实例化 ObjectCache abstract .
MemoryCache 是 ObjectCache 的实际实现。
来自文档:
Represents an object cache and provides the base methods and properties for accessing the object cache.
Represents the type that implements an in-memory cache.
查看 MemoryCache 的声明:
public class MemoryCache : ObjectCache,
IEnumerable, IDisposable
我们可以看到 MemoryCache 继承自 ObjectCache - 也就是说,它是使用 Memory 作为其存储的对象的缓存 - 因此这是 ObjectCache 的一个实现。
你可以自己写;例如,DatabaseCache,它也可以继承自 ObjectCache,但它会使用数据库作为后备存储。
对于日常使用,只要它满足您的需求,您就会使用和使用 MemoryCache。如果您想自己编写,可以从 ObjectCache 继承并实现所需的方法和属性。然而,实际上,这样做可能没有什么实际好处,因为 Microsoft 已经和许多其他供应商一样提供了其他几种缓存解决方案。
关于c# - .net 4.0 中的 MemoryCache 与 ObjectCache 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232130/