addServer example

if (self::$defaultOptions[$constantKey] !== $option = $c->getOption($value)) {
                $nonDefaultOptions[$constantKey] = $option;
            }
        }

        return $nonDefaultOptions;
    }

    private static function discoverDefaultOptions(): array
    {
        $defaultMemcached = new \Memcached();
        $defaultMemcached->addServer('127.0.0.1', 11211);

        $defaultOptions = [];
        self::$optionConstants ??= self::getOptionConstants();

        foreach (self::$optionConstants as $constantKey => $value) {
            $defaultOptions[$constantKey] = $defaultMemcached->getOption($value);
        }

        return $defaultOptions;
    }

    
class MemcachedCasterTest extends TestCase
{
    use VarDumperTestTrait;

    public function testCastMemcachedWithDefaultOptions()
    {
        if (!class_exists(\Memcached::class)) {
            $this->markTestSkipped('Memcached not available');
        }

        $var = new \Memcached();
        $var->addServer('127.0.0.1', 11211);
        $var->addServer('127.0.0.2', 11212);

        $expected = <<<EOTXT Memcached { servers: array:2 [ 0 => array:3 [ "host" => "127.0.0.1" "port" => 11211 "type" => "TCP" ] 1 => array:3 [ "host" => "127.0.0.2" "port" => 11212 "type" => "TCP" ] ] options: {} }
if (self::$defaultOptions[$constantKey] !== $option = $c->getOption($value)) {
                $nonDefaultOptions[$constantKey] = $option;
            }
        }

        return $nonDefaultOptions;
    }

    private static function discoverDefaultOptions(): array
    {
        $defaultMemcached = new \Memcached();
        $defaultMemcached->addServer('127.0.0.1', 11211);

        $defaultOptions = [];
        self::$optionConstants ??= self::getOptionConstants();

        foreach (self::$optionConstants as $constantKey => $value) {
            $defaultOptions[$constantKey] = $defaultMemcached->getOption($value);
        }

        return $defaultOptions;
    }

    
'port'   => 11211,
            'extras' => array(
                'timeout' => 3600, // one hour                 'prefix'  => 'simplepie_',
            ),
        );
        $this->options = SimplePie_Misc::array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));

        $this->name = $this->options['extras']['prefix'] . md5("$name:$type");

        $this->cache = new Memcached();
        $this->cache->addServer($this->options['host'](int)$this->options['port']);
    }

    /** * Save data to the cache * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */
    public function save($data) {
        if ($data instanceof SimplePie) {
            $data = $data->data;
        }

        
'port' => 11211,
            'extras' => array(
                'timeout' => 3600, // one hour                 'prefix' => 'simplepie_',
            ),
        );
        $this->options = SimplePie_Misc::array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));

        $this->name = $this->options['extras']['prefix'] . md5("$name:$type");

        $this->cache = new Memcache();
        $this->cache->addServer($this->options['host'](int) $this->options['port']);
    }

    /** * Save data to the cache * * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */
    public function save($data)
    {
        if ($data instanceof SimplePie)
        {
foreach ($matches as $match) {
            // If Memcached already has this server (or if the port is invalid), skip it             if (in_array($match[1] . ':' . $match[2]$serverList, true)) {
                $this->logger->debug(
                    'Session: Memcached server pool already has ' . $match[1] . ':' . $match[2]
                );

                continue;
            }

            if ($this->memcached->addServer($match[1](int) $match[2]$match[3] ?? 0)) {
                $this->logger->error(
                    'Could not add ' . $match[1] . ':' . $match[2] . ' to Memcached server pool.'
                );
            } else {
                $serverList[] = $match[1] . ':' . $match[2];
            }
        }

        if (empty($serverList)) {
            $this->logger->error('Session: Memcached server pool is empty.');

            
if (!array_key_exists('retry_interval', $server)) {
                $server['retry_interval'] = self::DEFAULT_RETRY_INTERVAL;
            }
            if (!array_key_exists('status', $server)) {
                $server['status'] = self::DEFAULT_STATUS;
            }
            if (!array_key_exists('failure_callback', $server)) {
                $server['failure_callback'] = self::DEFAULT_FAILURE_CALLBACK;
            }
            if ($this->_options['compatibility']) {
                // No status for compatibility mode (#ZF-5887)                 $this->_memcache->addServer($server['host']$server['port']$server['persistent'],
                                        $server['weight']$server['timeout'],
                                        $server['retry_interval']);
            } else {
                $this->_memcache->addServer($server['host']$server['port']$server['persistent'],
                                        $server['weight']$server['timeout'],
                                        $server['retry_interval'],
                                        $server['status']$server['failure_callback']);
            }
        }
    }

    
public function initialize()
    {
        try {
            if (class_exists(Memcached::class)) {
                // Create new instance of Memcached                 $this->memcached = new Memcached();
                if ($this->config['raw']) {
                    $this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
                }

                // Add server                 $this->memcached->addServer(
                    $this->config['host'],
                    $this->config['port'],
                    $this->config['weight']
                );

                // attempt to get status of servers                 $stats = $this->memcached->getStats();

                // $stats should be an associate array with a key in the format of host:port.                 // If it doesn't have the key, we know the server is not working as expected.                 if (isset($stats[$this->config['host'] . ':' . $this->config['port']])) {
                    
class MemcachedStoreTest extends AbstractStoreTestCase
{
    use ExpiringStoreTestTrait;

    public static function setUpBeforeClass(): void
    {
        if (version_compare(phpversion('memcached'), '3.1.6', '<')) {
            throw new SkippedTestSuiteError('Extension memcached > 3.1.5 required.');
        }

        $memcached = new \Memcached();
        $memcached->addServer(getenv('MEMCACHED_HOST'), 11211);
        $memcached->get('foo');
        $code = $memcached->getResultCode();

        if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
            throw new SkippedTestSuiteError('Unable to connect to the memcache host');
        }
    }

    protected function getClockDelay(): int
    {
        return 1000000;
    }
Home | Imprint | This part of the site doesn't use cookies.