0

为非数据库操作实现缓存是否有意义?实现它以评估冗长的代码块是否有意义?例如,我有一个接受参数的代码块。基于这个参数,它会遍历 if/else 块的洗衣清单,直到它匹配正确的值并返回一个值。在我的例子中,IN 参数是有限的,OUT 参数基于 IN 参数是一致的,即对于每个“A”,返回是“B”,对于每个“X”,返回是“Z”。这种操作不涉及数据库记录。我想知道通过引入缓存,我可以从长期的评估中减少任何时间。建议?谢谢。

4

2 回答 2

1

只要执行的代码足够长或足够复杂以证明付出的努力是合理的,这就是完全合理的。例如,参见http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html上的 Spring 的缓存注释支持,它适用于 Java 方法级别。但是您可以在非 Spring 环境中轻松地自己实现这种方法。

您应该首先分析您的应用程序,以查看在受影响的代码块中花费了多少时间/CPU,以查看缓存在这里是否有意义。

于 2012-12-11T13:38:00.753 回答
0

It depends how you value low latency. According to this presentation from Google (slide #13), 1 MB of sequential read from the main memory costs about 0.25 ms, and there are plenty of other things which are more expensive. From the same slide:

  • a single disk seek is 40 times slower;
  • reading 1 MB of data from network is 40 times slower;
  • reading 1 MB data from disk is 120 times slower

So caching can help tremendously in certain situations, but you need to do analysis and see how much time is your algorithm wasting and then decide.

于 2012-12-18T22:09:57.657 回答