xdmax/xcache

XCache,一款多用途缓存

3.0.5007 2018-12-11 15:11 UTC

README

XCache 允许缓存一切,包括 html、json、视图、对象、从 Class->method 获取的结果,还可以为 CDN 或代理添加 Cache-Control 头。版本 3.0.5007

允许三种方法

  • 作为特质,继承所有方法并自动缓存方法。
  • 作为特质,在同一个类中使用 XCache 方法。
  • 独立,将 XCache 作为单独的类使用。

XCache 包含以下驱动程序

  • 文件
  • Memcache
  • Memcached
  • MongoDB
  • Apc
  • Redis

新增:添加 XCACHE_BASEID 允许在同一源代码上分离缓存

安装

Composer

编辑你的基础 composer.json,并添加

{
    "require": {
        "xmadmax/xcache": "3.*"
    }
}

更新 composer

$ composer update

现在,你可以在任何 PHP 文件中使用 composer 自动加载包含 XCache

<?php
// First of all, define the xcacheconf configuration dir location (the name of this file will be ever xcacheconf.json)
define("XCACHE_CONFPATH",__DIR__)
// Include composer autoload
include_once "../../vendor/autoload.php";

配置

XCache 驱动程序

文件

使用以下内容配置 xcacheconf.json

        "cache_driver": "file",

在 cache_host 组中,定义要创建的 'cache' 目录的绝对路径

        "file" : {
            "path": "/var/tmp/xcache/",
            "options": false,
            "compress": true
        },

在 Windows 中,路径相对于 PHP 程序所在的驱动器。压缩选项可用,压缩需要更多时间,但读取更快,磁盘空间更少。

Memcache
Configure xcacheconf.json with:
        "cache_driver": "memcache",

在 cache_hosts 组中

        "memcache" : {
            "host": "127.0.0.1:11211",
            "options": false,
            "compress": true
        },

将主机定义为 "host:port"。压缩选项可用,压缩需要更多时间,但读取更快,共享内存空间更少。如果 php_memcache 不可用,则使用文件驱动程序。

Memcached

使用以下内容配置 xcacheconf.json

        "cache_driver": "memcached",

在 cache_hosts 组中

        "memcached" : {
            "host": "127.0.0.1:11211",
            "options": false,
            "compress": true
        },

将主机定义为 "host:port",或 "host:port,host:port,host:port" 用于一组服务器。OPT_PREFIX 将组合任何保存到集合中的项目的键,允许 xcache/memcached 同时与其他应用程序一起使用。压缩选项可用,压缩需要更多时间,但读取更快,共享内存空间更少。如果 php_memcached 不可用,则使用文件驱动程序。

MongoDB

使用以下内容配置 xcacheconf.json

        "cache_driver": "mongodb",

在 cache_hosts 组中

        "mongodb" : {
            "host": "127.0.0.1:27017:::xcachedb:xcachecollection",
            "options": {
                "OPT_PREFIX" : "mytest"
            },
            "compress": false
        },

将主机定义为 "host:port:user:passwd:database:collection"。OPT_PREFIX 将组合任何保存到集合中的项目的键,允许 xcache/mongodb 同时与其他应用程序一起使用。压缩选项可用,压缩需要更多时间,但读取更快,ram/disk 空间更少。如果 php_mongodb 不可用,则使用文件驱动程序。

Redis

使用以下内容配置 xcacheconf.json

        "cache_driver": "redis",

在 cache_hosts 组中

        "mongodb" : {
            "host": "127.0.0.1:6379",
            "options": {
                "OPT_PREFIX" : "mytest"
            },
            "compress": false
        },

将主机定义为 "host:port"。OPT_PREFIX 将组合任何保存到集合中的项目的键,允许 xcache/redis 同时与其他应用程序一起使用。压缩选项可用,压缩需要更多时间,但读取更快,ram/disk 空间更少。如果 php_redis 不可用,则使用文件驱动程序。

Apc
Configure xcacheconf.json with:
        "cache_driver": "apc",

在 cache_hosts 组中

        "apc" : {
            "options": false,
            "compress": false
        },

压缩选项可用,压缩需要更多时间,但读取更快,共享内存空间更少。如果 php_apc 不可用,则使用文件驱动程序。

其他选项

即使 $_GET 设置,也要缓存

如果您想在收到 GET 参数时也不缓存(例如:http://www.myweb.com?param=111),则必须将 cache_get 设置为 'true'。即使页面带有 GET 参数调用,它们也将是唯一 ID 的一部分,以组成缓存键,从而允许在 GET 参数更改时拥有不同的缓存。

即使 $_POST 设置,也要缓存

如果您想在收到 POST 参数时也不缓存,则必须将 cache_post 设置为 'true'。即使页面带有 POST 参数调用,它们也将是唯一 ID 的一部分,以组成缓存键,从而允许在 POST 参数更改时拥有不同的缓存。

仅在用户未登录时缓存

如果您想在用户未登录时才缓存,您的应用程序必须在浏览器中写入一个cookie。cookie的名称可以在'cache_logged_cookie'选项中配置。当XCache检测到这个cookie,并且'cache_only_not_logged_pages'设置为'true'时,缓存将被禁用。

XCache使用和示例

XCache可以缓存

  • 缓存您类中的任何方法(可以调用类中的任何方法,使用特性)
  • 通过URL缓存整个PHP代码(整个index.php、bootstrap等)。每个URI都可以有自己的缓存TTL
  • 缓存类中的特定方法
  • 缓存任何单个值(一个键/值对)
  • 只缓存已定义的值
  • 缓存每个值的定义TTL
  • 为任何页面设置Cache-control头,以便CDN能够理解。

缓存任何方法的自动缓存

如果您有一个这样的类

<?php

class myClass extends myMasterClass
{
    public function myMethod($params)
    {
        .
        .
        .
        return $something
    }
}

按照说明修改

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file

class myClass
{
    use XCacheDriver;  // Include this line after class definition
    public function __construct()
    {
        // This makes the magic happen
        $this->xCachePass();
    }

    public function _myMethod($params)   // Add a single '_' before the name of the method
    {
        .
        .
        .
        return $something
    }
}

现在,您可以调用

$params = 'a string, an array, an object ...';
$myClass = new myClass();
$result = $myClass->myMethod($params);

XCacheDriver将在'myClass'中找不到'myMethod',但会找到'_myMethod'。然后将在xcacheconf.json中查找,在'cache_method'组中,名称为'myClass_myMethod',并检索此缓存的TTL。xcacheconf.json可能看起来像这样

    "cache_methods": {
        "default": 0,
        "myClass_myMethod": 300
    },

然后,在调用$myClass->myMethod($params)之前,XCache将寻找此方法及参数的缓存,如果找到,则返回值,如果没有找到,则调用方法并将返回值缓存。

您可以配置正则表达式以缓存一组方法,例如,缓存myClass中的所有方法

    "cache_methods": {
        "default": 0,
         "regexp": {
            "^myClass_": 300
        }
   },

正则表达式使用preg_match进行评估。您可以使用preg_match接受的任何组合。

缓存整个PHP代码

您可以缓存代码块的所有输出,缓存ID将是REQUEST_URI:如果您有一个带有bootstrap或其他启动基础核心类的php

<?php
.
.
$app = new Bootstrap();
$app->init();
.
.

按照说明修改代码

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file
$XCache = new XCache();
if ($XCache->enableCache()) {
    die();
}
.
.
$app = new Bootstrap();
$app->init();
.
.
$XCache->writeAndFlushCache();

writeAndFlushCache方法必须是PHP文件结束处的最后一行。

要使其工作,您需要在xcacheconffile.json中配置'cache_pages'组,以检测REQUEST_URI。例如,如果bootstrap文件默认由htaccess加载,它将捕获任何URL,如'/'或'/mypage',然后此参数必须在xcacheconffile.json中定义

    "cache_pages": {
        "default": 30,
        "regexp": {
            "^/$": 3600,
            "^/mypage$": 600
        }
    },

'home'将缓存1小时,'mypage'将缓存5分钟,任何其他页面将缓存30秒(默认)

缓存类中的特定方法

XCache可以独立工作或作为任何类的特性。要独立工作,使用前面的示例(缓存整个PHP代码),要作为特性工作,使用示例(自动缓存任何方法)。这对于您想在任何PHP代码中包含XCache非常有用。

XCacheDriver有两个绕过XCache的方法

  • xCacheMethod
  • xCacheValue

这两个方法都可以在任何具有与(自动缓存任何方法)中描述的结构相同的类内部调用,例如

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file

class myClass
{
    use XCacheDriver;  // Include this line after class definition
    public function myMethod($params)
    {
        .
        .
        .
        return $something
    }
}

'myMethod'没有下划线,但也可以使用'xcacheMethod'调用

$myClass = new myClass();
$params = array('value1','value2');
$result = $myClass->xCacheMethod("cache_methods","myClass_myMethod",md5('myClass_myMethod'.json_encode($params)),$myClass,'myMethod',$params);

如果存在缓存,则从缓存中检索结果,如果不存在,则调用$myClass->myMethod($params),存储在缓存中并返回。这允许在只有一行的情况下进行调用和缓存检索。如前所述,您需要在xcacheconf.json文件中的'cache_method'组中添加"myClass_myMethod",

    "cache_methods": {
        "default": 0,
        "myClass_myMethod": 300
   },

xCacheMethod接受6个参数

  1. 缓存组
  2. 缓存名称
  3. 唯一ID,必须包含参数值
  4. 对象类
  5. 方法名称
  6. 参数

缓存键/值对

如前所述,"xCacheValue"也适用于任何继承XCacheDriver特性的类。使用前面的相同示例

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file

class myClass
{
    use XCacheDriver;  // Include this line after class definition
    public function myMethod($params)
    {
        .
        .
        .
        return $something
    }
}

用于保存结果值

$myClass = new myClass();
$result = $myClass->myMethod($params);
$myClass->xCacheValue("cache_values","myResult",md5('myResult'),$result);

要使其工作,必须将"myResult"键配置在xcacheconf.json文件中的'cache_values'组中,否则将使用'default' TTL:"cache_values":{"default":15,"regexp":"^myResult$":300}

要在任何其他PHP代码或行中检索此结果

$result = $myClass->xCacheValue("cache_values","myResult",md5('myResult'));

设置Cache-control头

XCache只能将'Cache-control' HTML头放入CDN可以理解的格式。使用与整个页面缓存相同的示例,在index.php或bootsrap.php中

<?php
.
.
$app = new Bootstrap();
$app->init();
.
.

按照说明修改代码

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file
$XCache = new XCache();
$XCache->setCacheHeaders();
.
.
$app = new Bootstrap();
$app->init();
.
.

不使用XCacheDriver使用XCache

在您的PHP文件中包含composer的autoload。

<?php
require_once '../../vendor/autoload.php'; // Wherever is the composer autoload file
define("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file

缓存类中的特定方法

$XCache = new XCache();
$myClass = new myClass();
$params = array('value1','value2');
$result = $XCache->cache("cache_methods","myClass_myMethod",md5('myClass_myMethod'.json_encode($params)),$myClass,'myMethod',$params);

如前所述,您需要在xcacheconf.json文件中将"myClass_myMethod"添加到'cache_method'组中

    "cache_methods": {
        "default": 0,
        "myClass_myMethod": 300
   },

缓存键/值对

$XCache = new XCache();
$myClass = new myClass();
$result = $myClass->myMethod($params);
$cached = $XCache->cache("cache_values","myResult",md5('myResult'),$result);

要使其工作,必须将"myResult"键配置在xcacheconf.json文件中的'cache_values'组中,否则将使用'default' TTL:"cache_values":{"default":15,"regexp":"^myResult$":300}

要在任何其他PHP代码或行中检索此结果

$result = $XCache->cache("cache_values","myResult",md5('myResult'));