Skip to content

Commit 5e9297f

Browse files
authored
Merge pull request #209 from david-quennoz-reup/main
fix: don't reset expiration when TTL is zero
2 parents d5e6e9c + 54d916d commit 5e9297f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

source/scripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const scripts = {
99
increment: `
1010
local totalHits = redis.call("INCR", KEYS[1])
1111
local timeToExpire = redis.call("PTTL", KEYS[1])
12-
if timeToExpire <= 0 or ARGV[1] == "1"
12+
if timeToExpire < 0 or ARGV[1] == "1"
1313
then
1414
redis.call("PEXPIRE", KEYS[1], tonumber(ARGV[2]))
1515
timeToExpire = tonumber(ARGV[2])

test/store-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,28 @@ describe('redis store test', () => {
200200
expect(await client.get('rl:test-store-two')).toEqual(null)
201201
})
202202

203+
it.skip('do not reset the expiration when the ttl is very close to 0', async () => {
204+
const store = new RedisStore({ sendCommand })
205+
const windowMs = 60
206+
store.init({ windowMs } as Options)
207+
208+
const key = 'test-store'
209+
await store.increment(key)
210+
211+
// FIXME: This makes the mock client return ttl = 1, not 0. So does setting
212+
// the ttl via client.pexpire to 1. Setting the ttl to 0 makes it expire
213+
// instantly, so the ttl returned is -2. If you can figure out a way to
214+
// consistently reproduce the close-to-0 behaviour with the mock client,
215+
// replace the advanceTimersByTime call with it.
216+
jest.advanceTimersByTime(59)
217+
await store.increment(key)
218+
219+
// Ensure the hit count is 2, and the expiry is not reset
220+
expect(Number(await client.pttl('rl:test-store'))).not.toEqual(windowMs)
221+
expect(Number(await client.pttl('rl:test-store'))).toBeLessThanOrEqual(0)
222+
expect(Number(await client.get('rl:test-store'))).toEqual(2)
223+
})
224+
203225
it('default export works', async () => {
204226
const store = new DefaultExportRedisStore({ sendCommand })
205227
store.init({ windowMs: 10 } as Options)

0 commit comments

Comments
 (0)