connect-mongo autoRemoveInterval 无法正常工作

connect-mongo autoRemoveInterval does not work correctly

我的代码:(express.jsexpress-session

app.use(session({
  store: new MongoStore({
    mongooseConnection: db,
    autoRemove: 'interval',
    autoRemoveInterval: 1 // 1 minute (min)
  }),
  secret: 'someSecret',
  name: 'secret',
  genid: ()=>{
     return uuid4();
  },
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    sameSite: true,
    maxAge: 60*1000 // 1 minute (ms)
  }
}));

我已经配置 connect-mongo 以便它每分钟清理过期的会话。 cookie 的 maxAge 值也是一分钟。但由于某种原因 connect-mongo 不是在一分钟后清除会话,而是在一分二十秒后清除会话。 connect-mongo 正好在一分钟内清理干净对我来说很重要,但我不明白他为什么清理这么晚。对不起我的英语不好。非常感谢您的帮助。

根据 connect-mongo documentation,它使用 MongoDB TTL 索引使会话过期。

以下内容来自MongoDB documentation:

The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time a document expires and the time that MongoDB removes the document from the database.

The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.

Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.

使用内置的过期方法只能精确到一两分钟。如果您需要更精确地执行该操作,则需要在您的应用程序中实现该行为。