Skip to content

Commit 2dc3745

Browse files
committed
Support ttl in hash
1 parent 48801cf commit 2dc3745

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
177177
false == limiter.exceeded? # => GET "limiter"
178178
```
179179

180-
Lists, unique lists, sets, and ordered sets support expiration:
180+
Lists, unique lists, sets, ordered sets, and hashes support expiration:
181181

182182
```ruby
183183
set = Kredis.set "myset", expires_in: 1.second

lib/kredis/attributes.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def kredis_limiter(name, limit:, key: nil, config: :shared, after_change: nil, e
7676
kredis_connection_with __method__, name, key, limit: limit, config: config, after_change: after_change, expires_in: expires_in
7777
end
7878

79-
def kredis_hash(name, key: nil, default: nil, typed: :string, config: :shared, after_change: nil)
80-
kredis_connection_with __method__, name, key, default: default, typed: typed, config: config, after_change: after_change
79+
def kredis_hash(name, key: nil, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil)
80+
kredis_connection_with __method__, name, key, default: default, typed: typed, config: config, after_change: after_change, expires_in: expires_in
8181
end
8282

8383
def kredis_boolean(name, key: nil, default: nil, config: :shared, after_change: nil, expires_in: nil)

lib/kredis/types.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def enum(key, values:, default:, config: :shared, after_change: nil)
5757
type_from(Enum, config, key, after_change: after_change, values: values, default: default)
5858
end
5959

60-
def hash(key, typed: :string, default: nil, config: :shared, after_change: nil)
61-
type_from(Hash, config, key, after_change: after_change, default: default, typed: typed)
60+
def hash(key, typed: :string, default: nil, config: :shared, after_change: nil, expires_in: nil)
61+
type_from(Hash, config, key, after_change: after_change, default: default, typed: typed, expires_in: expires_in)
6262
end
6363

6464
def list(key, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil)

lib/kredis/types/hash.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class Kredis::Types::Hash < Kredis::Types::Proxying
66
prepend Kredis::DefaultValues
7+
include Kredis::Expiration
78

89
proxying :hget, :hset, :hmget, :hdel, :hgetall, :hkeys, :hvals, :del, :exists?
910

@@ -18,7 +19,9 @@ def []=(key, value)
1819
end
1920

2021
def update(**entries)
21-
hset entries.transform_values { |val| type_to_string(val, typed) }.compact if entries.flatten.any?
22+
with_expiration do
23+
hset entries.transform_values { |val| type_to_string(val, typed) }.compact if entries.flatten.any?
24+
end
2225
end
2326

2427
def values_at(*keys)

test/attributes_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Person
4646
kredis_string :temporary_password, expires_in: 1.second
4747
kredis_hash :high_scores, typed: :integer
4848
kredis_hash :high_scores_with_default_via_lambda, typed: :integer, default: ->(p) { { high_score: JSON.parse(p.scores).max } }
49+
kredis_hash :high_scores_with_ttl, typed: :integer, expires_in: 1.second
4950
kredis_boolean :onboarded
5051
kredis_boolean :adult_with_default_via_lambda, default: ->(p) { Date.today.year - p.birthdate.year >= 18 }
5152
kredis_limiter :update_limit, limit: 3, expires_in: 1.second
@@ -404,6 +405,14 @@ class AttributesTest < ActiveSupport::TestCase
404405
assert_equal({ "high_score" => 28 }, @person.high_scores_with_default_via_lambda.to_h)
405406
end
406407

408+
test "hash with ttl" do
409+
@person.high_scores_with_ttl.update(the_lost_viking: 99)
410+
assert_equal({ "the_lost_viking" => 99 }, @person.high_scores_with_ttl.to_h)
411+
412+
sleep 1.1
413+
assert_equal({}, @person.high_scores_with_ttl.to_h)
414+
end
415+
407416
test "boolean" do
408417
@person.onboarded.value = true
409418
assert @person.onboarded.value

test/types/hash_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,14 @@ class HashTest < ActiveSupport::TestCase
130130
assert_nil @hash["key"]
131131
assert_equal "value2", @hash["key2"]
132132
end
133+
134+
test "support ttl" do
135+
@hash = Kredis.hash "myhash", expires_in: 1.second
136+
@hash[:key] = :value
137+
@hash.update("key2" => "value2", "key3" => "value3")
138+
assert_equal "value", @hash[:key]
139+
140+
sleep 1.1
141+
assert_equal [], @hash.keys
142+
end
133143
end

0 commit comments

Comments
 (0)