http://www.toplee.com/blog/100.html 参考网站
----------------------安装phpredis
https://github.com/nicolasff/phpredis/downloads
cd /tmp
tar zxvf nicolasff-phpredis-2.2.1-66-g89bdaf2.tar.gz
cd nicolasff-phpredis-89bdaf2/
/usr/local/php/bin/phpize
/configure --with-php-config=/usr/local/php/bin/php-config
make
make install
ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
cat /usr/local/php/etc/php.ini | grep redis
kill -USR2 `cat /dev/shm/pid/php-fpm.pid`
/usr/local/php/sbin/php-fpm -t
----------------安装配置
获得eaccelerator源代码eaccelerator-0.9.6-rc1.tar.bz2:
http://bart.eaccelerator.net/source/
需要用到phpize,所以:
#yum -y install php-devel
#whereis phpize
phpize: /usr/bin/phpize /usr/share/man/man1/phpize.1.gz
#tar jxvf eaccelerator-0.9.6-rc1.tar.bz2
#cd eaccelerator-0.9.6-rc1
#/usr/bin/phpize
#./configure --enable-eaccelerator=shared --with-php-config=/usr/bin/php-config
#make
#make install
Installing shared extensions: /usr/lib/php/modules/
注意上面的“Installing shared extensions”的地址,这是phpize告诉我们的扩展库的地址。
#ls -l /usr/lib/php/modules/ 就可看到eaccelerator.so
#cd /etc/php.d
#vim eaccelerator.ini
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator" 需要手动建立目录和权限
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="1"
eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
重启apache
查看phpinfo
----------------------------测试
如果你打开了eAccelerator的debug选项,可以从日志中看到类似下面的信息:
[root@localhost httpd]# tail -f eaccelerator_log
EACCELERATOR hit: "/var/www/html/test/index.php"
EACCELERATOR hit: "/var/www/html/test/index.php"
EACCELERATOR hit: "/var/www/html/phpinfo.php"
以上信息表示文件都得到了缓存和命中。
-----------------------------eAccelerator提供如下的API接口和文件 :(下述文件均在源码包的doc/php/目录下)
cd /tmp/eaccelerator-0.9.6-rc1/doc/php
[root@localhost php]# ls
cache.php dasm.php examples info.php shared_memory.php
有关上述文档详细说明请参考官方文档:API Documents:http://bart.eaccelerator.net/doc/phpdoc/
下面有部分网友翻译后的接口说明:
eaccelerator_put($key, $value, $ttl=0)
将 $value 以 $key 为键名存进缓存(php4下支持对像类型,看源码好像zend2里不支持了),$ttl 是这个缓存的生命周期,单位是秒,省略该参数或指定为 0 表示不限时,直到服务器重启清空为止。
eaccelerator_get($key)
根据 $key 从缓存中返回相应的 eaccelerator_put() 存进去的数据,如果这项缓存已经过期或不存在那么返回值是 NULL
eaccelerator_rm($key)
根据 $key 移除缓存
eaccelerator_gc()
移除清理所有已过期的 key
eaccelerator_lock($key)
为 $key 加上锁定操作,以保证多进程多线程操作时数据的同步。需要调用 eaccelerator_unlock($key) 来释放这个锁或等待程序请求结束时自动释放这个锁。
例如:
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?>
eaccelerator_unlock($key)
根据 $key 释放锁
eaccelerator_cache_output($key, $eval_code, $ttl=0)
将 $eval_code 代码的输出缓存 $ttl 秒,($ttl参数同 eacclerator_put)
例如:
eaccelerator_cache_result($key, $eval_code, $ttl=0)
将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
例如:
eaccelerator_cache_page($key, $ttl=0)
将当前整页缓存 $ttl 秒。
例如:
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?>
eaccelerator_rm_page($key)
删除由 eaccelerator_cache_page() 执行的缓存,参数也是 $key
-----------------------------PHP代码中使用eAccelerator加速
下面有一个测试的代码,你可以测试一下eAccelerator强大的威力:(该代码在 cli 模式下可能无效)
class test_cache {
var $pro = 'hello';
function test_cache() {
echo "Object Created!
\n";
}
function func() {
echo ', the world!';
}
function now($t) {
echo date('Y-m-d H:i:s', $t);
}
}
$tt = eaccelerator_get("test_tt");
if (!$tt)
{
$tt = new test_cache;
eaccelerator_put("test_tt", $tt);
echo "no cached!
\n";
}
else {
echo "cached
\n";
}
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>
另外,据说在著名的vBulletin 3.60Beta版里面已经集成了对eAccelerator的支持,下面是一段来自vBulletin里面的代码
// #############################################################################
// eAccelerator
/**
* Class for fetching and initializing the vBulletin datastore from eAccelerator
*
* @package vBulletin
* @version $Revision: 0.1 $
* @date $Date: 2005/06/12 13:14:18 $
*/
class vB_Datastore_eAccelerator extends vB_Datastore
{
/**
* Fetches the contents of the datastore from eAccelerator
*
* @param array Array of items to fetch from the datastore
*
* @return void
*/
function fetch($itemarray)
{
if (!function_exists('eaccelerator_get'))
{
trigger_error("eAccelerator not installed", E_USER_ERROR);
}
foreach ($this->defaultitems AS $item)
{
$this->do_fetch($item);
}
if (is_array($itemarray))
{
foreach ($itemarray AS $item)
{
$this->do_fetch($item);
}
}
$this->check_options();
// set the version number variable
$this->registry->versionnumber =& $this->registry->options['templateversion'];
}
/**
* Fetches the data from shared memory and detects errors
*
* @param string title of the datastore item
*
* @return void
*/
function do_fetch($title)
{
$data = eaccelerator_get($title);
if ($data === null)
{ // appears its not there, lets grab the data, lock the shared memory and put it in
$data = '';
$dataitem = $this->dbobject->query_first("
SELECT title, data FROM " . TABLE_PREFIX . "datastore
WHERE title = '" . $this->dbobject->escape_string($title) ."'
");
if (!empty($dataitem['title']))
{
$data =& $dataitem['data'];
$this->build($dataitem['title'], $dataitem['data']);
}
}
$this->register($title, $data);
}
/**
* Updates the appropriate cache file
*
* @param string title of the datastore item
*
* @return void
*/
function build($title, $data)
{
if (!function_exists('eaccelerator_put'))
{
trigger_error("eAccelerator not installed", E_USER_ERROR);
}
eaccelerator_lock($title);
eaccelerator_put($title, $data);
eaccelerator_unlock($title);
}
}