Skip to content

DATAREDIS-526 #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,14 @@ public Long getExpire(K key, final TimeUnit timeUnit) {
return execute(new RedisCallback<Long>() {

public Long doInRedis(RedisConnection connection) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getExpire(K, TimeUnit) has a bug if it's used with transactions/pipelining. It throws in the first place a NullPointerException. If the call is guarded against null access the results return the original ttl/pTtl value and not the converted one. I think we should move the conversion into the connections as that's the place where we're able to perform a conversion. Adding pTtl(K, TimeUnit) and ttl(K, TimeUnit) to RedisKeyCommands should fix that bug.

Long expire;
try {
return timeUnit.convert(connection.pTtl(rawKey), TimeUnit.MILLISECONDS);
expire = connection.pTtl(rawKey);
return expire < 0 ? expire : timeUnit.convert(expire, TimeUnit.MILLISECONDS);
} catch (Exception e) {
// Driver may not support pTtl or we may be running on Redis 2.4
return timeUnit.convert(connection.ttl(rawKey), TimeUnit.SECONDS);
expire = connection.ttl(rawKey);
return expire < 0 ? expire : timeUnit.convert(expire, TimeUnit.SECONDS);
}
}
}, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,39 @@ public void testGetExpireSeconds() {
assertEquals(Long.valueOf(1), redisTemplate.getExpire(key1, TimeUnit.SECONDS));
}

@Test
public void testGetExpireSecondsForKeyDoesNotExist() {
final K key1 = keyFactory.instance();
Long expire = redisTemplate.getExpire(key1, TimeUnit.SECONDS);
assertTrue(expire < 0l);
}

@Test
public void testGetExpireSecondsForKeyExistButHasNoAssociatedExpire() {
final K key1 = keyFactory.instance();
V value1 = valueFactory.instance();
redisTemplate.boundValueOps(key1).set(value1);
Long expire = redisTemplate.getExpire(key1, TimeUnit.SECONDS);
assertTrue(expire < 0l);
}


@Test
public void testGetExpireMillisForKeyDoesNotExist() {
final K key1 = keyFactory.instance();
Long expire = redisTemplate.getExpire(key1, TimeUnit.MILLISECONDS);
assertTrue(expire < 0l);
}

@Test
public void testGetExpireMillisForKeyExistButHasNoAssociatedExpire() {
final K key1 = keyFactory.instance();
V value1 = valueFactory.instance();
redisTemplate.boundValueOps(key1).set(value1);
Long expire = redisTemplate.getExpire(key1, TimeUnit.MILLISECONDS);
assertTrue(expire < 0l);
}

@Test
public void testGetExpireMillisNotSupported() {

Expand Down