C# .net 4.0 中的 MemoryCache 与 ObjectCache 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14232130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:07:07  来源:igfitidea点击:

What is difference between MemoryCache vs ObjectCache in .net 4.0?

c#.net.net-4.0

提问by Abdul Saboor

What is difference between .NET framework 4.0 MemoryCachevs ObjectCache? Where to use which object?

.NET 框架 4.0MemoryCache与4.0 之间有什么区别ObjectCache?在哪里使用哪个对象?

采纳答案by dash

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.

ObjectCache 是一个抽象类,它演示了您应该如何构建一个符合编写 ObjectCache 的人希望您遵守的规则的 Cache。您不能直接实例化 ObjectCache,因为它是抽象的

MemoryCache is an actual implementationof ObjectCache.

MemoryCache 是ObjectCache的实际实现

From the documentation:

从文档:

ObjectCache

对象缓存

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

表示对象缓存并提供访问对象缓存的基本方法和属性。

MemoryCache

内存缓存

Represents the type that implements an in-memory cache.

表示实现内存缓存的类型。

Looking at the declaration for MemoryCache:

查看 MemoryCache 的声明:

public class MemoryCache : ObjectCache, 
    IEnumerable, IDisposable

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 implementationof ObjectCache.

我们可以看到MemoryCache继承自ObjectCache——也就是说,它是一个使用Memory作为存储对象的缓存——因此这是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.

你可以自己写;例如,DatabaseCache,它也可以从 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.

对于日常使用,如果它满足您的需求,您将使用和消耗 MemoryCache。如果您想自己编写,可以从 ObjectCache 继承并实现所需的方法和属性。然而,实际上,这样做可能没有什么实际好处,因为 Microsoft 已经提供了几种其他缓存解决方案,许多其他供应商也是如此。

回答by PhilPursglove

ObjectCacheis an abstract class, you can't 'use' it per se. As Dash says in his comment, it's designed to show how a cache should be built and what operations it supports. MemoryCacheis an implementation of ObjectCacheand from your question is likely what you should use. However, because ObjectCacheis abstract, you could just as easily write your own FileCacheinheriting from ObjectCacheand it would be perfectly valid.

ObjectCache是一个抽象类,你不能“使用”它本身。正如 Dash 在他的评论中所说,它旨在展示应该如何构建缓存以及它支持哪些操作。MemoryCacheObjectCache您的问题的实现,很可能是您应该使用的。但是,因为ObjectCache是抽象的,所以您可以轻松地编写自己的FileCache继承自,ObjectCache并且它是完全有效的。

回答by Soner G?nül

From MSDN;

来自MSDN;

The ObjectCache type is the primary type for the in-memory object cache. The built-in MemoryCache class derives from the ObjectCache class. The MemoryCache class is the only concrete object cache implementation in the .NET Framework 4 that derives from the ObjectCache class.

ObjectCache 类型是内存中对象缓存的主要类型。内置的 MemoryCache 类派生自 ObjectCache 类。MemoryCache 类是 .NET Framework 4 中唯一从 ObjectCache 类派生的具体对象缓存实现。

public class MemoryCache : ObjectCache, 
    IEnumerable, IDisposable

MemoryCacheinherits from ObjectCache.

MemoryCache继承自ObjectCache.

You can get a reference to the default MemoryCacheinstance like this;

您可以MemoryCache像这样获得对默认实例的引用;

public static ObjectCache cache = MemoryCache.Default;