【问题标题】:Node cache doesn't fire the "on expired" callback as intended节点缓存不会按预期触发“过期”回调
【发布时间】:2020-12-16 11:34:15
【问题描述】:

我正在尝试使用节点缓存的“过期”事件在缓存键值对的 TTL 结束时提取最新数据。 我的理解是它应该在缓存过期时立即调用,但我认为它不会被调用,直到检查期结束并且缓存清除过期值。是这样还是我做错了什么?

https://www.npmjs.com/package/node-cache#expired

【问题讨论】:

    标签: node.js caching


    【解决方案1】:

    默认情况下,node-cache 不会定期检查过期键,因此只有在访问键时才会触发 on expired 事件。您可以通过在创建节点缓存​​时设置 checkperiod 选项来更改此行为。

    checkperiod: (default: 600) The period in seconds, as a number, used for the automatic delete check interval. 0 = no periodic check.
    
    const NodeCache = require( "node-cache" );
    const myCache = new NodeCache({ checkperiod: 120 }); // will check every 120 seconds
    

    Link the doc explaining this.

    【讨论】:

      【解决方案2】:

      我也在努力实现这一目标。正如您所描述的,我的经验如下:

      我的理解是当缓存时它应该立即被调用 过期,但我认为它不会被调用,直到检查期结束并且 缓存清除过期值

      【讨论】:

      • 我不确定npmjs.com/package/node-cache#options 下的 enableLegacyCallbacks 是否对此有任何影响。我的目标是在一段时间后刷新缓存。所以我借助 has (Key) 函数和节点上的一些 cron 实现来刷新缓存。
      猜你喜欢
      • 2016-05-02
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多