Redis类

Redis 类。

<?php

class RedisClient
{
    private static $config = array(
        'master' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'pass' => false,
        ),
        'slave' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'pass' => false,
        ),
    );

    private static $methods_run_at_master = array(
        'append', 'decr', 'decrby', 'getset', 'incr', 'incrby', 'incrbyfloat',
        'mset', 'msetnx', 'set', 'setbit', 'setex', 'psetex', 'setnx', 'setrange',
        'del', 'delete', 'expire', 'settimeout', 'pexpire', 'expireat', 'pexpireat',
        'migrate', 'move', 'persist', 'rename', 'renamekey', 'renamenx', 'restore',
        'hdel', 'hincrby', 'hincrby', 'hset', 'hsetnx', 'hmset',
        'blpop', 'brpop', 'brpoplpush', 'linsert', 'lpop', 'lpush', 'lpushx', 'lrem',
        'lremove', 'lset', 'ltrim', 'listtrim', 'rpop', 'rpoplpush', 'rpush', 'rpushx',
        'sadd', 'sdiffstore', 'sinterstore', 'smove', 'spop', 'srem', 'sremove', 'sunionstore',
        'zadd', 'zincrby', 'zinter', 'zrem', 'zdelete', 'zremrangebyrank',
        'zdeleterangebyrank', 'zremrangebyscore', 'zdeleterangebyscore', 'zunion',
    );

    public function setConfig($type, $host, $port = 6379, $pass = false)
    {
        self::$config[$type] = array(
            'host' => $host,
            'port' => $port,
            'pass' => $pass,
        );

        return $this;
    }

    public function setMasterConfig($host, $port = 6379, $pass = false)
    {
        $this->setConfig('master', $host, $port, $pass);

        return $this;
    }

    public function setSlaveConfig($host, $port = 6379, $pass = false)
    {
        $this->setConfig('slave', $host, $port, $pass);

        return $this;
    }

    public function select($type)
    {
        static $instances = array();

        if (isset($instances[$type])) {
            return $instances[$type];
        }

        $config = self::$config[$type];

        $redis = new Redis;
        $redis->connect($config['host'], $config['port']);

        if ($config['pass']) {
            $redis->auth($config['pass']);
        }

        $instances[$type] = $redis;

        return $redis;
    }

    public function __call($method, $arguments)
    {
        if (DEBUG_MODE || PHP_SAPI == 'cli' || in_array(strtolower($method), self::$methods_run_at_master)) {
            $type = 'master';
        } else {
            $type = 'slave';
        }

        $redis = $this->select($type);

        return call_user_func_array(array($redis, $method), $arguments);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值