Symfony 4, APCu y Memcached

Cómo integrar APCu y Memcached con Symfony

Image for post
Image for post

¿Cómo hemos llegado a ellos?

Image for post
Image for post

¿Qué necesito para ApcuAdaptar?

sudo apt-get install php7.0-apcu -y

¿Y cómo se usa?

use Symfony\Component\Cache\Adapter\ApcuAdapter;
class MenuService {public function __construct(NodeRepository $nodeRepository) {  $this->nodeRepository = $nodeRepository;  $this->cache = new ApcuAdapter();}
public function getMenu($locale) {  $menuCached = $this->cache->getItem(‘app.menu’);  if ($menuCached->isHit()) {    $menu = $menuCached->get();  } else {    $menu = $this->nodeRepository->buildMenu();    $menuCached->set($menu);    $this->cache->save($menuCached);  }  return $menu;}

Y MemcachedAdapter

<?phpnamespace App\Service;use Symfony\Component\Cache\Adapter\MemcachedAdapter;class MemcacheService {  private $memcacheClient;   private $cache;  public function __construct($namespace) {    $satName = $satConfig['name'];    $this->memcachedClient = MemcachedAdapter::createConnection(      'memcached://localhost:11211'    );    $this->cache = new MemcachedAdapter(      $this->memcachedClient    );  }  public function get($key) {    $cacheItem = $this->cache->getItem($key);    return ($cacheItem->isHit()) ? $cacheItem->get() : false;  }  public function set($key, $value) {    $cacheItem = $this->cache->getItem($key);    $cacheItem->set($value);    $this->cache->save($cacheItem);  }  public function delete($key) {    return $this->cache->deleteItem($key);  }
}

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store