设为首页 收藏本站
查看: 1036|回复: 0

[经验分享] php so模块扩展:phpize安装eaccelerator加速器和使用

[复制链接]

尚未签到

发表于 2017-4-10 13:11:58 | 显示全部楼层 |阅读模式
  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选项,可以从日志中看到类似下面的信息:
  
[iyunv@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
[iyunv@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) 来释放这个锁或等待程序请求结束时自动释放这个锁。
  例如:
  <?php
    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)
  例如:
  <?php eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30); ?>
 
eaccelerator_cache_result($key, $eval_code, $ttl=0)
  将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
  例如:
  <?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?>
 
eaccelerator_cache_page($key, $ttl=0)
  将当前整页缓存 $ttl 秒。
  例如:
  <?php
    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 模式下可能无效)
<?php
class test_cache {
  var $pro = 'hello';
 
  function test_cache() {
    echo "Object Created!<br>\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!<br>\n";
}
else {
  echo "cached<br>\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);
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362988-1-1.html 上篇帖子: php里获取第一个中文首字母并排序 下篇帖子: 源码下载站点及网络上大部分PHP站点集合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表