分片算法代码
<?php
namespace App\Repositories\Redis;
use App\Repositories\BaseRepository;
/**
* 实现分片对PHPredis的重新封装类
* 如果使用分片方式,获取实例传入的配置去获取 节点的配置信息生成 Sharding 信息, 节点中必须包含一个名为 default 的节点配置
* 总节点数最好是1024 整除的数
* @author yvan.xian <yvan.xian@idreamsky.com>
* @date 2017-10-26
*/
class RedisSharding extends BaseRepository{
/**
* 当前对象实例
* @var type
*/
private static $instance = null;
/**
* 设置当前实例的redis操作是否使用长连接 1 是 0 否
* @var type
*/
private $isPersistent = 1;
/**
* 实例分片哈希槽
* @var type
*/
private $shardingHashSlot = [];
/**
* 分片 哈希槽数
* @var type
*/
private $slotCount = 1024;
/**
*底层 redis 对象
* @var type
*/
private $redis = null;
/**
* 当前操作使用的redis配置名称
* @var type
*/
private $thisNodeName = '';
/**
* 分片使用的实例数组
* @var type
*/
private $redisConfigs = [];
/**
* 使用的 redis 配置项目(集群的根配置路径)
* @var type
*/
private $useConfig = '';
private function __construct() {
//加载 redis 的配置文件 2018-01-22 by yvan
App()->configure('redis');
//默认使用 redis.data_cache
$this->useConfig = 'redis.data_cache';
$this->redisConfigs = config($this->useConfig, []);
}
/**
* 获取当前实例,调用方法和 \Redis 一致
* @param int $isPersistent 是否使用持久化连接,默认使用 (cli 模式下不使用)
* @param string $useConfig 使用的配置根路径 database.redis 使用 laravel 框架配置获取的格式
*<