Module: Protocol::Redis::Cluster::Methods::Generic
- Defined in:
- lib/protocol/redis/cluster/methods/generic.rb
Overview
Provides generic Redis commands for cluster environments. These methods distribute operations across cluster nodes based on key slots.
Instance Method Summary collapse
-
#del(*keys, role: :master, **options) ⇒ Object
Delete one or more keys from the cluster.
-
#exists(*keys, role: :master, **options) ⇒ Object
Check existence of one or more keys in the cluster.
-
#get(key, role: :master, **options) ⇒ Object
Get the value of a single key from the cluster.
-
#mget(*keys, role: :master, **options) ⇒ Object
Get the values of multiple keys from the cluster.
-
#set(key, value, role: :master, **options) ⇒ Object
Set the value of a single key in the cluster.
Instance Method Details
#del(*keys, role: :master, **options) ⇒ Object
Delete one or more keys from the cluster. Uses the appropriate client(s) for each key’s slot.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/protocol/redis/cluster/methods/generic.rb', line 19 def del(*keys, role: :master, **) return 0 if keys.empty? count = 0 clients_for(*keys, role: role) do |client, grouped_keys| count += client.call("DEL", *grouped_keys) end return count end |
#exists(*keys, role: :master, **options) ⇒ Object
Check existence of one or more keys in the cluster.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/protocol/redis/cluster/methods/generic.rb', line 36 def exists(*keys, role: :master, **) return 0 if keys.empty? count = 0 clients_for(*keys, role: role) do |client, grouped_keys| count += client.call("EXISTS", *grouped_keys) end return count end |
#get(key, role: :master, **options) ⇒ Object
Get the value of a single key from the cluster.
74 75 76 77 78 79 |
# File 'lib/protocol/redis/cluster/methods/generic.rb', line 74 def get(key, role: :master, **) slot = slot_for(key) client = client_for(slot, role) return client.call("GET", key) end |
#mget(*keys, role: :master, **options) ⇒ Object
Get the values of multiple keys from the cluster.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/protocol/redis/cluster/methods/generic.rb', line 53 def mget(*keys, role: :master, **) return [] if keys.empty? results = Array.new(keys.size) key_to_index = keys.each_with_index.to_h clients_for(*keys, role: role) do |client, grouped_keys| values = client.call("MGET", *grouped_keys) grouped_keys.each_with_index do |key, i| results[key_to_index[key]] = values[i] end end return results end |
#set(key, value, role: :master, **options) ⇒ Object
Set the value of a single key in the cluster.
87 88 89 90 91 92 |
# File 'lib/protocol/redis/cluster/methods/generic.rb', line 87 def set(key, value, role: :master, **) slot = slot_for(key) client = client_for(slot, role) return client.call("SET", key, value) end |