Skip to content

Commit 52f9c7f

Browse files
committed
Support expiration in UniqueList
1 parent 52fc9ab commit 52f9c7f

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

lib/kredis/expiration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Kredis::Expiration
99
end
1010

1111
private
12-
def with_expiration(&block)
12+
def with_expiration(suppress: false, &block)
1313
result = block.call
14-
if expires_in && ttl < 0
14+
if !suppress && expires_in && ttl < 0
1515
expire expires_in.to_i
1616
end
1717
result

lib/kredis/types.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def list(key, default: nil, typed: :string, config: :shared, after_change: nil,
6565
type_from(List, config, key, after_change: after_change, default: default, typed: typed, expires_in: expires_in)
6666
end
6767

68-
def unique_list(key, default: nil, typed: :string, limit: nil, config: :shared, after_change: nil)
69-
type_from(UniqueList, config, key, after_change: after_change, default: default, typed: typed, limit: limit)
68+
def unique_list(key, default: nil, typed: :string, limit: nil, config: :shared, after_change: nil, expires_in: nil)
69+
type_from(UniqueList, config, key, after_change: after_change, default: default, typed: typed, limit: limit, expires_in: expires_in)
7070
end
7171

7272
def set(key, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil)

lib/kredis/types/list.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ def remove(*elements)
1717
types_to_strings(elements, typed).each { |element| lrem 0, element }
1818
end
1919

20-
def prepend(*elements)
20+
def prepend(*elements, suppress_expiration: false)
2121
return if elements.flatten.empty?
2222

23-
with_expiration do
23+
with_expiration(suppress: suppress_expiration) do
2424
lpush types_to_strings(elements, typed)
2525
end
2626
end
2727

28-
def append(*elements)
28+
def append(*elements, suppress_expiration: false)
2929
return if elements.flatten.empty?
3030

31-
32-
with_expiration do
31+
with_expiration(suppress: suppress_expiration) do
3332
rpush types_to_strings(elements, typed)
3433
end
3534
end

lib/kredis/types/unique_list.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@
33
# You'd normally call this a set, but Redis already has another data type for that
44
class Kredis::Types::UniqueList < Kredis::Types::List
55
proxying :multi, :ltrim, :exists?
6+
include Kredis::Expiration
67

78
attr_accessor :typed, :limit
89

910
def prepend(elements)
1011
elements = Array(elements).uniq
1112
return if elements.empty?
1213

13-
multi do
14-
remove elements
15-
super
16-
ltrim 0, (limit - 1) if limit
14+
with_expiration do
15+
16+
multi do
17+
remove elements
18+
super(elements, suppress_expiration: true)
19+
ltrim 0, (limit - 1) if limit
20+
end
1721
end
1822
end
1923

2024
def append(elements)
2125
elements = Array(elements).uniq
2226
return if elements.empty?
2327

24-
multi do
25-
remove elements
26-
super
27-
ltrim(-limit, -1) if limit
28+
with_expiration do
29+
30+
multi do
31+
remove elements
32+
super(elements, suppress_expiration: true)
33+
ltrim(-limit, -1) if limit
34+
end
2835
end
2936
end
3037
alias << append

test/types/unique_list_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "active_support/core_ext/integer"
45

56
class UniqueListTest < ActiveSupport::TestCase
67
setup { @list = Kredis.unique_list "myuniquelist", limit: 5 }
@@ -14,6 +15,16 @@ class UniqueListTest < ActiveSupport::TestCase
1415
assert_equal %w[ 1 2 3 4 5 ], @list.elements
1516
end
1617

18+
test "append with expiration" do
19+
@list = Kredis.unique_list "xs", limit: 5, expires_in: 1.second
20+
21+
@list.append(%w[ 1 2 3 ])
22+
assert_equal %w[ 1 2 3 ], @list.elements
23+
24+
sleep 1.1
25+
assert @list.elements.empty?
26+
end
27+
1728
test "prepend" do
1829
@list.prepend(%w[ 1 2 3 ])
1930
@list.prepend(%w[ 1 2 3 4 ])

0 commit comments

Comments
 (0)