Skip to content

Commit 75d34d9

Browse files
authored
Add expires_in support to kredis_counter attribute (#61)
1 parent 6c74e89 commit 75d34d9

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class Person < ApplicationRecord
171171
kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" }
172172
kredis_unique_list :skills, limit: 2
173173
kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
174+
kredis_counter :steps, expires_in: 1.hour
174175
end
175176

176177
person = Person.find(5)

lib/kredis/attributes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def kredis_slots(name, available:, key: nil, config: :shared, after_change: nil)
6262
kredis_connection_with __method__, name, key, available: available, config: config, after_change: after_change
6363
end
6464

65-
def kredis_counter(name, key: nil, config: :shared, after_change: nil)
66-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
65+
def kredis_counter(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
66+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
6767
end
6868

6969
def kredis_hash(name, key: nil, typed: :string, config: :shared, after_change: nil)

test/attributes_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Person
2323
kredis_set :vacations
2424
kredis_json :settings
2525
kredis_counter :amount
26+
kredis_counter :expiring_amount, expires_in: 1.second
2627
kredis_string :temporary_password, expires_in: 1.second
2728
kredis_hash :high_scores, typed: :integer
2829
kredis_boolean :onboarded
@@ -212,6 +213,13 @@ class AttributesTest < ActiveSupport::TestCase
212213
assert_equal 0, @person.amount.value
213214
end
214215

216+
test "counter with expires_at" do
217+
@person.expiring_amount.increment
218+
assert_changes "@person.expiring_amount.value", from: 1, to: 0 do
219+
sleep 1.1.seconds
220+
end
221+
end
222+
215223
test "hash" do
216224
@person.high_scores.update(space_invaders: 100, pong: 42)
217225
assert_equal({ "space_invaders" => 100, "pong" => 42 }, @person.high_scores.to_h)

0 commit comments

Comments
 (0)