Source for file apc.php

Documentation is available at apc.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.  * APC cache storage handler
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @see         http://php.net/manual/en/book.apc.php
  18.  * @since       11.1
  19.  */
  20. {
  21.     /**
  22.      * Get cached data from APC 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.         return apc_fetch($cache_id);
  36.     }
  37.  
  38.     /**
  39.      * Get all cached data
  40.      *
  41.      * @return  array  data
  42.      *
  43.      * @since   11.1
  44.      */
  45.     public function getAll()
  46.     {
  47.         parent::getAll();
  48.  
  49.         $allinfo apc_cache_info('user');
  50.         $keys $allinfo['cache_list'];
  51.         $secret $this->_hash;
  52.  
  53.         $data array();
  54.  
  55.         foreach ($keys as $key)
  56.         {
  57.  
  58.             $name $key['info'];
  59.             $namearr explode('-'$name);
  60.  
  61.             if ($namearr !== false && $namearr[0== $secret && $namearr[1== 'cache')
  62.             {
  63.                 $group $namearr[2];
  64.  
  65.                 if (!isset($data[$group]))
  66.                 {
  67.                     $item new JCacheStorageHelper($group);
  68.                 }
  69.                 else
  70.                 {
  71.                     $item $data[$group];
  72.                 }
  73.  
  74.                 $item->updateSize($key['mem_size'1024);
  75.  
  76.                 $data[$group$item;
  77.             }
  78.         }
  79.  
  80.         return $data;
  81.     }
  82.  
  83.     /**
  84.      * Store the data to APC by id and group
  85.      *
  86.      * @param   string  $id     The cache data id
  87.      * @param   string  $group  The cache data group
  88.      * @param   string  $data   The data to store in cache
  89.      *
  90.      * @return  boolean  True on success, false otherwise
  91.      *
  92.      * @since   11.1
  93.      */
  94.     public function store($id$group$data)
  95.     {
  96.         $cache_id $this->_getCacheId($id$group);
  97.         return apc_store($cache_id$data$this->_lifetime);
  98.     }
  99.  
  100.     /**
  101.      * Remove a cached data entry by id and group
  102.      *
  103.      * @param   string  $id     The cache data id
  104.      * @param   string  $group  The cache data group
  105.      *
  106.      * @return  boolean  True on success, false otherwise
  107.      *
  108.      * @since   11.1
  109.      */
  110.     public function remove($id$group)
  111.     {
  112.         $cache_id $this->_getCacheId($id$group);
  113.         return apc_delete($cache_id);
  114.     }
  115.  
  116.     /**
  117.      * Clean cache for a group given a mode.
  118.      *
  119.      * group mode    : cleans all cache in the group
  120.      * notgroup mode : cleans all cache not in the group
  121.      *
  122.      * @param   string  $group  The cache data group
  123.      * @param   string  $mode   The mode for cleaning cache [group|notgroup]
  124.      *
  125.      * @return  boolean  True on success, false otherwise
  126.      *
  127.      * @since   11.1
  128.      */
  129.     public function clean($group$mode null)
  130.     {
  131.         $allinfo apc_cache_info('user');
  132.         $keys $allinfo['cache_list'];
  133.         $secret $this->_hash;
  134.  
  135.         foreach ($keys as $key)
  136.         {
  137.  
  138.             if (strpos($key['info']$secret '-cache-' $group '-'=== xor $mode != 'group')
  139.             {
  140.                 apc_delete($key['info']);
  141.             }
  142.         }
  143.         return true;
  144.     }
  145.  
  146.     /**
  147.      * Force garbage collect expired cache data as items are removed only on fetch!
  148.      *
  149.      * @return  boolean  True on success, false otherwise.
  150.      *
  151.      * @since   11.1
  152.      */
  153.     public function gc()
  154.     {
  155.         $allinfo apc_cache_info('user');
  156.         $keys $allinfo['cache_list'];
  157.         $secret $this->_hash;
  158.  
  159.         foreach ($keys as $key)
  160.         {
  161.             if (strpos($key['info']$secret '-cache-'))
  162.             {
  163.                 apc_fetch($key['info']);
  164.             }
  165.         }
  166.     }
  167.  
  168.     /**
  169.      * Test to see if the cache storage is available.
  170.      *
  171.      * @return  boolean  True on success, false otherwise.
  172.      *
  173.      * @since   12.1
  174.      */
  175.     public static function isSupported()
  176.     {
  177.         return extension_loaded('apc');
  178.     }
  179.  
  180.     /**
  181.      * Lock cached item - override parent as this is more efficient
  182.      *
  183.      * @param   string   $id        The cache data id
  184.      * @param   string   $group     The cache data group
  185.      * @param   integer  $locktime  Cached item max lock time
  186.      *
  187.      * @return  object   Properties are lock and locklooped
  188.      *
  189.      * @since   11.1
  190.      */
  191.     public function lock($id$group$locktime)
  192.     {
  193.         $returning new stdClass;
  194.         $returning->locklooped false;
  195.  
  196.         $looptime $locktime 10;
  197.  
  198.         $cache_id $this->_getCacheId($id$group'_lock';
  199.  
  200.         $data_lock apc_add($cache_id1$locktime);
  201.  
  202.         if ($data_lock === false)
  203.         {
  204.  
  205.             $lock_counter 0;
  206.  
  207.             // Loop until you find that the lock has been released.
  208.             // That implies that data get from other thread has finished
  209.             while ($data_lock === false)
  210.             {
  211.  
  212.                 if ($lock_counter $looptime)
  213.                 {
  214.                     $returning->locked false;
  215.                     $returning->locklooped true;
  216.                     break;
  217.                 }
  218.  
  219.                 usleep(100);
  220.                 $data_lock apc_add($cache_id1$locktime);
  221.                 $lock_counter++;
  222.             }
  223.  
  224.         }
  225.         $returning->locked $data_lock;
  226.  
  227.         return $returning;
  228.     }
  229.  
  230.     /**
  231.      * Unlock cached item - override parent for cacheid compatibility with lock
  232.      *
  233.      * @param   string  $id     The cache data id
  234.      * @param   string  $group  The cache data group
  235.      *
  236.      * @return  boolean  True on success, false otherwise.
  237.      *
  238.      * @since   11.1
  239.      */
  240.     public function unlock($id$group null)
  241.     {
  242.         $cache_id $this->_getCacheId($id$group'_lock';
  243.  
  244.         $unlock apc_delete($cache_id);
  245.         return $unlock;
  246.     }
  247. }

Documentation generated on Tue, 19 Nov 2013 14:53:43 +0100 by phpDocumentor 1.4.3