php-4.php实现redis aof bgrewriteaof重写

bgrewriteaof

Aof.php

<?php
require_once 'Redis.php';
require_once 'Input.php';

class Aof
{
    /**
     * [protected description]
     * @var Redis
     */
    protected $redis;

    protected $commands;

    protected $filePath ;

    public function __construct($host = "192.160.1.140", $port = 6379, $file = "/aof_file.aof")
    {
        $this->redis = new RedisModel();
        $this->redis->pconnect($host, $port);
        $this->filePath = __DIR__.$file;
    }
    /**
     * aof重写方法
     * 六星教育 @shineyork老师
     * @return [type] [description]
     */
    public function aofRewrite()
    {
        // 1. 获取所有的 数据库
        $dbs = $this->redis->config('GET', 'databases')['databases'];
        // Input::info($dbs);
        // 2. 根据库去循环获取key scan
        //    key可能很多
        for ($i=0; $i < $dbs; $i++) {
            // 3. 再根据key 调用重写的规则的方法 去重写
            //    判断重写的数据类型

            // 用于记录命令的
            $commands = null;
            // 切换数据库
            $this->redis->select($i);
            // 针对于 $i 这个数据库去进行命令的重写
            $commands = $this->rewrite($commands);

            if (!empty($commands)) {
              Input::info($commands);
              $this->rewriteFile( "db:".$i.";key:".$commands);
            }
        }
    }

    protected function rewrite($commands, $iterator = -1)
    {
        $keys = $this->redis->scan($iterator);
        // Input::info($keys);
        // Input::info($iterator);

        // 是否有数据
        if (empty($keys)) {
            return ;
        }
        // 重写获取的key的数据
        foreach ($keys as $key) {
            // 得到key的类型
            $keyType = $this->redis->getType($key);
            // 再根据类型去重写命令,并且拼接
            $commands .= $this->{"rewrite".$keyType}($key);
        }

        // 判断后面是否还有数据
        if ($iterator > 0) {
            $this->rewrite($commands, $iterator);
        } else {
            return $commands;
        }
    }

    protected function rewriteFile($commands)
    {
        file_put_contents($this->filePath, $commands, 8);
    }

    protected function rewriteSet($key)
    {
        $value = $this->redis->sMembers($key);
        return $this->rewriteCommand('SADD', $key, implode(" ", $value));
    }
    // protected function rewriteList($key)
    // {
    //     return $this->rewriteCommand('', $key, $value);
    // }
    // protected function rewriteZset($key)
    // {
    //     return $this->rewriteCommand('', $key, $value);
    // }
    // protected function rewriteString($key)
    // {
    //     return $this->rewriteCommand('', $key, $value);
    // }
    // protected function rewriteHash($key)
    // {
    //     return $this->rewriteCommand('', $key, $value);
    // }
    // protected function rewriteExpireTime($key)
    // {
    //     return $this->rewriteCommand('SADD', $key, $value);
    // }
    protected function rewriteCommand($method, $key, $value)
    {
        return $method." ".$key." ".$value.";";
    }
}
(new Aof)->aofRewrite();

Redis.php

<?php


class RedisModel extends \Redis
{
    /**
     * 根据传递的key返回一下数据类型
     * 六星教育 @shineyork老师
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public function getType($key)
    {
        $keyType = $this->type($key);
        switch ($keyType) {
          case Redis::REDIS_SET :
            return "Set";
            break;
          case Redis::REDIS_LIST :
            return "List";
            break;
          case Redis::REDIS_ZSET :
            return "Zset";
            break;
          case Redis::REDIS_STRING :
            return "String";
            break;
          case Redis::REDIS_HASH :
            return "Hash";
            break;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值