<?php

namespace Heanup\Frame\Package;

/**
 * 队列
 * 使用redis实现
 */
class Queue extends \Heanup\Frame\Package
{
    const CONFIG_CLASS = 'Redis\\Queue';
    const PREFIX = '';

    /**
     * 获取队列长度
     * @return int
     */
    public static function length()
    {
        $key = self::getKey();
        $result = self::getConnection(true)->lLen($key);
        return $result;
    }

    /**
     * 插入数据
     * @param $data
     * @return int
     */
    public static function push($data)
    {
        $key = self::getKey();
        $result = self::getConnection(true)->rPush($key, $data);
        return $result;
    }

    /**
     * 取出数据
     * @return string
     */
    public static function pop()
    {
        $key = self::getKey();
        $result = self::getConnection(true)->lPop($key);
        return $result;
    }

    /**
     * 获取连接池
     * @param bool $isMaster
     * @return \Redis
     */
    protected static function getConnection($isMaster = false)
    {
        $class = get_called_class();
        $configClass = $class::CONFIG_CLASS;
        $redis = new \Heanup\Frame\Library\Redis($configClass);
        $connection = $redis->getConnection($isMaster);
        return $connection;
    }

}