SetWithTTL doesn't work

TTL doesn’t work with Get. It only gets cleaned up after cleanupTicker .
The below test fails.

func TestCacheWithTTL(t testing.T) {
    c, err := ristretto.NewCache(&ristretto.Config{
        NumCounters:        100,
        MaxCost:            10,
        IgnoreInternalCost: true,
        BufferItems:        64,
        Metrics:            true,
    })

    require.NoError(t, err)

    // Set initial value for key = 1
    insert := c.SetWithTTL(1, 1, 1, 800 * time.Millisecond)
    require.True(t, insert)

    time.Sleep(100 * time.Millisecond)

    // Get value from cache for key = 1
    val, ok := c.Get(1)
    require.True(t, ok)
    require.NotNil(t, val)
    require.Equal(t, 1, val)

    time.Sleep(1200 * time.Millisecond)

    val, ok = c.Get(1)
    require.False(t, ok)
    require.Nil(t, val)
}

@ibrahim @mrjn

@karlmcguire

1 Like

Looking into this, thanks for the report @Atul! I was able to reproduce it.

1 Like

I resolved this issue at my local machine, so I sent PR.

2 Likes

Thanks for the great PR @warashi! The fix has been merged into master. @Atul your test should pass now.