Source for file OCSP_FRMCONTR_CACHEDLIST.phpclass

Documentation is available at OCSP_FRMCONTR_CACHEDLIST.phpclass

  1. <?php
  2. /**
  3.   * Class file OCSP_FRMCONTR_CACHEDLIST_APC.phpclass
  4.   *
  5.   *
  6.   * @project    Open CSP-Management
  7.   * @package    forms
  8.   * @category   dbms_form
  9.   *
  10.   * @author     Peter Krebs (pk) <pitlinz@users.sourceforge.net>
  11.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  12.   *
  13.   * @since  pk-08-01-19
  14.   *
  15.   ***/
  16.  
  17. require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_LIST.phpclass";
  18.  
  19. /**
  20.   * abstract class OCSP_FRMCONTR_CACHEDLIST
  21.   *
  22.   * @project    Open CSP-Management
  23.   * @package    forms
  24.   * @category   dbms_form
  25.   *
  26.   * @author     Peter Krebs (pk) <pitlinz@users.sourceforge.net>
  27.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  28.   *
  29.   * @since  pk-08-01-19
  30.   *    *
  31.   */
  32. abstract class OCSP_FRMCONTR_CACHEDLIST extends OCSP_FRMCONTR_LIST
  33. {
  34.     /*** class constants  --------------------------------------------- */
  35.     
  36.     /**
  37.      * type of the cache used
  38.      * 
  39.      * @constant int _MY_FRMLIST_CACHETYPE_
  40.      * @abstract
  41.      * 
  42.      */
  43.     const _MY_FRMLIST_CACHETYPE_=0;
  44.         
  45.     /*** class variables  --------------------------------------------- */    
  46.  
  47.     
  48.     /*** compositions ------------------------------------------------  */
  49.  
  50.     /*** aggregation -------------------------------------------------- */
  51.  
  52.     /*** attributes --------------------------------------------------- */
  53.         
  54.     /**
  55.      * hashKey to identify the list in the cache
  56.      * 
  57.      * @var string $myCacheKey 
  58.      */
  59.     protected $myCacheKey="";
  60.     
  61.     /**
  62.      * systemtime when the rows where last filled from the storage
  63.      * 
  64.      * @var int $fillTime 
  65.      */
  66.     protected $fillTime=0;
  67.     
  68.     /**
  69.      * ttl of the storage result
  70.      *
  71.      * @var int 
  72.      */
  73.     protected $cacheRowTTL    =300;    // 5 minutes
  74.     
  75.  
  76.            
  77.     
  78.     /**
  79.      * array holding all rows
  80.      * 
  81.      * @var array $myCacheRows 
  82.      */
  83.     protected $myCacheRows=array();
  84.     
  85.     /**
  86.       *
  87.       * FACTORY _____________________________________________
  88.       *
  89.       */    
  90.     
  91.     /**
  92.      * includes the propper cache class and returns the class name
  93.      *
  94.      * @param unknown_type $aCacheType 
  95.      * @param unknown_type $debug 
  96.      * 
  97.      * @return string 
  98.      */
  99.     public static function includeCacheClass($aCacheType,$debug=FALSE)
  100.     {
  101.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_FRMCONTR_CACHEDLIST::loadCacheClass");
  102.             switch($aCacheType)
  103.         {
  104.             case _OCSP_CACHE_MEMCACHE_:
  105.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_MEMCACHE'))
  106.                 {
  107.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_MEMCACHE.phpclass";
  108.                 }
  109.                 return "OCSP_FRMCONTR_CACHEDLIST_MEMCACHE";
  110.               case _OCSP_CACHE_APC_:
  111.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_APC'))
  112.                 {
  113.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_APC.phpclass";
  114.                 }
  115.                 return "OCSP_FRMCONTR_CACHEDLIST_APC";
  116.              case _OCSP_CACHE_PEAR_:
  117.                 if (!class_exists('Cache_Lite'))
  118.                 {
  119.                     
  120.                     require_once "Cache/Lite.php";
  121.                   }
  122.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_PEAR'))
  123.                 {
  124.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_PEAR.phpclass";
  125.                    }
  126.                 return "OCSP_FRMCONTR_CACHEDLIST_PEAR";
  127.                case _OCSP_CACHE_DB_:
  128.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_DB'))                    
  129.                 {
  130.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_DB.phpclass";
  131.                 }
  132.                 return "OCSP_FRMCONTR_CACHEDLIST_DB";
  133.               case _OCSP_CACHE_SESSION_:
  134.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_SESSION'))                    
  135.                 {
  136.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_SESSION.phpclass";
  137.                    }
  138.                 return "OCSP_FRMCONTR_CACHEDLIST_SESSION";
  139.              default:
  140.                 return NULL;                    
  141.           }        
  142.         
  143.     }
  144.     
  145.     
  146.     /**
  147.      * factory a new cached list controll
  148.      *
  149.      * @param int $aCacheType 
  150.      * @param boolean $debug 
  151.      * 
  152.      * @return OCSP_FRMCONTR_CACHEDLIST 
  153.      */
  154.     public static function factoryNewByCacheType($aCacheType,$debug=FALSE)
  155.     {
  156.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_FRMCONTR_CACHEDLIST::factoryCacheType");
  157.                 
  158.         switch($aCacheType)
  159.         {
  160.             case _OCSP_CACHE_MEMCACHE_:
  161.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_MEMCACHE'))
  162.                 {
  163.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_MEMCACHE.phpclass";
  164.                 }
  165.                 return new OCSP_FRMCONTR_CACHEDLIST_MEMCACHE();
  166.               case _OCSP_CACHE_APC_:
  167.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_APC'))
  168.                 {
  169.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_APC.phpclass";
  170.                 }
  171.                 return new OCSP_FRMCONTR_CACHEDLIST_APC();
  172.              case _OCSP_CACHE_PEAR_:
  173.                 if (!class_exists('Cache_Lite'))
  174.                 {
  175.                     require_once "Cache/Lite.php";
  176.                   }
  177.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_PEAR'))
  178.                 {
  179.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_PEAR.phpclass";
  180.                    }
  181.                 return new OCSP_FRMCONTR_CACHEDLIST_PEAR();
  182.                case _OCSP_CACHE_DB_:
  183.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_DB'))                    
  184.                 {
  185.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_DB.phpclass";
  186.                 }
  187.                 return new OCSP_FRMCONTR_CACHEDLIST_DB();
  188.               case _OCSP_CACHE_SESSION_:
  189.                 if (!class_exists('OCSP_FRMCONTR_CACHEDLIST_SESSION'))                    
  190.                 {
  191.                     require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_FRMCONTR_CACHEDLIST_SESSION.phpclass";
  192.                    }
  193.                 return new OCSP_FRMCONTR_CACHEDLIST_SESSION();
  194.              default:
  195.                 return NULL;                    
  196.           }
  197.     }
  198.  
  199.    /**
  200.      * factory a new cached list controll
  201.      *
  202.      * @param int $aCacheType 
  203.      * @param boolean $debug 
  204.      * 
  205.      * @return OCSP_FRMCONTR_CACHEDLIST 
  206.      */
  207.     public static function factoryCachedList($aCacheType,$aHashKey,$debug=FALSE)
  208.     {                
  209.         if ($obj_controll=OCSP_FRMCONTR_CACHEDLIST::factoryNewByCacheType($aCacheType))
  210.         {
  211.             if ($arr_desc=$obj_controll->getCacheDesc($aHashKey))
  212.             {
  213.                 if ($debugecho "alert('desc: ".pcf_js_escape(print_r($arr_desc,TRUE))."');";
  214.                 if (intval($arr_desc['FORMID']))
  215.                 {
  216.                     $obj_form=OCSP_FORM::factory_from_id(intval($arr_desc['FORMID']));
  217.  
  218.                     $arr_settings=array('CONTROBJ' => &$obj_controll);                    
  219.                     $obj_form->prepareList($arr_settings,$debug);  
  220.                     $obj_controll->setCacheDesc($arr_desc);                                    
  221.                     return $obj_controll;
  222.                 }
  223.             }
  224.         }
  225.         
  226.         return NULL;
  227.     }
  228.     
  229.     
  230.     
  231.     
  232.     /**
  233.       *
  234.       * METHODS _____________________________________________
  235.       *
  236.       */        
  237.     
  238.     //---------------------------------------------------
  239.     // getter/setter
  240.     //---------------------------------------------------
  241.  
  242.  
  243.     /**
  244.      * returns the cache type used
  245.      *
  246.      * @return int (_OCSP_CACHE_* constant value)
  247.      */
  248.     function getCacheType()
  249.     {
  250.         return $this->get_myClassConstant('_MY_FRMLIST_CACHETYPE_');
  251.     }
  252.     
  253.    
  254.     //---------------------------------------------------
  255.     // hash key
  256.     //---------------------------------------------------
  257.         
  258.     /**
  259.      * generates and returns the cacheKey
  260.      * 
  261.      * if none is set a new one is created
  262.      *
  263.      * @param boolean $debug 
  264.      * 
  265.      * @return string 
  266.      *  
  267.      */
  268.     function generateMyCacheKey($debug=FALSE)
  269.     {
  270.         global $OCSP_OBJ;
  271.         
  272.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_FRMCONTR_CACHE::generateMyHash()");
  273.         
  274.         $str_hashVals $this->getCacheType();
  275.         $str_hashVals.= "_::_".$this->myForm->getId();
  276.         $str_hashVals.= "_::_".serialize($this->myFilter);
  277.         $str_hashVals.= "_::_".serialize($this->lstTables);
  278.         $str_hashVals.= "_::_".$this->lst_orderBy;
  279.         $str_hashVals.= "_::_".$this->lst_groupValues;
  280.         $str_hashVals.= "_::_".$OCSP_OBJ['USER']->getId();
  281.         $str_hashVals.= "_::_".$OCSP_OBJ['USER']->getGroups();
  282.         
  283.         if ($debugechoDebugLine(__FILE__,__LINE__,"hash value string: ".$str_hashVals);
  284.         
  285.         $this->myCacheKey=md5($str_hashVals);
  286.         
  287.         return $this->myCacheKey;
  288.     }
  289.     
  290.     /**
  291.      * returns the hash key to identify the cache
  292.      *
  293.      * @return string 
  294.      */
  295.     function getMyCacheKey()
  296.     {
  297.         return $this->myCacheKey;
  298.     }
  299.     
  300.     /**
  301.      * set the hash key to identify the cache
  302.      *
  303.      * @param string $aHashKey 
  304.      */
  305.     function setMyCacheKey($aHashKey)
  306.     {
  307.         $this->myCacheKey=$aHashKey;
  308.     }
  309.     
  310.     //---------------------------------------------------
  311.     // cache methods
  312.     //---------------------------------------------------
  313.     
  314.     /**
  315.      * caches the description
  316.      *
  317.      * @abstract
  318.      * 
  319.      */
  320.     public function saveCacheDesc()
  321.     {
  322.         
  323.     }
  324.     
  325.     /**
  326.      * returns a desc array if cached
  327.      * 
  328.      * @param string $cacheKey 
  329.      * 
  330.      * @return array 
  331.      * 
  332.      * @abstract
  333.      */
  334.     public function getCacheDesc($cacheKey=NULL)
  335.     {
  336.         return NULL;
  337.     }
  338.     
  339.     public function setCacheDesc($aDescArr)
  340.     {
  341.         $this->setListQuery($aDescArr['LISTQUERY']);
  342.         $this->setFilter($aDescArr['FORMFILTER']);
  343.         $this->setOrderBy($aDescArr['FORMORDERBY']);
  344.         $this->lst_groupValues=$aDescArr['FORMGROUPVALS'];
  345.         $this->fillTime=$aDescArr['CACHEFILLTIME'];
  346.     }
  347.     
  348.     /**
  349.      * returns the size of the cache array
  350.      * 
  351.      * @return int 
  352.      */
  353.     public function getNbrOfCachedRows()
  354.     {
  355.         return sizeof($this->myCacheRows);
  356.     }
  357.     
  358.     /**
  359.      * returns if the cache is valid and not outtimed
  360.      *
  361.      * @return boolean 
  362.      * 
  363.      * @abstract
  364.      */
  365.     public function cacheIsValid(
  366.     {
  367.         return FALSE;
  368.     }
  369.  
  370.     
  371.     /**
  372.      * return of the cache has been filled from the storage ($this->fillTime)
  373.      *
  374.      * @return boolean 
  375.      * 
  376.      */
  377.     public function cacheIsFilled()
  378.     {
  379.         if ($this->cacheIsValied(&& intval($this->fillTime))
  380.         {
  381.             return TRUE;
  382.         else {
  383.             return FALSE;
  384.         }
  385.     }
  386.     
  387.     /**
  388.      * loads the cached values into $this->myCachedRows
  389.      * or filles the cache if this has not happend
  390.      *
  391.      * @param boolean $debug 
  392.      * 
  393.      * @return boolean 
  394.      *
  395.      * @abstract
  396.      */
  397.     public function getCachedValues($debug=FALSE)
  398.     {
  399.         return FALSE;
  400.     }
  401.     
  402.     /**
  403.      * loads the values from the storage into $this->myCachedRows
  404.      * and cache $this->myCachedRows
  405.      *
  406.      * @param boolan $debug 
  407.      * 
  408.      * @return boolean 
  409.      * 
  410.      * @abstract
  411.      */
  412.     public function fillCache($debug=FALSE)
  413.     {
  414.         return FALSE;
  415.     }
  416.     
  417.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  418.     // list db methods
  419.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  420.     
  421.     
  422.     
  423.     function loadListValues($debug=FALSE)
  424.     {
  425.         if ($this->getCachedValues())
  426.         {
  427.             $int_maxCount intval($this->getLimit());
  428.             if (intval($int_maxCount))
  429.             {
  430.                 $i                  $this->getOffset();                   
  431.                 $int_count          0;
  432.                 $this->myListValues = array();
  433.                 
  434.                 while(isset($this->myCacheRows[$i]&& ($int_count $int_maxCount))
  435.                 {
  436.                     $this->myListValues[$int_count++$this->myCacheRows[$i++];
  437.                 }
  438.                 return $int_count;
  439.             else {
  440.                 $this->myListValues = $this->myCacheRows;
  441.             }
  442.             
  443.             $this->myListIdx=0;
  444.             $this->listLoadTS=time();
  445.                         
  446.             return sizeof($this->myListValues);
  447.         else {
  448.             if ($debugecho "alert('could not get chached values');";
  449.             return FALSE;
  450.         }
  451.     }
  452.     
  453.     
  454. }
  455. ?>

Documentation generated on Thu, 08 Jan 2009 17:46:13 +0100 by phpDocumentor 1.4.0a2