symfony1.4からmemcachedを使う方法 - 基本編
symfonyでmemcachedを利用する方法を簡単にまとめました。
nginx + php5でmemcachedを利用するという方法です。
http://labs.unoh.net/2010/05/symfonydoctrine.html
に詳しくあるのですが更に詳細はこちらを参照ください。
nginx, php-fastcgiでphp5,symfony1.4を動かせていることを前提とします。
http://kmusiclife.hatenablog.com/entry/20111105/1320467309
http://kmusiclife.hatenablog.com/entry/20120509/1336538654
などを参考にしてください。
必要なモジュールなどをインストールします。
# apt-get install memcached # apt-get install php5-memcache # apt-get install php5-memcached # /etc/init.d/php-fastcgi restart # /etc/init.d/nginx restart
config/ProjectConfiguration.class.php
public function configureDoctrine(Doctrine_Manager $manager)
{
$servers = array(
array(
'host' => '127.0.0.1',
'port' => 11211,
'persistent' => true),
);
$cacheDriver = new Doctrine_Cache_Memcache(array(
'servers' => $servers,
'compression' => false)
);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE_LIFESPAN, 60); // 結果キャッシュのデフォルトの存続秒数 sec
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE_LIFESPAN, 60); // クエリーキャッシュのデフォルトの存続秒数 sec
}Doctrineからの利用
$this->table_names = Doctrine_Core::getTable('table_name')
->createQuery('a')
->where('a.id =? ', $id)
->useResultCache(true)
->execute();
