Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate key_hash_stage #145

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 13 additions & 46 deletions app/models/solid_cache/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Entry < Record
VALUE_BYTE_SIZE = 4
FIXED_SIZE_COLUMNS_BYTE_SIZE = ID_BYTE_SIZE + CREATED_AT_BYTE_SIZE + KEY_HASH_BYTE_SIZE + VALUE_BYTE_SIZE

self.ignored_columns += [ :key_hash, :byte_size] if SolidCache.configuration.key_hash_stage == :ignored

class << self
def write(key, value)
upsert_all_no_query_cache([ { key: key, value: value } ])
Expand All @@ -22,23 +20,23 @@ def write_multi(payloads)
end

def read(key)
result = select_all_no_query_cache(get_sql, lookup_value(key)).first
result = select_all_no_query_cache(get_sql, key_hash_for(key)).first
result[1] if result&.first == key
end

def read_multi(keys)
key_hashes = keys.map { |key| lookup_value(key) }
key_hashes = keys.map { |key| key_hash_for(key) }
results = select_all_no_query_cache(get_all_sql(key_hashes), key_hashes).to_h
results.except!(results.keys - keys)
end

def delete_by_key(key)
delete_no_query_cache(lookup_column, lookup_value(key))
delete_no_query_cache(:key_hash, key_hash_for(key))
end

def delete_multi(keys)
serialized_keys = keys.map { |key| lookup_value(key) }
delete_no_query_cache(lookup_column, serialized_keys)
serialized_keys = keys.map { |key| key_hash_for(key) }
delete_no_query_cache(:key_hash, serialized_keys)
end

def clear_truncate
Expand All @@ -52,7 +50,7 @@ def clear_delete
def increment(key, amount)
transaction do
uncached do
result = lock.where(lookup_column => lookup_value(key)).pick(:key, :value)
result = lock.where(key_hash: key_hash_for(key)).pick(:key, :value)
amount += result[1].to_i if result&.first == key
write(key, amount)
amount
Expand Down Expand Up @@ -85,65 +83,34 @@ def upsert_all_no_query_cache(payloads)
def add_key_hash_and_byte_size(payloads)
payloads.map do |payload|
payload.dup.tap do |payload|
if key_hash?
payload[:key_hash] = key_hash_for(payload[:key])
payload[:byte_size] = byte_size_for(payload)
end
payload[:key_hash] = key_hash_for(payload[:key])
payload[:byte_size] = byte_size_for(payload)
end
end
end

def key_hash?
@key_hash ||= [ :indexed, :unindexed ].include?(SolidCache.configuration.key_hash_stage) &&
connection.column_exists?(table_name, :key_hash)
end

def key_hash_indexed?
key_hash? && SolidCache.configuration.key_hash_stage == :indexed
end

def lookup_column
key_hash_indexed? ? :key_hash : :key
end

def lookup_value(key)
key_hash_indexed? ? key_hash_for(key) : to_binary(key)
end

def lookup_placeholder
key_hash_indexed? ? 1 : "placeholder"
end

def exec_query_method
connection.respond_to?(:internal_exec_query) ? :internal_exec_query : :exec_query
end

def upsert_unique_by
connection.supports_insert_conflict_target? ? lookup_column : nil
connection.supports_insert_conflict_target? ? :key_hash : nil
end

def upsert_update_only
if key_hash_indexed?
[ :key, :value, :byte_size ]
elsif key_hash?
[ :value, :key_hash, :byte_size ]
else
[ :value ]
end
[ :key, :value, :byte_size ]
end

def get_sql
@get_sql ||= {}
@get_sql[lookup_column] ||= build_sql(where(lookup_column => lookup_placeholder).select(:key, :value))
@get_sql ||= build_sql(where(key_hash: 1).select(:key, :value))
end

def get_all_sql(key_hashes)
if connection.prepared_statements?
@get_all_sql_binds ||= {}
@get_all_sql_binds[[key_hashes.count, lookup_column]] ||= build_sql(where(lookup_column => key_hashes).select(:key, :value))
@get_all_sql_binds[key_hashes.count] ||= build_sql(where(key_hash: key_hashes).select(:key, :value))
else
@get_all_sql_no_binds ||= {}
@get_all_sql_no_binds[lookup_column] ||= build_sql(where(lookup_column => [ lookup_placeholder, lookup_placeholder ]).select(:key, :value)).gsub("?, ?", "?")
@get_all_sql_no_binds ||= build_sql(where(key_hash: [ 1, 2 ]).select(:key, :value)).gsub("?, ?", "?")
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/solid_cache/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Engine < ::Rails::Engine

SolidCache.configuration = SolidCache::Configuration.new(**options)
end

if config.solid_cache.key_hash_stage
ActiveStorage.deprecator.warn("config.solid_cache.key_hash_stage is deprecated and has no effect.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this okay? Or should it be ActiveSupport?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yes 😅

end
end

initializer "solid_cache.app_executor", before: :run_prepare_callbacks do |app|
Expand Down