wiki:XcacheApi

Version 21 (modified by moo, 6 years ago) (diff)

--

XCache APIs exported to PHP script

Constants

XC_TYPE_PHP
Cache Type = php opcode
XC_TYPE_VAR
Cache Type = variable data

Functions

Common Used Functions
mixed xcache_get(string name)
bool  xcache_set(string name, mixed value [, int ttl])
bool  xcache_isset(string name)
bool  xcache_unset(string name)
int   xcache_inc(string name [, int value [, int ttl]])
int   xcache_dec(string name [, int value [, int ttl]])

Warning : At the moment, It is not possible to store resources, callbacks or objects using xcache_* functions.

Examples:

Simple Counter
This guest book has been visited <?php echo xcache_inc("count"); ?> times.
Advanced Counter
<?php
if (!xcache_isset("count")) {
  xcache_set("count", load_count_from_mysql());
}
?>
This guest book has been visited <?php echo $count = xcache_inc("count"); ?> times.
<?php
// save every 100 hits
if (($count % 10) == 100) {
  save_count_to_mysql($count);
}
?>
Cacher
<?php
define(TMPDIR, '/tmp');

function load_abc_data()
{
  if (xcache_isset("abc_data")) {
    return xcache_get("abc_data");
  }
  // it worth a lock here to avoid useless yet harmful concurrent load from any slow backend (backend=mysql here).
  $fp = fopen(TMPDIR . "/abc_data.lock", "w");
  flock($fp, LOCK_EX);
  // check AGAIN after we get the lock
  if (xcache_isset("abc_data")) {
    fclose($fp);
    return xcache_get("abc_data");
  }
  mysql_query .... and get $data
  xcache_set("abc_data", $data, 120); // save for 2 minutes
  fclose($fp);
  return $data;
}

?>
a Simple OO wrapper
<?php

/**
 * XCache
 *
 * @package XCache
 * @version $Id$
 * @copyright 2007
 * @author Cristian Rodriguez <judas.iscariote@flyspray.org>
 * @license BSD {@link http://www.opensource.org/licenses/bsd-license.php}
 */

class XCache {
    
    private static $xcobj;
    
    private function __construct()
    {
    }

    public final function __clone()
    {
        throw new BadMethodCallException("Clone is not allowed");
    } 
    
    /**
     * getInstance 
     * 
     * @static
     * @access public
     * @return object XCache instance
     */
    public static function getInstance() 
    {
        if (!(self::$xcobj instanceof XCache)) {
            self::$xcobj = new XCache;
        }
        return self::$xcobj; 
    }

    /**
     * __set 
     * 
     * @param mixed $name 
     * @param mixed $value 
     * @access public
     * @return void
     */
    public function __set($name, $value)
    {
        xcache_set($name, $value);
    }

    /**
     * __get 
     * 
     * @param mixed $name 
     * @access public
     * @return void
     */
    public function __get($name)
    {
        return xcache_get($name);
    }

    /**
     * __isset 
     * 
     * @param mixed $name 
     * @access public
     * @return bool
     */
    public function __isset($name)
    {
        return xcache_isset($name);
    }

    /**
     * __unset 
     * 
     * @param mixed $name 
     * @access public
     * @return void
     */
    public function __unset($name)
    {
        xcache_unset($name);
    }
}

//example use...

$cache = XCache::getInstance();
$cache->foo = PHP_INT_MAX;
$cache->bar = range('a', 'z');
var_dump($cache->foo);
var_dump($cache->bar);
unset($cache->bar);
var_dump(isset($cache->bar));
Administrator Functions
int    xcache_count(int type)
array  xcache_info(int type, int id)
array  xcache_list(int type, int id)
void   xcache_clear_cache(int type, int id)
string xcache_coredump(int op_type)

See xcache/admin/*.php

Coverager Functions
array xcache_coverager_decode(string data)
void  xcache_coverager_start([bool clean = true])
void  xcache_coverager_stop([bool clean = false])
array xcache_coverager_get([bool clean = false])
xcache_coverager_decode
Decode data from file that is stored by coverage auto dumper (by setting xcache.coveragedump_directory)
Dis/Assembler? Opcode Functions
string xcache_asm(string filename)
string xcache_dasm_file(string filename)
string xcache_dasm_string(string code)
string xcache_encode(string filename)
bool   xcache_decode(string filename)

string xcache_get_op_type(int op_type)
string xcache_get_data_type(int type)
string xcache_get_opcode(int opcode)
string xcache_get_op_spec(int op_type)
string xcache_get_opcode_spec(int opcode)
mixed  xcache_get_special_value(zval value)
string xcache_is_autoglobal(string name)

See xcache/Decompiler.class.php

Note: Disassembler isn't available for win32 built binaries due to license problem.