PHP redis 哈希槽分片算法

分片算法代码

<?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 框架配置获取的格式
     *<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值