Cache Invalidation — Is there a General Solution?

前端 未结 9 1383
情深已故
情深已故 2020-11-30 16:36

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

Phil Karlton

Is there a

9条回答
  •  猫巷女王i
    2020-11-30 17:37

    There is no general solution but:

    • You cache can act as a proxy (pull). Assume your cache knows the last origin change's timestamp, when someone call getData(), the cache ask the origin for it's last change's timestamp, if the same, it returns the cache, otherwise it updates its content with the source one and return its content. (A variation is the client to directly send the timestamp on the request, the source would only return content if its timestamp is different.)

    • You can still use a notification process (push), the cache observe the source, if the source changes, it sends a notification to the cache which is then flagged as "dirty". If someone calls getData() the cache will first get updated to the source, remove the "dirty" flag; then return its content.

    The choice generally speaking depends on:

    • The frequency: many calls on getData() would prefer a push so to avoid the source to be flooded by a getTimestamp function
    • Your access to the source: Are you owning the source model ? If not, chances are you cannot add any notification process.

    Note: As using the timestamp is the traditional way http proxies are working, another approach is sharing a hash of the content stored. The only way I know for 2 entities to get updated together are either I call you (pull) or you call me… (push) that's all.

提交回复
热议问题