@@ -1655,6 +1655,35 @@ func (c *ClusterClient) slotMasterNode(ctx context.Context, slot int) (*clusterN
16551655 return state .slotMasterNode (slot )
16561656}
16571657
1658+ // ReplicaForKey gets a client for a replica node to run any command on it.
1659+ // This is especially useful if we want to run a particular lua script which has
1660+ // only read only commands on the replica.
1661+ // This is because other redis commands generally have a flag that points that
1662+ // they are read only and automatically run on the replica nodes
1663+ // if ClusterOptions.ReadOnly flag is set to true.
1664+ func (c * ClusterClient ) ReplicaForKey (ctx context.Context , key string ) (* Client , error ) {
1665+ state , err := c .state .Get (ctx )
1666+ if err != nil {
1667+ return nil , err
1668+ }
1669+ slot := hashtag .Slot (key )
1670+ node , err := c .slotReadOnlyNode (state , slot )
1671+ if err != nil {
1672+ return nil , err
1673+ }
1674+ return node .Client , err
1675+ }
1676+
1677+ // MasterForKey return a client to the master node for a particular key.
1678+ func (c * ClusterClient ) MasterForKey (ctx context.Context , key string ) (* Client , error ) {
1679+ slot := hashtag .Slot (key )
1680+ node , err := c .slotMasterNode (ctx , slot )
1681+ if err != nil {
1682+ return nil , err
1683+ }
1684+ return node .Client , err
1685+ }
1686+
16581687func appendUniqueNode (nodes []* clusterNode , node * clusterNode ) []* clusterNode {
16591688 for _ , n := range nodes {
16601689 if n == node {
0 commit comments