PHP Classes

XCache: Store and retrieve data from different cache types

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (4 months ago) RSS 2.0 feedNot enough user ratingsTotal: 229 All time: 8,167 This week: 81Up
Version License PHP version Categories
xcache 2.1MIT/X Consortium ...5.3PHP 5, Cache
Description 

Author

This package can store and retrieve data of any type from different cache types.

The base class can perform several types of operations with cached data like storing a given cache entry with a given key name for given amount of time, check if a cached entry exists, retrieve a cached data entry, increment a cache value, delete a cached entry, or delete all cache entries.

The class can also capture the current script output and store it in a cache entry if it is not yet cached.

The package comes with several sub-classes to support storing and retrieving cached data from different containers.

Currently it provides sub-classes to cache data in files, APC, memcached, MongoDB and a dummy container.

The base class can load a configuration script that defines options of the caching process.

Picture of Xavier Pérez
  Performance   Level  
Name: Xavier Pérez is available for providing paid consulting. Contact Xavier Pérez .
Classes: 9 packages by
Country: Spain Spain
Age: ???
All time rank: 132727 in Spain Spain
Week rank: 305 Up12 in Spain Spain Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php
require 'XCache.php';
/**
 * Example using xcache in a class
 *
 * The static var xcache MUST TO EXISTS, this obtains the class itself, and it's used to call any method inside the class.
 *
 * The
 */
class testCache
{
   
// MUST TO EXISTS
   
public static $xcache='';

    public function
__construct()
    {
       
// xcache MUST TO BE ASSIGNED TO THIS CLASS
       
self::$xcache=$this;
    }

    public function
dumpServerVar($item)
    {
       
$XCache = XCache::getXCInstance();
        return
$XCache->cache('cache_test','MyTestInfo','MyTestInfoID',get_class($this),'_dumpServerVar',$item);
    }

    public function
_dumpServerVar($item)
    {
        if (isset(
$_SERVER[$item]))
            return
$_SERVER[$item];
        else
            return
null;
    }

   
/**
     * Function to call other class within XCCache
     *
     * @param type $date
     * @return type
     */
   
public function otherClassCache($date)
    {
       
// xcache MUST TO BE ASSIGNED TO A CLASS
       
$XCache = XCache::getXCInstance();
        return
$XCache->cache('cache_test','MyExternallClassCall','otherClass','otherClassCache','otherMethod',$date);
    }
}

/**
 * Another class to show how can be called any external class
 */
class OtherClassCache
{
   
// MUST TO EXISTS
   
public static $xcache;

    public function
__construct()
    {
       
// xcache MUST TO BE ASSIGNED TO THIS CLASS
       
self::$xcache = $this;
    }
   
    function
otherMethod($date)
    {
        return
"External call to other class successfull on $date";
    }
}

/**
 * Load the test class
 */
$testCache = new testCache();

/**
 * Example loading dumServerVar, this method calls XCache->cache.
 * XCache->cache load _dumpServerVar method if it's not cached through xcache static var.
 */
echo 'Cache $_SERVER[\'REQUEST_TIME\'] : '.$testCache->dumpServerVar('REQUEST_TIME')."<br />";
echo
'$_SERVER[\'REQUEST_TIME\'] date '.date('Y-m-d H:i:s',$testCache->dumpServerVar('REQUEST_TIME'))."<br />";
echo
'$_SERVER[\'REQUEST_TIME\'] cached '.(time()-$testCache->dumpServerVar('REQUEST_TIME')).' seconds ago'."<br />";
echo
'This cache expires each: '.XCache::getXCInstance()->getCacheItemExpiration('cache_test','MyTestInfo','otherClass').' seconds'."<hr />";

/**
 * Example loading other class inside testCache, this method calls XCache->cache.
 * XCache->cache load otherClassCache->otherMethod method if it's not cached through xcache static var.
 */
echo 'Cache other class method : '.$testCache->otherClassCache(date('Y-m-d H:i:s'))."<br />";
echo
'This cache expires each: '.XCache::getXCInstance()->getCacheItemExpiration('cache_test','MyExternallClassCall','MyTestInfoID').' seconds'."<hr />";

/**
 * Example using increment
 */
echo 'Cache & increment test num : '.XCache::getXCInstance()->incCache('cache_test','MyTestCount','MyTestCountID',1)."<hr />";

/**
 * Example saving a fixed value
 */
$randNum = rand(1000,9999);
echo
'Cache a rand value : '.XCache::getXCInstance()->cache('cache_test','MyTestValue','RandVar',$randNum)."<br />";
echo
'This cache expires each: '.XCache::getXCInstance()->getCacheItemExpiration('cache_test','MyTestValue','RandVar').' seconds'."<hr />";

/**
 * Example saving an object
 */
$myObject = new stdClass();
$myObject->id = 24;
$myObject->name = 'Product name';
$myObject->qty_random = rand(1,1000);
$myObject->date_insert = date('Y-m-d H:i:s');

echo
'Cache an object :';
var_dump(XCache::getXCInstance()->cache('cache_test','MyTestValue','MyObject',$myObject));
echo
"<br />";
echo
'This Object cache expires each: '.XCache::getXCInstance()->getCacheItemExpiration('cache_test','MyTestValue','MyObject').' seconds'."<hr />";

/**
 * Memory usage
 */
echo "Memory used: ".number_format(memory_get_usage()/1000,0)."Kb";
?>
<br />
<br />
Press 'refresh' to see how the cache changes ...
<input type='button' value='Refresh' onclick='location.reload();'>


Details

# XCache XCache v.2.2 (26/Jul/2013) XCache can use apc / file / memcache and apc drivers to cache any object. All objects can be grouped under same TTL (time to live), to expire in same ammount of seconds. Can save and retrieve any object / method result / sql result / string / array / file / html Drivers for: files / apc / memcache / mongodb / xcache ## Use 1) Edit xcacheconf.php and set your prefered values. 2) Include XCache.php in your code require ('XCache.php'); 3) Instance XCache $XCache = XCache::getXCInstance(); 4) Create a group in xcacheconf.php and set TTL. $config['cache_test'] = array ('MyTestGroup' => 30); 5) Call XCache: return $XCache->cache('cache_test','MyTestGroup','myUniqueID','MyValueOrObject'); 5) Call a method with XCache : $XCache = XCache::getXCInstance(); return $XCache->cache('cache_test','MyTestGroup','GetProduct_[ProductID]','MyProducts','getProduct','[ProductID]'); This call execute: MyProducts->getProduct($ID) Remember to load (include/require) MyProducts class before to xcache call. Execute 'demo.php' and take a look at the code to see how it works.

  Files folder image Files  
File Role Description
Files folder imagedrivers (6 files)
Accessible without login Plain text file demo.php Example Demo.php
Accessible without login Plain text file readme.md Doc. Readme
Plain text file XCache.php Class Main class
Plain text file xcacheconf.php Conf. XCche config file

  Files folder image Files  /  drivers  
File Role Description
  Plain text file XCache_apc.php Class APC driver
  Plain text file XCache_dummy.php Class Dummy driver
  Plain text file XCache_file.php Class File driver
  Plain text file XCache_memcache.php Class Memcache driver
  Plain text file XCache_mongodb.php Class MongoDB driver
  Plain text file XCache_xcache.php Class New driver

 Version Control Unique User Downloads Download Rankings  
 100%
Total:229
This week:0
All time:8,167
This week:81Up
User Comments (1)
This is a good class ;-)
10 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar