什么是.NET 4.0的MemoryCache区别VS ObjectCache?区别、NET、MemoryCache、ObjectCache

2023-09-03 10:41:56 作者:沉鱼づ/落雁て

什么是.NET 4.0的MemoryCache区别VS ObjectCache? 凡使用哪个对象?

what is difference between .net 4.0 MemoryCache vs ObjectCache? Where to use which object?

推荐答案

ObjectCache是​​演示你应该如何去建立一个缓存坚持谁写ObjectCache的人希望你遵守规则的抽象类。无法实例ObjectCache直接,因为它是抽象。

ObjectCache is the abstract class that demonstrates how you should build a Cache that adheres to the rules the person who wrote ObjectCache wants you to obey. You cannot instantiate ObjectCache directly as it is abstract.

的MemoryCache是​​一个实际的实施 ObjectCache的。

MemoryCache is an actual implementation of ObjectCache.

从文档:

ObjectCache

再presents对象缓存,提供了基础的方法和   属性访问对象的缓存。

Represents an object cache and provides the base methods and properties for accessing the object cache.

MemoryCache

再presents实现在内存中缓存的类型。

Represents the type that implements an in-memory cache.

纵观声明的MemoryCache:

Looking at the declaration for MemoryCache:

public class MemoryCache : ObjectCache, 
    IEnumerable, IDisposable

我们可以看到,从的MemoryCache继承ObjectCache - 也就是说,它是一个使用内存作为存储对象的缓存 - 因此,这是的的实施的ObjectCache的

We can see that MemoryCache inherits from ObjectCache - that is, it's an cache for objects that uses Memory as its storage - this is therefore an implementation of ObjectCache.

您可以编写自己的;例如,DatabaseCache,这也可以从ObjectCache继承,而是会使用一个数据库作为后备存储。

You could write your own; for example, DatabaseCache, which could also inherit from ObjectCache but instead it would use a database as the backing storage.

对于日常使用,只要它满足你的需求,你会使用和消耗的MemoryCache。如果你想写你自己的,你可以继承ObjectCache和实施所需的方法和属性。然而,在现实中,有可能是没有什么实际利益这样做的微软已经做出一些其他的缓存解决方案可用,像许多其他供应商。

For everyday use, provided it met your needs, you would use and consume a MemoryCache. If you wanted to write your own, you could inherit from ObjectCache and implement the required methods and properties. However, in reality, there is probably little practical benefit to doing this as Microsoft already make several other caching solutions available, as do many other vendors.