Memcached example


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: {} }
foreach (self::$optionConstants as $constantKey => $value) {
            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;
    }

    
'host'   => '127.0.0.1',
            '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;
        }

    }

    /** * Re-initialize existing session, or creates a new one. * * @param string $path The path where to store/retrieve the session * @param string $name The session name */
    public function open($path$name): bool
    {
        $this->memcached = new Memcached();
        $this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); // required for touch() usage
        $serverList = [];

        foreach ($this->memcached->getServerList() as $server) {
            $serverList[] = $server['host'] . ':' . $server['port'];
        }

        if (
            ! preg_match_all(
                '#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#',
                

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
    {
        
$this->assertInstanceOf($expectedStoreClass$store);
    }

    public static function validConnections(): \Generator
    {
        if (class_exists(\Redis::class)) {
            yield [new \Redis(), RedisStore::class];
        }
        yield [new \Predis\Client(), RedisStore::class];
        if (class_exists(\Memcached::class)) {
            yield [new \Memcached(), MemcachedStore::class];
        }
        if (\extension_loaded('sysvsem')) {
            yield ['semaphore', SemaphoreStore::class];
        }
        if (class_exists(AbstractAdapter::class) && MemcachedAdapter::isSupported()) {
            yield ['memcached://server.com', MemcachedStore::class];
            yield ['memcached:?host[localhost]&host[localhost:12345]', MemcachedStore::class];
        }
        if (class_exists(\Redis::class) && class_exists(AbstractAdapter::class)) {
            yield ['redis://localhost', RedisStore::class];
            yield ['redis://localhost?lazy=1', RedisStore::class];
            

    }

    /** * {@inheritDoc} */
    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']
                );

                

    public static function createConnection(#[\SensitiveParameter] array|string $servers, array $options = []): \Memcached     {
        if (\is_string($servers)) {
            $servers = [$servers];
        }
        if (!static::isSupported()) {
            throw new CacheException('Memcached > 3.1.5 is required.');
        }
        set_error_handler(static fn ($type$msg$file$line) => throw new \ErrorException($msg, 0, $type$file$line));
        try {
            $client = new \Memcached($options['persistent_id'] ?? null);
            $username = $options['username'] ?? null;
            $password = $options['password'] ?? null;

            // parse any DSN in $servers             foreach ($servers as $i => $dsn) {
                if (\is_array($dsn)) {
                    continue;
                }
                if (!str_starts_with($dsn, 'memcached:')) {
                    throw new InvalidArgumentException('Invalid Memcached DSN: it does not start with "memcached:".');
                }
                
foreach (self::$optionConstants as $constantKey => $value) {
            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;
    }

    
Home | Imprint | This part of the site doesn't use cookies.