Skip to content

Commit

Permalink
Support expiration in UniqueList
Browse files Browse the repository at this point in the history
  • Loading branch information
heka1024 committed Feb 1, 2024
1 parent ace0dfb commit 6fd4413
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/kredis/expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Kredis::Expiration
end

private
def with_expiration(&block)
def with_expiration(suppress: false, &block)
result = block.call
if expires_in && ttl < 0
if !suppress && expires_in && ttl < 0
expire expires_in.to_i
end
result
Expand Down
4 changes: 2 additions & 2 deletions lib/kredis/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def list(key, default: nil, typed: :string, config: :shared, after_change: nil,
type_from(List, config, key, after_change: after_change, default: default, typed: typed, expires_in: expires_in)
end

def unique_list(key, default: nil, typed: :string, limit: nil, config: :shared, after_change: nil)
type_from(UniqueList, config, key, after_change: after_change, default: default, typed: typed, limit: limit)
def unique_list(key, default: nil, typed: :string, limit: nil, config: :shared, after_change: nil, expires_in: nil)
type_from(UniqueList, config, key, after_change: after_change, default: default, typed: typed, limit: limit, expires_in: expires_in)
end

def set(key, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil)
Expand Down
9 changes: 4 additions & 5 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ def remove(*elements)
types_to_strings(elements, typed).each { |element| lrem 0, element }
end

def prepend(*elements)
def prepend(*elements, suppress_expiration: false)
return if elements.flatten.empty?

with_expiration do
with_expiration(suppress: suppress_expiration) do
lpush types_to_strings(elements, typed)
end
end

def append(*elements)
def append(*elements, suppress_expiration: false)
return if elements.flatten.empty?


with_expiration do
with_expiration(suppress: suppress_expiration) do
rpush types_to_strings(elements, typed)
end
end
Expand Down
23 changes: 15 additions & 8 deletions lib/kredis/types/unique_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@
# You'd normally call this a set, but Redis already has another data type for that
class Kredis::Types::UniqueList < Kredis::Types::List
proxying :multi, :ltrim, :exists?
include Kredis::Expiration

attr_accessor :typed, :limit

def prepend(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
super
ltrim 0, (limit - 1) if limit
with_expiration do

multi do
remove elements
super(elements, suppress_expiration: true)
ltrim 0, (limit - 1) if limit
end
end
end

def append(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
super
ltrim(-limit, -1) if limit
with_expiration do

multi do
remove elements
super(elements, suppress_expiration: true)
ltrim(-limit, -1) if limit
end
end
end
alias << append
Expand Down
11 changes: 11 additions & 0 deletions test/types/unique_list_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "test_helper"
require "active_support/core_ext/integer"

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

test "append with expiration" do
@list = Kredis.unique_list "xs", limit: 5, expires_in: 1.second

@list.append(%w[ 1 2 3 ])
assert_equal %w[ 1 2 3 ], @list.elements

sleep 1.1
assert @list.elements.empty?
end

test "prepend" do
@list.prepend(%w[ 1 2 3 ])
@list.prepend(%w[ 1 2 3 4 ])
Expand Down

0 comments on commit 6fd4413

Please sign in to comment.