Connect-mongo sessions not being deleted automatically

前端 未结 2 1212
一个人的身影
一个人的身影 2021-01-31 21:29

I have an application using NodeJS, Express, MongoDB and connect-mongo.

My issue is that sessions don’t seem to be automatically deleted from MongoDB when they expire, s

2条回答
  •  情话喂你
    2021-01-31 22:33

    I don't know from where the clear_interval options was taken but I'm looking at the code of the latest version:

    class MongoStore extends Store {
     constructor(options) {
      options = options || {}
    
      /* Fallback */
      if (options.fallbackMemory && MemoryStore) {
        return new MemoryStore()
      }
    
      super(options)
    
      /* Options */
      this.ttl = options.ttl || 1209600 // 14 days
      this.collectionName = options.collection || 'sessions'
      this.autoRemove = options.autoRemove || 'native'
      this.autoRemoveInterval = options.autoRemoveInterval || 10
      this.transformFunctions = computeTransformFunctions(options, true)
    
      // ...
    
    setAutoRemoveAsync() {
      const removeQuery = {expires: {$lt: new Date()}}
      switch (this.autoRemove) {
        case 'native':
          return this.collection.createIndex({expires: 1}, {expireAfterSeconds: 0})
        case 'interval':
          this.timer = setInterval(() => this.collection.remove(removeQuery, {w: 0}), this.autoRemoveInterval * 1000 * 60)
          this.timer.unref()
          return Promise.resolve()
        default:
          return Promise.resolve()
      }
    }
    

    So the correct way to set auto remove based on that code seems to be:

    const store = new MongoStore({
      url: "put db connection string here ...",
      autoRemove: 'interval',     
      autoRemoveInterval: 60 * 24 // In minutes. Default
    })
    

    I dont see any trace of a clear_interval option, so it seems to me the suggested solution would have no effect ...

提交回复
热议问题