缓存失效,有没有一种通用的解决方案?缓存、解决方案

由网友(不屑沵旳回眸)分享简介:只有两种计算机科学难题:缓存失效和命名的事情"There are only two hard problems in Computer Science: cache invalidation and naming things."菲尔Karlton 的有没有到一个无效缓存中通用的解决方案或方法;要知道,当一个条目是陈...

只有两种计算机科学难题:缓存失效和命名的事情

"There are only two hard problems in Computer Science: cache invalidation and naming things."

菲尔Karlton 的

有没有到一个无效缓存中通用的解决方案或方法;要知道,当一个条目是陈旧的,所以你保证始终得到新鲜的数据?

Is there a general solution or method to invalidating a cache; to know when an entry is stale, so you are guaranteed to always get fresh data?

例如,假设一个函数的getData()从一个文件中获取数据。 它缓存它基于文件,它会检查每一个它被称为时间的最后修改时间。 然后添加第二个函数 transformData()该转换数据,并缓存其结果在下一次函数被调用。它不知道该文件的 - 你怎么添加的依赖,如果文件被更改,这个缓存是无效

For example, consider a function getData() that gets data from a file. It caches it based on the last modified time of the file, which it checks every time it's called. Then you add a second function transformData() which transforms the data, and caches its result for next time the function is called. It has no knowledge of the file - how do you add the dependency that if the file is changed, this cache becomes invalid?

您可以致电的getData()每次 transformData()被调用,它是这样的值进行比较用于构建缓存,但是这可能最终会被非常昂贵的。

You could call getData() every time transformData() is called and compare it with the value that was used to build the cache, but that could end up being very costly.

推荐答案

你所谈论的是一辈子的依赖链,即有一件事是依赖于其他可它的控制范围之外进行修改。

What you are talking about is lifetime dependency chaining, that one thing is dependent on another which can be modified outside of it's control.

如果您有一个从幂函数, A B C 的地方,如果 A B 是相同的,那么 C 是相同的,但检查的成本 B 高,那么您:

If you have an idempotent function from a, b to c where, if a and b are the same then c is the same but the cost of checking b is high then you either:

在接受你的某个时候与过时的信息操作,不经常检查 B 请你最好的水平,使检查 B 尽快 accept that you sometime operate with out of date information and do not always check b do your level best to make checking b as fast as possible

您不能有你的蛋糕和熊掌兼得......

You cannot have your cake and eat it...

如果你可以叠加的基础上额外的缓存洁癖那么这会影响最初的问题不是一个位。如果你选择了1,那么您有任何的自由,你给了自己,因此可以缓存更多,但一定要记得考虑缓存值b 的有效性。如果你选择了2,你仍然必须检查 B 每一次,但可以依傍的缓存 A 如果 B 检查出来。

If you can layer an additional cache based on a over the top then this affects the initial problem not one bit. If you chose 1 then you have whatever freedom you gave yourself and can thus cache more but must remember to consider the validity of the cached value of b. If you chose 2 you must still check b every time but can fall back on the cache for a if b checks out.

如果您图层缓存,你必须考虑你是否已经违反了系统的规则的行为相结合的结果。

If you layer caches you must consider whether you have violated the 'rules' of the system as a result of the combined behaviour.

如果你知道 A 始终有有效期,如果 B 没有,那么你可以安排你的缓存,像这样(伪code):

If you know that a always has validity if b does then you can arrange your cache like so (pseudocode):

private map<b,map<a,c>> cache // 
private func realFunction    // (a,b) -> c

get(a, b) 
{
    c result;
    map<a,c> endCache;
    if (cache[b] expired or not present)
    {
        remove all b -> * entries in cache;   
        endCache = new map<a,c>();      
        add to cache b -> endCache;
    }
    else
    {
        endCache = cache[b];     
    }
    if (endCache[a] not present)     // important line
        result = realFunction(a,b); 
        endCache[a] = result;
    else   
        result = endCache[a];
   return result;
}

显然,连续的层次感(比如 X )是平凡的,只要在每一个阶段,新添加的输入的有效性相匹配的 A B 的关系 X B X A

Obviously successive layering (say x) is trivial so long as, at each stage the validity of the newly added input matches the a:b relationship for x:b and x:a.

不过这是很可能的,你可以得到三个输入其效力是完全独立的(或者是循环的),所以没有层次感将是可能的。这意味着线条为标志//重要将不得不改变,以

However it is quite possible that you could get three inputs whose validity was entirely independent (or was cyclic), so no layering would be possible. This would mean the line marked // important would have to change to

如果(在endCache [A] 过期或没有present)

if (endCache[a] expired or not present)

阅读全文

相关推荐

最新文章