Skip to content

Commit

Permalink
ability to change index to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Katkov committed Oct 10, 2023
1 parent 8021da4 commit 8e1bb5d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ $ gem install solid_cache
Add the migration to your app:

```bash
$ bin/rails solid_cache:install:migrations
$ rake solid_cache:install
```

By default, this migration uses a btree index which descent through index to find a right entry. Hash index offers a faster record lookup, it’s O(1) operation that should improve lookup performance by 40-60%. But main drawback is that #delete_matched method will not be operational with hash index.

In case, you want to go with hash index run the following instead:

```bash
$ rake solid_cache:install[hash]
```

Then run it:
Expand Down
11 changes: 0 additions & 11 deletions db/migrate/20230724121448_create_solid_cache_entries.rb

This file was deleted.

18 changes: 14 additions & 4 deletions lib/generators/solid_cache/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require 'rails/generators/active_record'

class SolidCache::InstallGenerator < Rails::Generators::Base
class_option :skip_migrations, type: :boolean, default: nil,
desc: "Skip migrations"
include ActiveRecord::Generators::Migration

source_root File.expand_path("templates", __dir__)
class_option :skip_migrations, type: :boolean, default: nil, desc: "Skip migrations"
class_option :index, type: :string, default: 'btree', desc: "Index type for key column"

def add_rails_cache
%w[development test production].each do |env_name|
Expand All @@ -11,8 +16,13 @@ def add_rails_cache
end

def create_migrations
unless options[:skip_migrations]
rails_command "railties:install:migrations FROM=solid_cache", inline: true
return if options[:skip_migrations]

case options[:index]
when 'btree'
migration_template 'create_solid_cache_entries_btree.rb', 'db/migrate/create_solid_cache_entries.rb'
when 'hash'
migration_template 'create_solid_cache_entries_hash.rb', 'db/migrate/create_solid_cache_entries.rb'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSolidCacheEntries < ActiveRecord::Migration[7.0]
def change
create_table :solid_cache_entries do |t|
t.binary :key, null: false, limit: 1024
t.binary :value, null: false, limit: 512.megabytes
t.datetime :created_at, null: false

t.index :key, unique: true
end
end
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSolidCacheEntries < ActiveRecord::Migration[7.0]
def change
create_table :solid_cache_entries do |t|
t.binary :key, null: false, limit: 1024
t.binary :value, null: false, limit: 512.megabytes
t.datetime :created_at, null: false

t.index :key, unique: true, using: :hash
end
end
end

10 changes: 8 additions & 2 deletions lib/tasks/solid_cache_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
desc "Copy over the migration, and set cache"
namespace :solid_cache do
task :install do
Rails::Command.invoke :generate, [ "solid_cache:install" ]
task :install, [:index] do |_t, args|
args.with_defaults(index: 'btree')

if args[:index] == 'btree' || args[:index] == 'hash'
Rails::Command.invoke :generate, [ "solid_cache:install", "--index=#{args[:index]}" ]
else
abort "Invalid index type - only 'btree' and 'hash' are supported."
end
end
end
7 changes: 0 additions & 7 deletions test/unit/delete_matched_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ 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 8e1bb5d

Please sign in to comment.