Concurrent.php 1.74 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 14/12/15
 * Time: 上午2:02
 */

namespace Hood\Concurrent\Yar;

use Hood\Core\Root;

class Concurrent extends Root
{
    private $uri;

    public function __construct($uri)
    {
        parent::__construct();
        $this->uri = $uri;
    }

    /**
     * 注册一个并行的服务调用
     * @param $uri
     * @param $method
     * @param $parameters
     * @param string $callback
     */
    public function call($uri, $method, $parameters, $callback = '')
    {
        \Yar_Concurrent_Client::call($uri, $method, $parameters, $callback);
    }

    /**
     * 链式调用
     * @param $method
     * @param $parameters
     * @param string $callback
     * @return $this
     */
    public function calls($method, $parameters, $callback = '')
    {
        $this->call($this->uri, $method, $parameters, $callback);
        return $this;
    }

    /**
     * 发送所有注册的并行调用
     * @param string $callback
     * @param callable $errorCallback
     */
    public function loop($callback = '', $errorCallback = '')
    {
        \Yar_Concurrent_Client::loop($callback, $errorCallback);
    }

    /**
     * 缓存tag
     * @param $tagName
     * @return $this
     */
    public function tag($tagName)
    {
        $this->_cacheTagName = $tagName;
        return $this;
    }

    /**
     * 缓存key
     * @param string $key
     * @param null $prefix
     * @return $this
     */
    public function key($key)
    {
        $this->_cacheKey = (string)$key;
        return $this;
    }

    /**
     * 缓存时间
     * @param $expire
     * @return $this
     */
    public function expire($expire)
    {
        $this->_cacheExpire = (int)$expire;
        return $this;
    }
}