使用单例进行缓存

发布于 2024-12-01 18:56:08 字数 331 浏览 1 评论 0原文

我最近读到,单例是一种反模式,除非确实需要,否则不应使用。

在我们所有的项目中,我们使用单例模式来保存一些缓存数据。例如:

class SomeClass
{
   public SomeClass()
   {
       var somedata = Singleton.Instance.GetSomeData(stringRepresintation); // or like that
       var someData = Singleton.Instance.SomeData;
   }      
}

保存该数据的推荐设计是什么(静态类或其他)?

I recently read that singleton is an anti-pattern and should not be used unless it is really needed.

In all our projects, we use the singleton pattern to hold some cache data. For example:

class SomeClass
{
   public SomeClass()
   {
       var somedata = Singleton.Instance.GetSomeData(stringRepresintation); // or like that
       var someData = Singleton.Instance.SomeData;
   }      
}

What is the recommended design to hold that data (static class, or something else)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-12-08 18:56:08

好吧,您可以将缓存视为依赖项 - 并将一个实例(可能是同一实例)传递到需要它的所有内容中。这就是我在已经使用依赖注入的情况下所做的。

这将使测试那些需要缓存的类变得更容易 - 您可以为每个测试创建一个新的缓存,而不必担心清除现有的缓存。您还可以并行化测试,而不必担心两个测试会弄乱彼此的缓存。此外,它使缓存依赖性更加清晰。

(是否像 cdhowie 建议的那样使用接口来表示缓存取决于您。您不必这样做,尽管如果您这样做,它会将类与其依赖关系更多地解耦。如果您缓存非常简单,您不介意在测试中使用生产实现,它可能不值得提取接口。)

Well, you could think of the cache as a dependency - and pass an instance (probably the same instance) into everything which needed it. That's what I'd do in a situation where I was already using dependency injection.

That would make it easier to test those classes which need the cache - you could create a new cache for each test, and not have to worry about clearing out an existing one. You could also parallelize tests without worrying about two tests messing up each others caches. Additionally, it makes the cache dependency clearer.

(Whether or not you use an interface to represent the cache as cdhowie suggests is up to you. You don't have to, although it would decouple the classes from their dependencies more if you did. If your cache is very simple and you don't mind using the production implementation in tests, it may not be worth extracting an interface.)

鹿! 2024-12-08 18:56:08

您将创建一些ICache 接口来定义缓存提供程序所需的成员。然后您将允许将实例传递给 SomeClass()。如果调用者未指定默认实例,您仍然可以使用默认实例(实现 ICache)。这就是依赖注入模式,本质上与单例模式完全相反。

这将允许替换不同的缓存机制(或只是不同的缓存实例),而无需对 SomeClass 进行任何更改。

You would create some ICache interface that defines the members necessary for a cache provider. Then you would allow an instance to be passed in to SomeClass(). You can still use a default instance (implementing ICache) if the caller doesn't specify one. This is the dependency injection pattern, which is essentially the complete opposite of the singleton pattern.

This will allow a different caching mechanism (or just different cache instances) to be substituted in without any changes to SomeClass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文