From e7e0450ca5e2bfa6711306fe4e310237d661e84e Mon Sep 17 00:00:00 2001 From: Miller Date: Fri, 8 Mar 2024 16:00:52 +0900 Subject: [PATCH 1/4] # Motivation - Support `include?` method for `Kredis::Types::List` # Modification - Allow `List` to proxy `lpos` (refer: https://redis.io/commands/lpos/) # Result - Users can use `include?` without load members of list to memory --- lib/kredis/types/list.rb | 6 +++++- test/types/list_test.rb | 8 ++++++++ test/types/unique_list_test.rb | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/kredis/types/list.rb b/lib/kredis/types/list.rb index 0c1e06f..854fa77 100644 --- a/lib/kredis/types/list.rb +++ b/lib/kredis/types/list.rb @@ -3,7 +3,7 @@ class Kredis::Types::List < Kredis::Types::Proxying prepend Kredis::DefaultValues - proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del + proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :lpos attr_accessor :typed @@ -33,6 +33,10 @@ def last(n = nil) n ? lrange(-n, -1) : lrange(-1, -1).first end + def include?(element) + !lpos(element).nil? + end + private def set_default append default diff --git a/test/types/list_test.rb b/test/types/list_test.rb index 7507548..75d6cfb 100644 --- a/test/types/list_test.rb +++ b/test/types/list_test.rb @@ -131,4 +131,12 @@ class ListTest < ActiveSupport::TestCase assert_equal [ 0, 1, 2, 3, 4, 10, 20, 30 ], Kredis.list("mylist", typed: :integer).to_a.sort end + + test "include?" do + list = Kredis.list "int-list", typed: :integer + list.append [ 1, 2, 3 ] + + assert list.include?(1) + assert_not list.include?(4) + end end diff --git a/test/types/unique_list_test.rb b/test/types/unique_list_test.rb index 8ac91ed..c45fbf0 100644 --- a/test/types/unique_list_test.rb +++ b/test/types/unique_list_test.rb @@ -93,4 +93,10 @@ class UniqueListTest < ActiveSupport::TestCase @list.prepend(%w[ 6 7 8 ]) assert_equal %w[ 8 7 6 1 2 3 ], @list.elements end + + test "include?" do + @list.append(%w[ 1 2 3 ]) + assert @list.include?("1") + assert_not @list.include?("4") + end end From c107f542471ab94e46de457000a3526d97a796aa Mon Sep 17 00:00:00 2001 From: Miller Date: Fri, 8 Mar 2024 16:03:49 +0900 Subject: [PATCH 2/4] Document `List#include?` --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 66fcbf7..981bd1a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ integer_list = Kredis.list "myintegerlist", typed: :integer, default: [ 1, 2, 3 integer_list.append([ 4, 5, 6 ]) # => RPUSH myintegerlist "4" "5" "6" integer_list << 7 # => RPUSH myintegerlist "7" [ 1, 2, 3, 4, 5, 6, 7 ] == integer_list.elements # => LRANGE myintegerlist 0 -1 +integer_list.include? 7 unique_list = Kredis.unique_list "myuniquelist" unique_list.append(%w[ 2 3 4 ]) # => LREM myuniquelist 0, "2" + LREM myuniquelist 0, "3" + LREM myuniquelist 0, "4" + RPUSH myuniquelist "2", "3", "4" From e7bd0987a5b58262de048fd82f72956eb4f9204b Mon Sep 17 00:00:00 2001 From: Miller Date: Fri, 8 Mar 2024 16:09:27 +0900 Subject: [PATCH 3/4] Update TC --- test/types/unique_list_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/unique_list_test.rb b/test/types/unique_list_test.rb index c45fbf0..60226f8 100644 --- a/test/types/unique_list_test.rb +++ b/test/types/unique_list_test.rb @@ -95,7 +95,7 @@ class UniqueListTest < ActiveSupport::TestCase end test "include?" do - @list.append(%w[ 1 2 3 ]) + @list = Kredis.unique_list "myuniquelist", default: %w[ 1 2 3 ] assert @list.include?("1") assert_not @list.include?("4") end From cc286644063aadc207ce0bbeff7926b6bbd1f0a6 Mon Sep 17 00:00:00 2001 From: Miller Date: Sun, 17 Mar 2024 14:20:10 +0900 Subject: [PATCH 4/4] Update document --- README.md | 2 +- lib/kredis/types/list.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 981bd1a..a4af3e5 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ integer_list = Kredis.list "myintegerlist", typed: :integer, default: [ 1, 2, 3 integer_list.append([ 4, 5, 6 ]) # => RPUSH myintegerlist "4" "5" "6" integer_list << 7 # => RPUSH myintegerlist "7" [ 1, 2, 3, 4, 5, 6, 7 ] == integer_list.elements # => LRANGE myintegerlist 0 -1 -integer_list.include? 7 +integer_list.include? 7 # => LPOS myintegerlist 7, Requires Redis 6+ unique_list = Kredis.unique_list "myuniquelist" unique_list.append(%w[ 2 3 4 ]) # => LREM myuniquelist 0, "2" + LREM myuniquelist 0, "3" + LREM myuniquelist 0, "4" + RPUSH myuniquelist "2", "3", "4" diff --git a/lib/kredis/types/list.rb b/lib/kredis/types/list.rb index 854fa77..500031b 100644 --- a/lib/kredis/types/list.rb +++ b/lib/kredis/types/list.rb @@ -33,6 +33,7 @@ def last(n = nil) n ? lrange(-n, -1) : lrange(-1, -1).first end + # Require Redis 6+ for LPOS def include?(element) !lpos(element).nil? end