question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TTL not working....

See original GitHub issue

Heya, I’m having some issues with the TTL option of your module and was wondering if you could help me out.

I have a pretty basic setup of your module and basically have a function that checks if a value has been added to the storage yet. If this is the case it returns the key at which it’s stored. If not it creates a new key and adds the value to the storage then returns the newly created key to the callback function.

For this functionality I use the storage.forEach() method.

For testing purposes I set the TTL option in the initSync() function to 100. According to your documentation this should set the TTL to 100 milliseconds.

However when I run my testing function, a function that tries to add the same value to the storage every second, it returns the same KEY every single time.

Then there’s this quote from your docs that gets me even more confused:

With ttl (time to live), it is recommended that you use getItem(key, callback) or getItemSync(key) since, if a ttl of a certain key is expired the key-file is immediately deleted from disk, the callback will execute whenever that happends, if there is no ttl used or it has expired yet, the callback will also immediately execute in a synchronous fashion.

I really hope you’ll be able to help me get the TTL working. Thanks in advance!

My code

Down here I’ll share my source code, this might make it easier for you to see what’s going on.

This is my module:

var KEY = require('my-key-generation')

module.exports = function (PATH, TTL) {
  /* Initialize the storage */
  storage.initSync({
    dir: PATH,
    ttl: TTL
  })

  /* Add existing KEYs to our KEY generator: This way it knows what keys to skip.
   * NOTE: this function is sync, but that's okey. It'll only be called on init. */
  KEY.list = storage.keys()

  return {
    getKey: function (ID, callback) {
      var returnval, exists

      /* First check if the id is already stored. */
      storage.forEach(function (key, value) {
        if (value === ID) {
          /* We've already stored this ID */
          exists = true
          returnval = callback(null, key)
        }
      })

      if (exists) {
        return returnval
      }

      /* We've looped over the entire storage but not found our ID, let's add it */
      var newKey = KEY()
      storage.setItem(newKey, ID, function (err) {
        if (err) {
          returnval = callback(err)
        }
        returnval = callback(null, newKey)
      })

      return returnval
    }
  }
}

And my testfile:

var module = require('./index.js')('./tokenstorage', 100)

module.empty(function (err) {
  if (err) throw err
  setInterval(function () {
    module.getKey('myfirstID', function (err, KEY) {
      if (err) throw err
      console.log('KEY: ' + KEY)
    })
  }, 1000)
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jessevdpcommented, Feb 17, 2017

I’m afraid I’m a bit confused about the whole mix of expiredInterval and ttl. Could you clear that up for me? If it’s not too much trouble. 😉

0reactions
akhourycommented, Mar 8, 2017

added ttl to readme, closing…

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongodb TTL not working - Stack Overflow
TTL thread that removes expired documents runs every 60 seconds. ... The TTL collection monitor will not start because of this.
Read more >
Troubleshoot why items with expired TTL aren't deleted ... - AWS
To check if TTL is working properly, do the following: Be sure that you activated TTL on the table and the related settings...
Read more >
TTL Testing and Troubleshooting - Ikelite
If you're experiencing trouble getting a flash to fire in TTL (or at all), we recommend removing and reinstalling the camera's lens, battery...
Read more >
[Solved] DynamoDB TTL Not Working - Dynobase
There are multiple reasons that may cause TTL not to work. The first cause is TTL being disabled on the DynamoDB table. The...
Read more >
MongoDB TTL does not work on atlas but works fine on ...
I followed this article - https://docs.mongodb.com/manual/tutorial/expire-data/ I created the index on atlas first.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found