Skip to content

Commit 08895e0

Browse files
committed
Cast ttl argument to integer in expire, setex and a few others.
While migrating a big Rails app I noticed that it was extremely frequent to pass `ActiveSupport::Duration` instances as `ttl` argument. We already cast a few arguments explictly like this, so I think it's worth it here.
1 parent ce508dc commit 08895e0

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Cast `ttl` argument to integer in `expire`, `setex` and a few others.
4+
35
# 5.0.3
46

57
- Add `OutOfMemoryError` as a subclass of `CommandError`

lib/redis/commands/keys.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def persist(key)
7676
# - `:lt => true`: Set expiry only when the new expiry is less than current one.
7777
# @return [Boolean] whether the timeout was set or not
7878
def expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil)
79-
args = [:expire, key, seconds]
79+
args = [:expire, key, Integer(seconds)]
8080
args << "NX" if nx
8181
args << "XX" if xx
8282
args << "GT" if gt
@@ -96,7 +96,7 @@ def expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil)
9696
# - `:lt => true`: Set expiry only when the new expiry is less than current one.
9797
# @return [Boolean] whether the timeout was set or not
9898
def expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
99-
args = [:expireat, key, unix_time]
99+
args = [:expireat, key, Integer(unix_time)]
100100
args << "NX" if nx
101101
args << "XX" if xx
102102
args << "GT" if gt
@@ -132,7 +132,7 @@ def ttl(key)
132132
# - `:lt => true`: Set expiry only when the new expiry is less than current one.
133133
# @return [Boolean] whether the timeout was set or not
134134
def pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil)
135-
args = [:pexpire, key, milliseconds]
135+
args = [:pexpire, key, Integer(milliseconds)]
136136
args << "NX" if nx
137137
args << "XX" if xx
138138
args << "GT" if gt
@@ -152,7 +152,7 @@ def pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil)
152152
# - `:lt => true`: Set expiry only when the new expiry is less than current one.
153153
# @return [Boolean] whether the timeout was set or not
154154
def pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
155-
args = [:pexpireat, key, ms_unix_time]
155+
args = [:pexpireat, key, Integer(ms_unix_time)]
156156
args << "NX" if nx
157157
args << "XX" if xx
158158
args << "GT" if gt

lib/redis/commands/strings.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, ke
105105
# @param [String] value
106106
# @return [String] `"OK"`
107107
def setex(key, ttl, value)
108-
send_command([:setex, key, ttl, value.to_s])
108+
send_command([:setex, key, Integer(ttl), value.to_s])
109109
end
110110

111111
# Set the time to live in milliseconds of a key.
@@ -115,7 +115,7 @@ def setex(key, ttl, value)
115115
# @param [String] value
116116
# @return [String] `"OK"`
117117
def psetex(key, ttl, value)
118-
send_command([:psetex, key, ttl, value.to_s])
118+
send_command([:psetex, key, Integer(ttl), value.to_s])
119119
end
120120

121121
# Set the value of a key, only if the key does not exist.

0 commit comments

Comments
 (0)