php To operate the redis cluster cluster, you need to use the phpredis extension. This is a c extension with higher performance.
phpredis cluster usage (recommended learning: PHP video tutorial)
<?php $redis_list = ['10.30.5.162:7000','10.30.5.163:7000','10.30.5.163:7001']; $client = new RedisCluster(NUll,$redis_list); echo $client->get('new_item_key:d89b561fb759fd533a8c2781ef15dd5f')
Code description
The first parameter is NULL. Don’t ask me, I don’t know why. Anyway, I couldn’t find the document, and I didn’t understand this article.
The second parameter is the list of master servers of the redis cluster we need to connect to. We have 3 masters, so just fill in 3, you can fill in one master node, or even fill in one slave node, but the performance will be different.
Cluster Principle
Why can redisCluster be operated by filling in any node address?
In cluster mode, Redis receives any key-related commands First calculate the slot corresponding to the key.
If the slave node is initialized, the redis command will first be sent to the slave node.
The slave node finds the corresponding node according to the slot. If the node is If it is itself, it will process the key command;
If it is not itself, it will cause a MOVED redirection error and notify the client to request the correct node. This process is called MOVED redirection
The redirection information contains the slot corresponding to the key and the node address responsible for the slot. Based on this information, the client can redirect to the correct When a node initiates a request, the phpredis client can directly initiate a request to the node where the key is located based on the redirection information to obtain the data
The above is the detailed content of Does phpredis support clustering?. For more information, please follow other related articles on the PHP Chinese website!