search
HomeBackend DevelopmentPHP Tutorial在Win7 中为php扩展配置Xcache_PHP

XCache 工程由 mOo 领队, 他也是 Lighttpd 的开发成员之一. Lighttpd 是最快的 Web 服务器应用程序之一, 并且超越 Apache 以及许多其他 Web 服务器. XCache 努力达到类似的效果.

目前最新的版本为XCache 3.2.0,它是php5全系列支持的,官方网站: http://xcache.lighttpd.net/

如果英文不好的朋友,可以点右边切换语言为中文。

首先下载最新的版本: http://xcache.lighttpd.net/pub/Releases/3.2.0/ 记得选择正确的版本。

下载解压后放到php下的ext目录下,然后打开php.ini 添加extension = php_xcache.dll

压缩包内还有一个中文版Xcache的php.ini的示范,还有就是一个查看Xcache及信息的程序.

注意xcache.admin.pass 用md5加密后存放

xcache.count可以根据你CPU的数设置,默认为1

xcache.slots缓存的文件/变量hash参考值,根据自己的实际情况可以设置

完成后,重新启动Apache服务。

代码如下:


;; 本文件只是例子, 请在 php.ini 里设置以便生效
[xcache-common]
;; 非 windows 例子:
extension = xcache.so
;; Windows 系统例子:
; extension = php_xcache.dll
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "mOo"
; xcache.admin.pass = md5($您的密码)
; 登录使用 $your_password下面的密码请用md5加密后填写到里面
xcache.admin.pass = ""
[xcache]
; 这里的多数选项仅在 ini 里可以修改, 这里列出的都是默认值, 除非另外说明
; 选择底层内存共享实现方案
xcache.shm_scheme =        "mmap"
; 禁用: xcache.size=0
; 启用: xcache.size=64M 之类 (任意>0的值) 同时请注意您的系统 mmap 上限
xcache.size  =               60M
; 建议设置为 cpu 数 (cat /proc/cpuinfo |grep -c processor)
xcache.count =                 1
; 只是个 hash 参考值, 实际存储项目(php脚本/变量)可超过这个数字
xcache.slots =                8K
; 缓存项目的 ttl, 0=永久
xcache.ttl   =                 0
; 扫描过期项目的时间间隔, 0=不扫描, 其他值以秒为单位
xcache.gc_interval =           0
; 同上, 只是针对变量缓存设置
xcache.var_size  =            4M
xcache.var_count =             1
xcache.var_slots =            8K
; xcache_*() 函数 ttl 参数的默认值
xcache.var_ttl   =             0
; 限制 xcache_*() 函数 ttl 参数不超过此设置. 0=不限制
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
; /dev/zero 时无效
xcache.readonly_protection = Off
; 对于 *nix 系统, xcache.mmap_path 是文件路径而不是目录. (自动创建/覆盖)
; 如果您期望启用 ReadonlyProtection, 必须避免使用 "/dev/*", 可以使用类似 "/tmp/xcache"
; 不同 php 进程组不会共享同一个 /tmp/xcache
; 对于 Win32 系统, xcache.mmap_path=匿名MAP名字, 不是文件路径. 建议使用 XCache 字眼避免跟其他软件冲突
xcache.mmap_path =    "/dev/zero"
 
; 仅在 XCache 异常时有用. 设置为空(禁用) 或者类似 "/tmp/phpcore/" (能被 php 写入文件)
xcache.coredump_directory =   ""
; 仅用于 Windows. 除非 XCache 开发人员告诉你, 否则保持默认值
xcache.coredump_type =         0
; 异常时自动禁止缓存
xcache.disable_on_crash =    Off
; 启用实验性功能 (如果有)
xcache.experimental =        Off
; 以下是 Request 级可改设置. 可以 ini_set, .htaccess 等
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =           Off
[xcache.coverager]
; 本功能开启后降低运行性能
; 仅在 xcache.coverager == On && xcache.coveragedump_directory == "非空值" 时本功能才会启用
; per request settings. 可以 ini_set, .htaccess 等
; 启用代码流程覆盖面信息采集以及 xcache_coverager_start/stop/get/clean() 等函数
xcache.coverager =           Off
xcache.coverager_autostart =  On
; 仅在 php ini 文件内设置
; 请确保本目录能被 coverage viewer 脚本读取 (注意 open_basedir)
xcache.coveragedump_directory = ""

 然后查看phpinfo,看看Xcache是否已经生效。如下图

现在在web发布目录中新建一个目录如xcache,将官方的压缩包内的lib及htdocs目录放里面,

在浏览器输入http://127.0.0.1/xcache/htdocs/, 会弹出一个登陆的账号密码对话框,输入进去后,你就可以看到xcache的环境及配置,变量等等。。

但实际上Xcache不但能缓存变量,而且能缓存php文件,如果你的php环境中配置了Xcache扩展后,它会自动将每次给你访问的php文件都自动缓存。无需再额外的修改代码,十分的方便快捷,如下图的我只访问了phpmyadmin,Xcache官方的程序包就可以检测到phpmyadmin的cache列表。

代码很简单,带单例模式,可以直接在应用环境中使用,代码在php5.5.12中完美测试通过。

代码如下:


 $c =new Cache_Xcache();
  
 $c->set('key', 'aaaa123');
  
 echo $c->get('key');
  
 Cache_Xcache::getInstance()->set('key1', '999999999999999');
  
 echo Cache_Xcache::getInstance()->get('key1');
 /**------------------------------代码开始----------------------------------**/
  class Cache_Xcache {
    /**
     * 单例模式实例化本类
     *
     * @var object
     */
    protected static $_instance = null;
    /**
     * 默认的缓存策略
     *
     * @var array
     */
    protected $_defaultOptions = array('expire' => 900);
    /**
     * 构造方法
     *
     * @access public
     * @return boolean
     */
    public function __construct() {
        //分析xcache扩展模块         if (!extension_loaded('xcache')) {
            die('The xcache extension to be loaded before use!');
        }
        return true;
    }
    /**
     * 写入缓存
     *
     * @access public
     *
     * @param string $key 缓存key
     * @param mixted $value 缓存值
     * @param integer $expire 生存周期
     *
     * @return boolean
     */
     public function set($key, $value, $expire = null) {
        //参数分析         if (!$key) {
            return false;
        }
        $expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
        return xcache_set($key, $value, $expire);
     }
    /**
     * 读取缓存,失败或缓存撒失效时返回 false
     *
     * @access public
     *
     * @param string $key 缓存key
     *
     * @return mixted
     */
     public function get($key) {
        //参数分析         if (!$key) {
            return false;
        }
        return xcache_isset($key) ? xcache_get($key) : false;
     }
    /**
     * 缓存一个变量到数据存储
     *
     * @access public
     *
     * @param string $key 数据key
     * @param mixed $value 数据值
     * @param int $expire 缓存时间(秒)
     *
     * @return boolean
     */
    public function add($key, $value, $expire = null) {
        //参数分析         if (!$key) {
            return false;
        }
        $expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
        return !xcache_isset($key) ? $this->set($key,$value,$expire) : false;
    }
    /**
     * 删除指定的缓存
     *
     * @access public
     *
     * @param string $key 缓存Key
     *
     * @return boolean
     */
     public function delete($key) {
         //参数分析         if (!$key) {
            return false;
        }
        return xcache_unset($key);
     }
    /**
     * 清空全部缓存变量
     *
     * @access public
     * @return boolean
     */
    public function clear() {
        return xcache_clear_cache(XC_TYPE_VAR, 0);
    }
    /**
     * 单例模式
     *
     * 用于本类的单例模式(singleton)实例化
     *
     * @access public
     * @return object
     */
    public static function getInstance() {
        if (!self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.