PHP code example of xmadmax / xcache

1. Go to this page and download the library: Download xmadmax/xcache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xmadmax / xcache example snippets


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


// 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

        "cache_driver": "file",

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

        "cache_driver": "memcache",

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

        "cache_driver": "memcached",

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

        "cache_driver": "mongodb",

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

        "cache_driver": "redis",

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

        "cache_driver": "apc",

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




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


e("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);

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

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


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


e("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();

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


e("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();
$params = array('value1','value2');
$result = $myClass->xCacheMethod("cache_methods","myClass_myMethod",md5('myClass_myMethod'.json_encode($params)),$myClass,'myMethod',$params);

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


e("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);

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


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


e("XCACHE_CONFPATH",'/var/www/myApp/conf'); // Wherever is the xcacheconf.json file
$XCache = new XCache();
$XCache->setCacheHeaders();
.
.
$app = new Bootstrap();
$app->init();
.
.


e("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);

    "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);

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