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

Preload multiple attributes at once - N+1 queries #139

Open
Laykou opened this issue Jan 3, 2024 · 0 comments
Open

Preload multiple attributes at once - N+1 queries #139

Laykou opened this issue Jan 3, 2024 · 0 comments

Comments

@Laykou
Copy link

Laykou commented Jan 3, 2024

Is there a way to preload multiple attributes at once?

Having:

class Transaction < ApplicationRecord
  kredis_datetime :locked_at
end


Transaction.limit(100).each do |transaction|
  puts transaction.locked_at
end

triggers 100 Kredis Proxy .... calls

Can I somehow use e.g. redis.multi method to do this only once?

I tried wrapping it somehow into:

transactions = Transaction.limit(100).to_a

$redis.multi do
  transactions.each { |e| e.locked_at } # do the preload
end

# later I expected to use the `.locked_at` without additional call to Redis

but this resulted only in a warning:

Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.

redis.multi do
  redis.get("key")
end

should be replaced by

redis.multi do |pipeline|
  pipeline.get("key")
end

Thank you for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant