TTL not working....
See original GitHub issueHeya, 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:
- Created 7 years ago
- Comments:8
Top GitHub Comments
I’m afraid I’m a bit confused about the whole mix of
expiredInterval
andttl
. Could you clear that up for me? If it’s not too much trouble. 😉added ttl to readme, closing…