Source for file xcache.php

Documentation is available at xcache.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Cache
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * XCache cache storage handler
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @link        http://xcache.lighttpd.net/
  18.  * @since       11.1
  19.  */
  20. {
  21.     /**
  22.      * Get cached data by id and group
  23.      *
  24.      * @param   string   $id         The cache data id
  25.      * @param   string   $group      The cache data group
  26.      * @param   boolean  $checkTime  True to verify cache time expiration threshold
  27.      *
  28.      * @return  mixed  Boolean false on failure or a cached data string
  29.      *
  30.      * @since   11.1
  31.      */
  32.     public function get($id$group$checkTime true)
  33.     {
  34.         $cache_id $this->_getCacheId($id$group);
  35.         $cache_content xcache_get($cache_id);
  36.  
  37.         if ($cache_content === null)
  38.         {
  39.             return false;
  40.         }
  41.  
  42.         return $cache_content;
  43.     }
  44.  
  45.     /**
  46.      * Get all cached data
  47.      *
  48.      * This requires the php.ini setting xcache.admin.enable_auth = Off.
  49.      *
  50.      * @return  array  data
  51.      *
  52.      * @since   11.1
  53.      */
  54.     public function getAll()
  55.     {
  56.         parent::getAll();
  57.  
  58.         $allinfo xcache_list(XC_TYPE_VAR0);
  59.         $keys $allinfo['cache_list'];
  60.         $secret $this->_hash;
  61.  
  62.         $data array();
  63.  
  64.         foreach ($keys as $key)
  65.         {
  66.  
  67.             $namearr explode('-'$key['name']);
  68.  
  69.             if ($namearr !== false && $namearr[0== $secret && $namearr[1== 'cache')
  70.             {
  71.                 $group $namearr[2];
  72.  
  73.                 if (!isset($data[$group]))
  74.                 {
  75.                     $item new JCacheStorageHelper($group);
  76.                 }
  77.                 else
  78.                 {
  79.                     $item $data[$group];
  80.                 }
  81.  
  82.                 $item->updateSize($key['size'1024);
  83.  
  84.                 $data[$group$item;
  85.             }
  86.         }
  87.  
  88.         return $data;
  89.     }
  90.  
  91.     /**
  92.      * Store the data by id and group
  93.      *
  94.      * @param   string  $id     The cache data id
  95.      * @param   string  $group  The cache data group
  96.      * @param   string  $data   The data to store in cache
  97.      *
  98.      * @return  boolean  True on success, false otherwise
  99.      *
  100.      * @since   11.1
  101.      */
  102.     public function store($id$group$data)
  103.     {
  104.         $cache_id $this->_getCacheId($id$group);
  105.         $store xcache_set($cache_id$data$this->_lifetime);
  106.         return $store;
  107.     }
  108.  
  109.     /**
  110.      * Remove a cached data entry by id and group
  111.      *
  112.      * @param   string  $id     The cache data id
  113.      * @param   string  $group  The cache data group
  114.      *
  115.      * @return  boolean  True on success, false otherwise
  116.      *
  117.      * @since   11.1
  118.      */
  119.     public function remove($id$group)
  120.     {
  121.         $cache_id $this->_getCacheId($id$group);
  122.  
  123.         if (!xcache_isset($cache_id))
  124.         {
  125.             return true;
  126.         }
  127.  
  128.         return xcache_unset($cache_id);
  129.     }
  130.  
  131.     /**
  132.      * Clean cache for a group given a mode.
  133.      *
  134.      * This requires the php.ini setting xcache.admin.enable_auth = Off.
  135.      *
  136.      * @param   string  $group  The cache data group
  137.      * @param   string  $mode   The mode for cleaning cache [group|notgroup]
  138.      *  group mode  : cleans all cache in the group
  139.      *  notgroup mode  : cleans all cache not in the group
  140.      *
  141.      * @return  boolean  True on success, false otherwise
  142.      *
  143.      * @since   11.1
  144.      */
  145.     public function clean($group$mode null)
  146.     {
  147.         $allinfo xcache_list(XC_TYPE_VAR0);
  148.         $keys $allinfo['cache_list'];
  149.  
  150.         $secret $this->_hash;
  151.         foreach ($keys as $key)
  152.         {
  153.             if (strpos($key['name']$secret '-cache-' $group '-'=== xor $mode != 'group')
  154.             {
  155.                 xcache_unset($key['name']);
  156.             }
  157.         }
  158.         return true;
  159.     }
  160.  
  161.     /**
  162.      * Garbage collect expired cache data
  163.      *
  164.      * This is a dummy, since xcache has built in garbage collector, turn it
  165.      * on in php.ini by changing default xcache.gc_interval setting from
  166.      * 0 to 3600 (=1 hour)
  167.      *
  168.      * @return  boolean  True on success, false otherwise.
  169.      *
  170.      * @since   11.1
  171.      */
  172.     public function gc()
  173.     {
  174.         /*
  175.         $now = time();
  176.  
  177.         $cachecount = xcache_count(XC_TYPE_VAR);
  178.  
  179.             for ($i = 0; $i < $cachecount; $i ++) {
  180.  
  181.                 $allinfo  = xcache_list(XC_TYPE_VAR, $i);
  182.                 $keys = $allinfo ['cache_list'];
  183.  
  184.                 foreach($keys as $key) {
  185.  
  186.                     if (strstr($key['name'], $this->_hash)) {
  187.                         if (($key['ctime'] + $this->_lifetime ) < $this->_now) xcache_unset($key['name']);
  188.                     }
  189.                 }
  190.             }
  191.  
  192.          */
  193.  
  194.         return true;
  195.     }
  196.  
  197.     /**
  198.      * Test to see if the cache storage is available.
  199.      *
  200.      * @return  boolean  True on success, false otherwise.
  201.      *
  202.      * @since   12.1
  203.      */
  204.     public static function isSupported()
  205.     {
  206.         return (extension_loaded('xcache'));
  207.     }
  208. }

Documentation generated on Tue, 19 Nov 2013 15:18:36 +0100 by phpDocumentor 1.4.3