缓存/记忆功能结果是谁的责任? [英] Whose responsibility is it to cache / memoize function results?

查看:65
本文介绍了缓存/记忆功能结果是谁的责任?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发允许用户通过实现一组接口来扩展系统的软件。

I'm working on software which allows the user to extend a system by implementing a set of interfaces.

为了测试我们所要进行的可行性这样,我的公司通过以与用户完全相同的方式在这些类中实现我们所有的业务逻辑,来食用自己的狗食。

In order to test the viability of what we're doing, my company "eats its own dog food" by implementing all of our business logic in these classes in the exact same way a user would.

我们有一些实用程序类/方法将所有内容捆绑在一起,并使用可扩展类中定义的逻辑。

We have some utility classes / methods that tie everything together and use the logic defined in the extendable classes.

I想要缓存用户定义函数的结果。我应该在哪里做?

I want to cache the results of the user-defined functions. Where should I do this?


  • 是类本身吗?看来这可能会导致大量代码重复。

  • Is it the classes themselves? This seems like it can lead to a lot of code duplication.

使用这些类的实用程序/引擎吗?如果是这样,不知情的用户可以直接调用class函数,而不会获得任何缓存好处。

Is it the utilities/engine which uses these classes? If so, an uninformed user may call the class function directly and not receive any caching benefit.

public interface ILetter { string[] GetAnimalsThatStartWithMe(); }

public class A : ILetter { public string[] GetAnimalsThatStartWithMe()
                           { 
                               return new [] { "Aardvark", "Ant" }; 
                           }
                         }
public class B : ILetter { public string[] GetAnimalsThatStartWithMe()
                           { 
                               return new [] { "Baboon", "Banshee" }; 
                           } 
                         }
/* ...Left to user to define... */
public class Z : ILetter { public string[] GetAnimalsThatStartWithMe()
                           { 
                               return new [] { "Zebra" };
                           }
                         }

public static class LetterUtility
{
    public static string[] GetAnimalsThatStartWithLetter(char letter)
    {
        if(letter == 'A') return (new A()).GetAnimalsThatStartWithMe();
        if(letter == 'B') return (new B()).GetAnimalsThatStartWithMe();
        /* ... */
        if(letter == 'Z') return (new Z()).GetAnimalsThatStartWithMe();
        throw new ApplicationException("Letter " + letter + " not found");
    }
}

LetterUtility 应该负责缓存? ILetter 的每个实例都应该吗?

Should LetterUtility be responsible for caching? Should each individual instance of ILetter? Is there something else entirely that can be done?

我正试图简化此示例,因此这些示例函数不需要缓存。但是考虑一下我添加了一个使得(new C())。GetAnimalsThatStartWithMe()的类每次运行都需要10秒:

I'm trying to keep this example short, so these example functions don't need caching. But consider I add this class that makes (new C()).GetAnimalsThatStartWithMe() take 10 seconds every time it's run:

public class C : ILetter
{
    public string[] GetAnimalsThatStartWithMe()
    {
        Thread.Sleep(10000);
        return new [] { "Cat", "Capybara", "Clam" };
    }
}

我发现自己在努力使软件达到与并可能减少代码(在此示例中:将结果缓存在 LetterUtility 中),并一遍又一遍地执行相同的工作(在此示例中:每次 C )。

I find myself battling between making our software as fast as possible and maintaining less code (in this example: caching the result in LetterUtility) and doing the exact same work over and over (in this example: waiting 10 seconds every time C is used).

推荐答案


哪个层对这些用户可定义功能的结果进行缓存的最佳方法是什么?

Which layer is best responsible for caching of the results of these user-definable functions?

答案很明显:可以正确实现所需缓存策略的层是正确的层

正确的缓存策略必须具有两个特征:

A correct cache policy needs to have two characteristics:


  • 它绝不能提供过时的数据。它必须知道正在缓存的方法是否会产生不同的结果,并在调用者获取陈旧数据之前的某个时刻使缓存无效

  • It must never serve up stale data; it must know whether the method being cached is going to produce a different result, and invalidate the cache at some point before the caller would get stale data

代表用户有效地管理缓存的资源。没有过期策略且无限制增长的缓存具有另一个名称:我们通常称它们为内存泄漏。

It must manage cached resources efficiently on the user's behalf. A cache without an expiration policy that grows without bounds has another name: we usually call them "memory leaks".

您系统中的什么层知道问题缓存是否陈旧?的答案。和缓存太大了吗? 那是应该实现缓存的层。

What's the layer in your system that knows the answers to the questions "is the cache stale?" and "is the cache too big?" That's the layer that should implement the cache.

这篇关于缓存/记忆功能结果是谁的责任?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆