/
cloud-castle
/
memcached
Обзор
Документация
Войти
/
cloud-castle
/
memcached
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
9
CI/CD
Аналитика
main
benchmarks/competitors.php
151 строка
5 KB
Алексей Зорин
refactor: магические числа вынесены в приватные константы
27 июл 2026, 23:46
27 июл 2026, 23:46
743ec9c
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * Ленивые фабрики участников сравнения. * * Каждая фабрика создаёт клиента только при вызове и возвращает операцию * «записать значение и прочитать его назад» — одну и ту же для всех * участников. Ленивость нужна для честного замера памяти: в процесс * попадает только целевая библиотека, а не все семь сразу. * * @internal инструмент разработки */ use CloudCastle\Memcached\Client; use CloudCastle\Memcached\Configuration\Config; use CloudCastle\Memcached\Configuration\Protocol; use CloudCastle\Memcached\Configuration\Server; use CloudCastle\Memcached\Configuration\Topology; $host = getenv('MEMCACHED_HOST') ?: '127.0.0.1'; $port = (int) (getenv('MEMCACHED_PORT') ?: '11211'); /** * Создаёт нативный клиент расширения. */ $nativeClient = static function () use ($host, $port): \Memcached { $memcached = new \Memcached(); $memcached->addServer($host, $port); return $memcached; }; return [ 'cloud-castle/memcached' => static function () use ($host, $port): callable { $client = new Client(new Config([new Server($host, $port)], 'bench:cc:', new Topology(Protocol::Meta))); $index = 0; return static function () use ($client, &$index): mixed { $key = 'k' . (++$index % 1000); $client->set($key, 'value'); return $client->get($key); }; }, 'symfony/cache' => static function () use ($nativeClient): callable { $cache = new \Symfony\Component\Cache\Psr16Cache( new \Symfony\Component\Cache\Adapter\MemcachedAdapter($nativeClient(), 'benchsf'), ); $index = 0; return static function () use ($cache, &$index): mixed { $key = 'k' . (++$index % 1000); $cache->set($key, 'value'); return $cache->get($key); }; }, 'illuminate/cache' => static function () use ($nativeClient): callable { $store = new \Illuminate\Cache\MemcachedStore($nativeClient(), 'benchil:'); $cache = new \Illuminate\Cache\Repository($store); $index = 0; return static function () use ($cache, &$index): mixed { $key = 'k' . (++$index % 1000); $cache->set($key, 'value'); return $cache->get($key); }; }, 'matthiasmullie/scrapbook' => static function () use ($nativeClient): callable { $cache = new \MatthiasMullie\Scrapbook\Psr16\SimpleCache( new \MatthiasMullie\Scrapbook\Adapters\Memcached($nativeClient()), ); $index = 0; return static function () use ($cache, &$index): mixed { $key = 'benchsb_k' . (++$index % 1000); $cache->set($key, 'value'); return $cache->get($key); }; }, 'phpfastcache/phpfastcache' => static function () use ($host, $port): callable { $config = new \Phpfastcache\Drivers\Memcached\Config([ 'servers' => [['host' => $host, 'port' => $port]], ]); $cache = new \Phpfastcache\Helper\Psr16Adapter('Memcached', $config); $index = 0; return static function () use ($cache, &$index): mixed { $key = 'benchpfc_k' . (++$index % 1000); $cache->set($key, 'value'); return $cache->get($key); }; }, 'tedivm/stash' => static function () use ($nativeClient): callable { $pool = new \Stash\Pool(new \Stash\Driver\Memcache(['servers' => [[ getenv('MEMCACHED_HOST') ?: '127.0.0.1', (int) (getenv('MEMCACHED_PORT') ?: '11211'), ]]])); $index = 0; return static function () use ($pool, &$index): mixed { $key = 'benchstash_k' . (++$index % 1000); $item = $pool->getItem($key); $item->set('value'); $pool->save($item); return $pool->getItem($key)->get(); }; }, 'laminas/laminas-cache' => static function () use ($host, $port): callable { $storage = new \Laminas\Cache\Storage\Adapter\Memcached( new \Laminas\Cache\Storage\Adapter\MemcachedOptions([ 'servers' => [[$host, $port]], 'namespace' => 'benchlm', ]), ); $cache = new \Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator($storage); $index = 0; return static function () use ($cache, &$index): mixed { $key = 'k' . (++$index % 1000); $cache->set($key, 'value'); return $cache->get($key); }; }, 'ext-memcached' => static function () use ($nativeClient): callable { $memcached = $nativeClient(); $index = 0; return static function () use ($memcached, &$index): mixed { $key = 'benchnative_k' . (++$index % 1000); $memcached->set($key, 'value'); return $memcached->get($key); }; }, ];