memcached icon indicating copy to clipboard operation
memcached copied to clipboard

Timestamp based expiration doesn't seem to work

Open srcspider opened this issue 7 years ago • 3 comments

Unless I'm missing something here, timestamps don't seem to work.

const Memcached = require('memcached');
const memcached = new Memcached();

function nowInSeconds() {
	return Math.floor(Date.now() / 1000);
}

memcached.set('xxx', 'yyy', nowInSeconds() + 1, err => {
	setTimeout(() => {
		memcached.get('xxx', (err, value) => {
			if (value == 'yyy') {
				console.log('ERROR, GOT VALUE')
			}
			else {
				console.log('OK, NO VALUE')
			}
		})
	}, 3000)
})

Result

ERROR, GOT VALUE

As a side note, if the ttl is in seconds since now (which is to say below "30days" in seconds), ie. memcached.set('xxx', 'yyy', 1, err => { that works fine.

srcspider avatar Jan 10 '18 11:01 srcspider

Addendum: Just for clarity:

https://github.com/memcached/memcached/wiki/Programming#expiration

Expiration times are specified in unsigned integer seconds. They can be set from 0, meaning "never expire", to 30 days (60*60*24*30). Any time higher than 30 days is interpreted as a unix timestamp date. If you want to expire an object on january 1st of next year, this is how you do that.

srcspider avatar Jan 10 '18 11:01 srcspider

Could you give me a hint about which version? I tested v2.2.2. It works fine with your case.

Can reproduce herenode issues/321/index.js

mapleeit avatar Oct 03 '18 09:10 mapleeit

I found a strange result today by accident. It seems that there may be some delay before Memcached clear the value.

it('set unix timestamp expire should be correct', async () => {
    const memcached = new Memcached(common.servers.single)
    const testnr = ++(global as any).testnumbers

    // get next 1 second unix timestamp
    const unixNow = Math.floor(Number(new Date()) / 1000) + 1
    console.log('set value expired at: ')
    console.log(unixNow * 1000)
    return memcached.set(`test:${testnr}`, 'value', unixNow).then((ok) => {
        console.log('set success at:')
        console.log(new Date().getTime())
        // after 1.1 seconds, should be expired
        return common.wait(1100).then(() => {
            console.log('retrieve value at: ')
            console.log(new Date().getTime())
            return memcached.get(`test:${testnr}`).then((value) => {
                console.log(value)
                assert.isUndefined(value)
            })
        })
    })
})

This is the result of test case above. The value should be undefined normally.

set value expired at:
1538643191000
set success at:
1538643190464
retrieve value at:
1538643191564
value

mapleeit avatar Oct 04 '18 09:10 mapleeit