Skip to content

Commit

Permalink
implement support for hash index
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Katkov committed Oct 9, 2023
1 parent 79553e4 commit f88c0f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/solid_cache/store/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def delete_matched(matcher, options = {})
instrument :delete_matched, matcher do
raise ArgumentError, "Only strings are supported: #{matcher.inspect}" unless String === matcher
raise ArgumentError, "Strings cannot start with wildcards" if SQL_WILDCARD_CHARS.include?(matcher[0])
raise NotImplementedError, "Primary Key uses a Hash Index, delete_matched method is not supported in this case" if primary_key_using_hash_index?

options ||= {}
batch_size = options.fetch(:batch_size, 1000)
Expand Down Expand Up @@ -49,6 +50,10 @@ def clear(options = nil)
end

private
def primary_key_using_hash_index?
ActiveRecord::Base.connection.indexes(:activesupport_cache_entries).first {|i| i.columns.first == 'key'}.using == :hash
end

def read_entry(key, **options)
deserialize_entry(read_serialized_entry(key, **options), **options)
end
Expand Down
7 changes: 7 additions & 0 deletions test/unit/delete_matched_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class DeleteMatchedTest < ActiveSupport::TestCase
assert @cache.exist?(other_key)
end

test "will raise error if hash index is used" do
# TODO: change migration to have hash index.
assert_raise NotImplementedError do
@cache.delete_matched("foo")
end
end

test "fails when starts with %" do
assert_raise ArgumentError do
@cache.delete_matched("%foo")
Expand Down

0 comments on commit f88c0f6

Please sign in to comment.