apc_queue
[ class tree: apc_queue ] [ index: apc_queue ] [ all elements ]

Source for file class.apc_queue.php

Documentation is available at class.apc_queue.php

  1. <?php
  2. /**
  3.  * APC queue
  4.  * @package apc_queue
  5.  * @author Julius Beckmann
  6.  * @link http://juliusbeckmann.de/classes/apc_queue/
  7.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8.  * @filesource
  9.  */
  10.  
  11. /**
  12.  * Simple queue interface
  13.  * @package apc_queue
  14.  */
  15. interface queue_interface{
  16.     public function add($value);
  17.     public function get();
  18.     public function length();
  19. }
  20.  
  21. /**
  22.  * Simple class for fifo queues saved in APC cache
  23.  * @name APC queue
  24.  * @version v0.1_2010.01.29
  25.  * @access public
  26.  * @package apc_queue
  27.  *      
  28.  *       Copyright 2009 Julius Beckmann
  29.  *      
  30.  *       This program is free software; you can redistribute it and/or modify
  31.  *       it under the terms of the GNU General Public License as published by
  32.  *       the Free Software Foundation; either version 2 of the License, or
  33.  *       (at your option) any later version.
  34.  *      
  35.  *       This program is distributed in the hope that it will be useful,
  36.  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38.  *       GNU General Public License for more details.
  39.  *      
  40.  *       You should have received a copy of the GNU General Public License
  41.  *       along with this program; if not, write to the Free Software
  42.  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  43.  *       MA 02110-1301, USA.
  44.  */
  45. class apc_queue implements queue_interface {
  46.     
  47.     /**
  48.      * Overall prefix for queue keys
  49.      */
  50.     private $prefix = 'APC_QUEUE_';
  51.     /**
  52.      * Name of this queue
  53.      */
  54.     private $name = '';
  55.     
  56.     /**
  57.      * Key prefix
  58.      */
  59.     private $key = NULL;
  60.     
  61.     /**
  62.      * Key of head counter
  63.      */
  64.     private $head = NULL;
  65.  
  66.     /**
  67.      * Key of tail counter
  68.      */
  69.     private $tail = NULL;
  70.     
  71.     /**
  72.      * Creates queue object. Every queue needs a name.
  73.      * @param string $queue_name 
  74.      * @param boolean $force_new 
  75.      */
  76.     public function __construct($queue_name$force_new=FALSE{
  77.         $this->name = (string)$queue_name;
  78.         // Defined counter keys
  79.         $this->head = $this->prefix.$this->name.'_head';
  80.         $this->tail = $this->prefix.$this->name.'_tail';
  81.         $this->key  = $this->prefix.$this->name.'_';
  82.         $this->init($force_new);
  83.     }
  84.     
  85.     /**
  86.      * Initializes the queue and checks for counter integrity
  87.      * @access private
  88.      * @param boolean $force_new 
  89.      * @return boolean 
  90.      */
  91.     private function init($force_new=FALSE{
  92.         if($force_new{
  93.             // Force new queue by resettings counters
  94.             apc_store($this->head0);
  95.             apc_store($this->tail0);
  96.         }else{
  97.             // Check head counter
  98.             $success FALSE;
  99.             $val_head apc_fetch($this->head$success)
  100.             if(!$success{
  101.                 apc_store($this->head0);
  102.             }
  103.             // Check tail counter
  104.             $success FALSE;
  105.             $val_tail apc_fetch($this->tail$success)
  106.             if(!$success{
  107.                 apc_store($this->tail0);
  108.             }   
  109.             // Check counter integrity
  110.             if($val_head $val_tail{
  111.                 // Force new queue
  112.                 $this->init(TRUE);
  113.             }
  114.         }
  115.         return TRUE;
  116.     }
  117.     
  118.     /**
  119.      * Adds a value to queue
  120.      * @param mixed $value 
  121.      */
  122.     public function add($value{
  123.         $id apc_fetch($this->head)+1;
  124.         apc_store($this->head$id);
  125.         apc_store($this->key.$id$value);
  126.         return TRUE;
  127.     }
  128.     
  129.     /**
  130.      * Fetches next value from queue. FALSE if empty.
  131.      * @return mixed 
  132.      */
  133.     public function get({
  134.         // Check length
  135.         if($this->length(1{
  136.             // Reset counters if queue is empty
  137.             $this->init(TRUE);
  138.             return FALSE;
  139.         }
  140.         $id apc_fetch($this->tail)+1;
  141.         apc_store($this->tail$id);
  142.         return apc_fetch($this->key.$id);
  143.     }
  144.     
  145.     /**
  146.      * Returns queue length
  147.      * @return integer 
  148.      */
  149.     public function length({
  150.         $head apc_fetch($this->head);
  151.         $tail apc_fetch($this->tail);
  152.         $length $head $tail;
  153.         return ($length >= 0$length ;
  154.     }
  155.     
  156. }
  157.  
  158. // Check if APC has some memory left
  159. // var_dump(apc_sma_info());
  160.  
  161. // Example:
  162.  
  163. /*
  164. $q = new apc_queue('test', isset($_GET['force']));
  165.  
  166. echo "LENGTH: ", $q->length(), "\nSTORE: ";
  167. for($i=0; $i<10; ++$i) {
  168.     $rand = rand(0,9);
  169.     echo $rand,',';
  170.     $q->add($rand);
  171. }
  172. echo "\nLENGTH: ", $q->length(), "\nFETCH: ";
  173. while(($g = $q->get()) !== FALSE) {
  174.     echo $g,',';
  175. }
  176. echo "\nLENGTH: ", $q->length();
  177.  
  178. */

Documentation generated on Fri, 29 Jan 2010 08:49:11 +0100 by phpDocumentor 1.4.3