phpDocumentor general
[ class tree: general ] [ index: general ] [ all elements ]

Source for file c_cache.inc

Documentation is available at c_cache.inc

  1. <?php
  2. /**
  3.  * Classe di gestione cache di metadati, pagine, xsl e dati
  4.  * Classe di generazione istanza unica (factory pattern)
  5.  *  
  6.  * @package general
  7.  * @author Ubik <emiliano.leporati@gmail.com>
  8.  */
  9.  
  10.  
  11. /**
  12.  * Gestisce le cache di metadati, pagine, xsl e dati su sdb
  13.  *  
  14.  * @package general
  15.  */
  16. class CACHE 
  17. {
  18.     /**
  19.      * Conteggi per le statistiche di esecuzione
  20.      * @var array 
  21.      * @static
  22.      */
  23.     public static $time array('CACHE' => 0);
  24.  
  25.  
  26.     /**
  27.      * Imposta flag e directory
  28.      */
  29.     public function __construct()
  30.     {
  31.         $this->mtd_cache = constant_true('CACHE_METADATI');
  32.         $this->mtd_dir = path(PHDIR'system/cache/metadata');
  33.  
  34.         $this->db_cache = constant_true('CACHE_DB');
  35.         $this->db_dir = path(PHDIR'system/cache/query');
  36.  
  37.         $this->reg_file = path(PHDIR,'system/cache/registry.ser');
  38.         $this->reg_lock = path(PHDIR,'system/cache/cache.lock');
  39.  
  40.         $this->xsl_cache = constant_true('CACHE_XSL');
  41.  
  42.         $this->page_cache = constant_true('CACHE_XML');
  43.         $this->page_dir = path(PHDIR'system/cache/xml');
  44.  
  45.         $this->lang_src_dir path(PHDIR'system/lang');
  46.         $this->lang_dir path(PHDIR'system/cache/lang');
  47.     }
  48.  
  49.  
  50.     /**
  51.      * Cache in memoria delle stringhe di localizzazione
  52.      * @var array 
  53.      */
  54.     private $lang_data = array();
  55.  
  56.     public function lang_js()
  57.     {
  58.         if (!isset($_SESSION['lang']))
  59.             throw new CodeException("Richiesta localizzazione ma lingua non impostata.");
  60.  
  61.         $args func_get_args();
  62.         if (!count($args)) {
  63.             trigger_error('Lang chiamata senza parametri'E_USER_WARNING);
  64.             return '';
  65.         }
  66.  
  67.         $id array_shift($args);
  68.  
  69.         $source path($this->lang_src_dir$_SESSION['lang'].'.xml');
  70.         $cache  path($this->lang_dir$_SESSION['lang'].'.xml.obj');
  71.  
  72.         if (!file_exists($cache|| filemtime($sourcefilemtime($cache)) {
  73.  
  74.             $d new DOMDocument();
  75.             if (!$d -> load($source))
  76.                 throw new CodeException("File di localizzazione $source non valido (XML).");
  77.  
  78.             $xpath new DOMXPath($d);
  79.             $root $d->documentElement;
  80.  
  81.             $this->lang_data['LC_TIME'$root->getAttribute('lc-time');
  82.             $this->lang_data['DATE_TYPE'$root->getAttribute('date-type');
  83.  
  84.             $nodes $xpath->query("//application/string[@id='$id']");
  85.             if ($nodes.length == 0)
  86.                 throw new CodeException(lang('LANG-STRING-MISSING'$id));
  87.  
  88.             $string $nodes->item(0)->textContent;
  89.         else {
  90.             $strings unserialize(file_get_contents($cache));
  91.             $string  $strings[$id];
  92.         }
  93.  
  94.         if (count($args)) {
  95.             array_unshift($args$string);
  96.             $string call_user_func_array('sprintf'$args);
  97.         }
  98.  
  99.         return $string;
  100.     }
  101.  
  102.     /**
  103.      * Carica la cache delle stringhe
  104.      * @param bool $force 
  105.      */
  106.     public function lang_load($force)
  107.     {
  108.         if (!isset($_SESSION['lang']))
  109.             throw new CodeException("Richiesta localizzazione ma lingua non impostata.");
  110.  
  111.         // se voglio un refresh o non ho caricato ancora nulla
  112.         if ($force || !isset($_SESSION['lang_data'])) {
  113.  
  114.             $source path($this->lang_src_dir$_SESSION['lang'].'.xml');
  115.             $cache  path($this->lang_dir$_SESSION['lang'].'.xml.obj');
  116.  
  117.             if (!file_exists($source))
  118.                 throw new CodeException("File di localizzazione $source non trovato.");
  119.  
  120.             // se non esiste il file di cache oppure il file source รจ + recente, carico il source
  121.             if ($force || !file_exists($cache|| filemtime($sourcefilemtime($cache)) {
  122.                 $this->lang_data = array();
  123.  
  124.                 $d new DOMDocument();
  125.                 if (!$d -> load($source))
  126.                     throw new CodeException("File di localizzazione $source non valido (XML).");
  127.  
  128.                 $xpath new DOMXPath($d);
  129.                 $root $d->documentElement;
  130.  
  131.                 $this->lang_data['LC_TIME'$root->getAttribute('lc-time');
  132.                 $this->lang_data['DATE_TYPE'$root->getAttribute('date-type');
  133.  
  134.                 $nodes $xpath->query('//application/string|//system/string')//$root->getElementsByTagName('string');
  135.                 for($i 0$i $nodes->length$i ++{
  136.                     $key $nodes->item($i)->getAttribute('id');
  137.                     $val $nodes->item($i)->textContent;
  138.  
  139.                     $this->lang_data[$key$val;
  140.                 }
  141.  
  142.                 $dbmss array('FBIRD' => 'firebird''MYSQL' => 'mysql''MSSQL' => 'mssql''ODBC' => 'odbc');
  143.                 foreach($dbmss as $ubk => $dbms{
  144.                     $this->lang_data[$ubkarray();
  145.                     $nodes $xpath->query("//{$dbms}/string");
  146.                     for($i 0$i $nodes->length$i ++{
  147.                         $key $nodes->item($i)->getAttribute('id');
  148.                         $val $nodes->item($i)->textContent;
  149.                         $val html_entity_decode($valENT_QUOTES'UTF-8');
  150.     
  151.                         $this->lang_data[$ubk][$key$val;
  152.                     }
  153.                 }
  154.                 // scrivo la cache
  155.                 scrivi_file($cacheserialize($this->lang_data));
  156.             // altrimenti carico la cache
  157.             else {
  158.                 $this->lang_data = unserialize(file_get_contents($cache));
  159.             }
  160.  
  161.             $_SESSION['lang_data'$this->lang_data;
  162.         }
  163.         if (!isset($this->lang_data))
  164.             $this->lang_data = $_SESSION['lang_data'];
  165.     }
  166.  
  167.     /**
  168.      * Ritorna la stringa indicata
  169.      * @param string $id 
  170.      * @param mixed ... Parametri per stringhe parametriche (sprintf)
  171.      * @return string 
  172.      */
  173.     public function lang()
  174.     {
  175.         $args func_get_args();
  176.         if (!count($args)) {
  177.             trigger_error('Lang chiamata senza parametri'E_USER_WARNING);
  178.             return '';
  179.         }
  180.  
  181.         $id array_shift($args);
  182.  
  183.         if (!isset($this->lang_data[$id]))
  184.             $this->lang_load(TRUE);
  185.         if (!isset($this->lang_data[$id]))
  186.             throw new CodeException(lang('LANG-STRING-MISSING'$id));
  187.  
  188.         $string $this->lang_data[$id];
  189.  
  190.         if (count($args)) {
  191.             array_unshift($args$string);
  192.             $string call_user_func_array('sprintf'$args);
  193.         }
  194.  
  195.         return ' '.$string.' ';
  196.     }
  197.  
  198.     /**
  199.      * Ritorna la stringa indicata
  200.      * @param string $db Database per cui si vuole ottenere la stringa (FBIRD, MYSQL, MSSQL, ODBC, ...)
  201.      * @param string $id Stringa voluta
  202.      * @return string 
  203.      */
  204.     public function lang_database($db$id)
  205.     {
  206.         if (!isset($this->lang_data[$db]|| !isset($this->lang_data[$db][$id]))
  207.             $this->lang_load(TRUE);
  208.  
  209.         if (!isset($this->lang_data[$db]))
  210.             throw new CodeException(lang('LANG-DBMS-MISSING'$db));
  211.         if (!isset($this->lang_data[$db][$id]))
  212.             throw new CodeException(lang('LANG-DBMS-STRING-MISSING'$db$id));
  213.  
  214.         return $this->lang_data[$db][$id];
  215.     }
  216.  
  217.     /**
  218.      * Array associativo con il codice trasformato di immagini con rollover (gfx:img)
  219.      * @deprecated
  220.      * @var array 
  221.      */
  222.     private $img_data = array();
  223.  
  224.     /**
  225.      * Ritorna il codice HTML rappresentante un'immagine con rollover dato il nome di base ($name => $name_dis.ext / $name.ext)
  226.      * Si basa sul template XSL gfx:img, che usa a sua volta il parametro ini DEF_IMG_EXT
  227.      * @deprecated
  228.      * @param string $name nome dell'immagine
  229.      * @return string 
  230.      */
  231.     public function img_get($name)
  232.     {
  233.         if (!isset($this->img_data[$name])) {
  234.             $this->img_data[$namexsl_transform("<gfx:img xmlns:gfx=\"".NS_GFX."\" nome=\"$name\"/>"true);
  235.         
  236.         return $this->img_data[$name];
  237.     }
  238.  
  239.  
  240.     /**
  241.      * Cache metadati attiva / disattiva
  242.      * Si basa sul parametro ini CACHE_METADATI
  243.      * @var bool 
  244.      */
  245.     private $mtd_cache = FALSE;
  246.     /**
  247.      * Directory in cui vengono memorizzati i file di cache delle meta-informazioni
  248.      * Sotto PHDIR, system/cache/metadata
  249.      * @var bool 
  250.      */
  251.     private $mtd_dir = '';
  252.     /**
  253.      * Cache in memoria delle meta-informazioni
  254.      * @var array 
  255.      */
  256.     private $mtd_data = array();
  257.  
  258.     /**
  259.      * Fornisce il nome file contenente le meta-informazioni
  260.      * @param string $signature Firma dell'oggetto di cui reperire le informazioni, fornita da {@link mtd_signature}
  261.      * @return string 
  262.      */
  263.     public function mtd_fname($signature)
  264.     {
  265.         return path($this->mtd_dir$signature.'.obj');
  266.     }
  267.  
  268.     /**
  269.      * Fornisce il nome file contenente le meta-informazioni
  270.      * @param array $db_connection Informazioni di connessione al db contenente l'oggetto
  271.      * @param string $sql Oggetto del db (tabella, vista, stored procedure)
  272.      * @return string 
  273.      */
  274.     public function mtd_signature($db_connection$sql)
  275.     {
  276.         if (($p stripos($sql,' where ')) !== FALSE{
  277.             $sql substr($sql0$p);
  278.         }
  279.         return CRYPTER::hash($db_connection['DB_SERVER'].$db_connection['DB_NAME'].$sql);
  280.     }
  281.  
  282.     /**
  283.      * Registra le meta-informazioni su file
  284.      * @param array $db_connection Informazioni di connessione al db contenente l'oggetto
  285.      * @param string $sql Oggetto del db (tabella, vista, stored procedure)
  286.      * @param array $info Meta-informazioni
  287.      */
  288.     public function mtd_write($db_connection$sql$info)
  289.     {
  290.         $sig $this->mtd_signature($db_connection$sql);
  291.         
  292.         $this->mtd_data[$sig$info;
  293.  
  294.         if ($this->mtd_cache{
  295.             scrivi_file($this->mtd_fname($sig)serialize($info));
  296.         }
  297.     }
  298.  
  299.     /**
  300.      * Ritorna le meta-informazioni associate all'oggetto, se esistono
  301.      * @param array $db_connection Informazioni di connessione al db contenente l'oggetto
  302.      * @param string $sql Oggetto del db (tabella, vista, stored procedure)
  303.      * @return array|NULL
  304.      */
  305.     public function mtd_get($db_connection$sql)
  306.     {
  307.         $sig $this->mtd_signature($db_connection$sql);
  308.  
  309.         if (isset($this->mtd_data[$sig])) {
  310.             return $this->mtd_data[$sig];
  311.  
  312.         elseif ($this->mtd_cache{
  313.             if (file_exists($this->mtd_fname($sig))) 
  314.                 $this->mtd_data[$sigunserialize(file_get_contents($this->mtd_fname($sig)));
  315.                 return $this->mtd_data[$sig];
  316.             }
  317.         
  318.         else {
  319.             return NULL;
  320.         }
  321.     }
  322.  
  323.     /**
  324.      * Cache pagine attiva / disattiva
  325.      * Si basa sul parametro ini CACHE_XML
  326.      * @var bool 
  327.      */
  328.     private $page_cache = FALSE;
  329.     /**
  330.      * Directory in cui vengono memorizzati i file di cache delle pagine
  331.      * Sotto PHDIR, system/cache/xml
  332.      * @var bool 
  333.      */
  334.     private $page_dir = '';
  335.  
  336.     /**
  337.      * Fornisce il nome file contenente la pagina pre-trasformata
  338.      * @param string $filename Nome del file da reperire, completo di percorso (relativo alla PHDIR)
  339.      * @return string 
  340.      */
  341.     public function page_name($filename)
  342.     {
  343.         return path($this->page_dirstr_replace('/','-',$filename).'.obj');
  344.     }
  345.  
  346.     /**
  347.      * Memorizza la pagina trasformata sul filesystem
  348.      * @param string $filename Nome del file trasformato
  349.      * @param TAG $DOM Trasformazione del file
  350.      */
  351.     public function page_write($filename&$DOM)
  352.     {
  353.         if ($this->page_cache{
  354.             scrivi_file($this->page_name($filename)serialize($DOM));
  355.         }
  356.     }
  357.  
  358.     /**
  359.      * Fornisce la trasformazione del file XML indicato
  360.      * @param string $filename Nome del file da reperire, completo di percorso (relativo alla PHDIR)
  361.      * @return TAG 
  362.      */
  363.     public function page_get($filename)
  364.     {
  365.         if ($this->page_cache && file_exists($cache $this->page_name($filename)) && filemtime($cachefilemtime($filename)) {
  366.             return unserialize(file_get_contents($cache));
  367.         else {
  368.             return NULL;
  369.         }
  370.  
  371.     }
  372.  
  373.     /**
  374.      * Attiva l'uso della cache se consentito dai parametri di configurazione
  375.      */
  376.     public function page_activate()
  377.     {
  378.         $this->page_cache = constant_true('CACHE_XML');
  379.     }
  380.  
  381.     /**
  382.      * Disattiva l'uso della cache
  383.      */
  384.     public function page_deactivate()
  385.     {
  386.         $this->page_cache = FALSE;
  387.     }
  388.  
  389.  
  390.     /**
  391.      * Cache trasformazioni XSL attiva / disattiva
  392.      * Si basa sul parametro ini CACHE_XSL
  393.      * @var bool 
  394.      */
  395.      private $xsl_cache = FALSE;
  396.  
  397.     /**
  398.      * Restituisce il file XSL, tenuto in sessione
  399.      * @return string 
  400.      */
  401.     public function xsl_get()
  402.     {
  403.         return array_get_default('CACHE_XSL'$_SESSION);
  404.     }
  405.  
  406.     /**
  407.      * Memorizza in sessione il file XSL
  408.      * @param string $xsl 
  409.      */
  410.     public function xsl_write($xsl)
  411.     {
  412.         if ($this->xsl_cache && !isset($_SESSION['CACHE_XSL']))
  413.             $_SESSION['CACHE_XSL'$xsl;
  414.     }
  415.  
  416.  
  417.     
  418.     /**
  419.      * Array associativo nome_tabella => true / false, indica se la cache e' da aggiornare
  420.      * @var array 
  421.      */
  422.     private $reg_update = array();
  423.     /**
  424.      * Array associativo nome_tabella => ultimo aggiornamento cache
  425.      * @var array 
  426.      */
  427.     private $reg_cache = array();
  428.     /**
  429.      * File con le informazioni della cache
  430.      * sotto PHDIR, system/cache/registry.ser
  431.      * @var string 
  432.      */
  433.     private $reg_file = '';
  434.     /**
  435.      * File di lock per accesso a {@link $reg_file}
  436.      * sotto PHDIR, system/cache/cache.lock
  437.      * @var string 
  438.      */
  439.     private $reg_lock = '';
  440.  
  441.     /**
  442.      * Indica se registrare la funzione di shutdown per la scrittura del registry
  443.      * @var bool 
  444.      */
  445.     private $reg_shutdown = FALSE;
  446.  
  447.     
  448.     /**
  449.      * Cache query attiva / disattiva
  450.      * Si basa sul parametro ini CACHE_DB
  451.      * @var bool 
  452.      */
  453.     private $db_cache = FALSE;
  454.     /**
  455.      * Cache dati query
  456.      * @var array 
  457.      */
  458.     private $db_data = array();
  459.     /**
  460.      * Directory salvataggio dati query
  461.      * sotto PHDIR, system/cache/query
  462.      * @var string 
  463.      */
  464.     private $db_dir = '';
  465.     /**
  466.      * Numero record per ogni file di query
  467.      * @var integer 
  468.      */
  469.     private $db_range = 30;
  470.     /**
  471.      * Cache in memoria dei dati caricati via query
  472.      * @var array 
  473.      */
  474.     private $db_sql = array();
  475.  
  476.     /**
  477.      * Invalida la cache di una tabella, normalmente viene chiamato dalle funzioni
  478.      * di aggiornamento (update, add, delete).
  479.      * 
  480.      * @param string $nome_tabella nome della tabella modificata
  481.      */
  482.     public function azzera($nome_tabella)
  483.     {
  484.         if (!$this->db_cachereturn false;
  485.  
  486.         $this->reg_cache[$nome_tabella0;
  487.         $this->reg_update[$nome_tabellatrue;
  488.         
  489.         // aggiorna il registro a chiusura della pagina
  490.         if (!$this->reg_shutdown{
  491.             $this->reg_shutdown = TRUE;
  492.             register_shutdown_function(array($this'scrivi_registry'));
  493.         }
  494.     }
  495.  
  496.  
  497.  
  498.     /**
  499.      * Carica i dati dalla cache di una tabella, se vecchia o non presente ricrea la cache.
  500.      * 
  501.      * @param string $nome_tabella nome della tabella
  502.      * @param string $campo_id nome del campo su cui viene fatta la ricerca
  503.      * @param string $valore_id valore cercato
  504.      * 
  505.      * @return array|NULLl'insieme dei record cercati
  506.      */
  507.     public function carica_rs($nome_tabella$campo_id$valore_id)
  508.     {
  509.         if (constant_true('LOG_STATS')) $_start get_microtime();
  510.         
  511.         if (!is_numeric($valore_id))
  512.             $valore_id substr($valore_id1-1);
  513.  
  514.         $file_name $this->nome_file($nome_tabella$campo_id$valore_id);
  515.  
  516.         // dati gia' caricati
  517.         if (isset($this->db_data[$file_name])){
  518.             $data =$this->db_data[$file_name];
  519.  
  520.         // dati da caricare
  521.         else 
  522.             $file_path path($this->db_dir,$file_name);
  523.             $file_mtime filemtime($file_path);
  524.             // carico il file da disco    
  525.             if ($file_mtime && isset($this->reg_cache[$nome_tabella]&& ($file_mtime >= $this->reg_cache[$nome_tabella])) {
  526.                 log_value("CACHE: tabella $nome_tabella ok (mtime $file_mtime ) <= ");
  527.                 $data unserialize(file_get_contents($file_path));
  528.             else {
  529.                 if (!$file_mtimelog_value("CACHE: carico da db $file_name non esistente");
  530.                 elseif (!isset($this->reg_cache[$nome_tabella])) log_value("CACHE: carico da db $file_name non presente in cache");
  531.                 elseif (!($file_mtime >= $this->reg_cache[$nome_tabella])) 
  532.                     log_value("CACHE: carico da db $file_name percha' piu' aggiornato $file_mtime > $this->reg_cache[$nome_tabella.
  533.                         ' - ' date("h:i:s d/m/Y",$file_mtime' > ' date("h:i:s d/m/Y",$this->reg_cache[$nome_tabella]));
  534.             }
  535.         }
  536.         
  537.         // file non presente o scaduto, devo crearlo
  538.         if (!isset($data)) {
  539.             if (is_numeric($valore_id)) {
  540.                 $min $valore_id  ($valore_id $this->db_range)
  541.                 $max $min $this->db_range;
  542.                 $cond "where ".c_and(gte($campo_id$min)lt($campo_id$max));
  543.             else {
  544.                 $cond "";
  545.             }
  546.             
  547.             $sql "select * from $nome_tabella $cond";
  548.             $tmp_rs new RECORDSET($sql);
  549.             $data $tmp_rs->getRS();
  550.             unset($tmp_rs);
  551.             
  552.             // aggiorno il file su disco
  553.             scrivi_file($file_path,serialize($data));
  554.             $this->db_data[$file_name&$data;
  555.             
  556.             if (!isset($this->reg_cache[$nome_tabella])) {
  557.                 if (!isset($this->reg_update[$nome_tabella])) {
  558.                     $this->reg_update[$nome_tabellatrue;
  559.                     log_value("CACHE: update [$nome_tabella]");
  560.                 }
  561.                     
  562.                 if    (!$this->reg_shutdown){
  563.                     $this->reg_shutdown = TRUE;
  564.                     register_shutdown_function(array($this'scrivi_registry'));
  565.                 }
  566.             }
  567.         }
  568.         
  569.         
  570.         $rs array();
  571.         if (isset($data[$valore_id])) {
  572.             log_value("CACHE: id richiesto $valore_id trovato");
  573.             $rs[$data[$valore_id];
  574.         }
  575.  
  576.         if (constant_true('LOG_STATS')) {
  577.             $_end get_microtime();
  578.             self::$time['CACHE'+= ($_end $_start);
  579.         }
  580.         
  581.         return $rs;
  582.     }
  583.  
  584.     /**
  585.      * Carica il registro della cache se necessario (check sull'ora di modifica)
  586.      * 
  587.      * @param bool $ignore_session ignora i dati di cache in sessione
  588.      * 
  589.      * @return array il registro delle della cache ([nome_tabella] => time_stamp)
  590.      */ 
  591.     public function carica_registry($ignore_session false)
  592.     {
  593.         $registry null;
  594.         if (!$this->db_cache)
  595.             return $registry;
  596.  
  597.         if ($ignore_session){
  598.             if (file_exists($this->reg_file)){
  599.                 $registry unserialize(file_get_contents($this->reg_file));    
  600.                 return $registry;
  601.             else {
  602.                 return $registry;    
  603.             }
  604.         else {
  605.             //echo "caricamento registry\n";
  606.             $cache_reg_mtime @filemtime($this->reg_file);
  607.             if (!$cache_reg_mtime){
  608.                 unset($_SESSION['REGISTRY_TIME']);
  609.                 unset($this->reg_cache)
  610.                 return $registry;
  611.             }
  612.             //echo "Registry del ".date("h:i:s d/m/Y",$cache_reg_mtime)."\n";
  613.             if ($cache_reg_mtime && !empty($_SESSION['REGISTRY_TIME'])){
  614.                 if ($cache_reg_mtime $_SESSION['REGISTRY_TIME']){
  615.                     // registro vecchio, ricaricare
  616.                     unset($this->reg_cache);     
  617.                     log_value('CACHE: Registro cache da aggiornare '$this->reg_file);
  618.                     log_value('CACHE: Registro session: '.date('d/m/Y h:i:s',$_SESSION['REGISTRY_TIME']).
  619.                             ' - ' date('d/m/Y h:i:s',$cache_reg_mtime.' mtime file');
  620.                 }
  621.             }    
  622.             
  623.             if (!isset($this->reg_cache)){
  624.                 if ($cache_reg_mtime == null){
  625.                     $this->reg_cache = array();
  626.                     log_value('CACHE: Nessun registro cache presente '$this->reg_file);
  627.                 else {
  628.                     $registry unserialize(file_get_contents($this->reg_file));
  629.                     if (!is_array($registry)){
  630.                         // errore nel caricamento del file
  631.                         $this->reg_cache = array();
  632.                         log_value('CACHE: Errore nel caricamento del registro cache '$this->reg_file);
  633.                     else {
  634.                         $this->reg_cache = &$registry;
  635.                         $_SESSION['REGISTRY_TIME'$cache_reg_mtime;
  636.                         log_value('CACHE: Registro cache caricato '$this->reg_file);
  637.                     }
  638.                 }
  639.             }
  640.             return $this->reg_cache;
  641.         
  642.     }
  643.  
  644.  
  645.     /**
  646.      * Aggiorna il registro della cache su disco
  647.      */ 
  648.     public function scrivi_registry()
  649.     {
  650.         log_value("CACHE: scrivendo il registry");
  651.         
  652.         
  653.         if !($fp fopen($this->reg_lock'w')) ) {
  654.             log_value("CACHE: Impossibile aprire il lock file ".$this->reg_lock);
  655.             throw new FatalException("Impossibile aprire il lock file ".$this->reg_lock);
  656.         }
  657.         if!(flock($fpLOCK_EX)) ) {
  658.             log_value("CACHE: Impossibile eseguire il lock sulla cache ".$this->reg_lock);
  659.             throw new FatalException("Impossibile eseguire il lock sulla cache");
  660.         }
  661.         
  662.         $registry $this->carica_registry(true);
  663.         
  664.         // da verificare l'aggiornamento della cache
  665.         
  666.         if (!$registry){
  667.             $registry array();    
  668.         }
  669.  
  670.         foreach($this->reg_update as $tabella => $value){
  671.             $registry[$tabellatime();
  672.             log_value('CACHE: Aggiornata tabella: ' $tabella ' ts: ($registry[$tabella]) ' date("h:i:s d/m/Y",$registry[$tabella]));
  673.         }
  674.         scrivi_file($this->reg_file,serialize($registry));
  675.         
  676.         if!(flock($fpLOCK_UN)) ){
  677.             throw new FatalException("Impossibile rilasciare il lock sulla cache");
  678.         }
  679.         fclose($fp);
  680.         
  681.     }
  682.  
  683.  
  684.     /**
  685.      * Restituisce il nome del file che deve contenere i dati.
  686.      * 
  687.      * @param string $nome_tabella nome della tabella
  688.      * @param string $campo_id nome del campo su cui viene fatta la ricerca
  689.      * @param string $valore_id valore cercato
  690.      * 
  691.      * @return string il nome del file
  692.      */ 
  693.     public function nome_file($nome_tabella$campo_id$valore_id)
  694.     {
  695.         return "{$nome_tabella}_{$campo_id}_(floor($valore_id $this->db_range1'.db';
  696.     }
  697.  
  698.  
  699.     /**
  700.      * Carica il recordset passato con i dati della cache locale
  701.      * 
  702.      * @param mixed $sql Query da effettuare
  703.      * @param bool $carica_info Indica se impostare i meta-dati del recordset
  704.      * @param RECORDSET $db_object Il {@link RECORDSET} / {@link GESTORE} da caricare
  705.      * 
  706.      * @return bool true se caricato, false se i dati non sono presenti
  707.      */ 
  708.     public function carica_sql($sql$carica_info&$db_object)
  709.     {
  710.         if (!$this->db_cachereturn false;
  711.  
  712.         if (is_array($sql)){
  713.             $sql $sql['SQL'$sql['PAGE'$sql['PAGE-SIZE'];
  714.         }    
  715.         if (!isset($this->db_sql[$sql])){
  716.             return false;
  717.         }
  718.         
  719.         $cache_rs $this->db_sql[$sql];
  720.         
  721.         $db_object->setRS($cache_rs->getRS());
  722.         
  723.         if ($carica_info){
  724.             $db_object->campi $cache_rs->campi;
  725.             $db_object->lunghezza $cache_rs->lunghezza;
  726.             $db_object->tipo $cache_rs->tipo;
  727.         }
  728.  
  729.         return true;
  730.     }
  731.  
  732.  
  733.     /**
  734.      * Dice se la cache e' valida
  735.      * 
  736.      * @param bool $usa_cache 
  737.      * @param string $table 
  738.      * @param string $campo_id 
  739.      * @param string $condizione 
  740.      * 
  741.      * @return array 
  742.      */ 
  743.     public function validate($usa_cache$table$campo_id$condizione)
  744.     {
  745.         if (!$this->db_cachereturn array(FALSENULL);
  746.  
  747.         if ($this->db_cache && $usa_cache && $table[0== 't'){
  748.             if (is_null($campo_id)) $campo_id preg_replace('/^(\w+)\.(\w+)$/','\2'$usa_cache);
  749.             if ($condizione && !(bool)preg_match('/^\s*'.$campo_id.'\s*=\s*(\d+)\s*$/'$condizione$_info_condizione)) {
  750.                 return array(FALSENULL);
  751.             
  752.             return array($usa_cache$_info_condizione[1]);
  753.         
  754.     }
  755.  
  756.  
  757.     /**
  758.      * Memorizza localmente la query effettuata
  759.      * 
  760.      * @param mixed $sql 
  761.      * @param RECORDSET $db_object 
  762.      * @return bool 
  763.      */ 
  764.     public function salva_sql($sql&$db_object)
  765.     {
  766.         if (!$this->db_cachereturn false;
  767.  
  768.         if (is_array($sql)){
  769.             $sql $sql['SQL'$sql['PAGE'$sql['PAGE-SIZE'];
  770.         }    
  771.  
  772.         $this->db_sql[$sql=$db_object;
  773.  
  774.         return true;
  775.     }
  776.  
  777. }
  778.  
  779.  
  780. /**
  781.  * Ritorna il gestore della cache, assicurandosi che non vi siano istanze multiple
  782.  * @package general
  783.  */
  784. {
  785.     private static $instance NULL;
  786.  
  787.     final public static function get()
  788.     {
  789.         if (is_null(self::$instance)) {
  790.  
  791.             self::$instance new CACHE();
  792.             self::$instance->carica_registry();
  793.  
  794.         }
  795.  
  796.         return self::$instance;
  797.     }
  798. }
  799.  
  800. /**
  801.  * Shortcut globale per CACHE::lang
  802.  */
  803. function lang()
  804. {
  805.     static $cache;
  806.     if (!isset($cache)) $cache CACHE_CLASS_FACTORY::get();
  807.     $args  func_get_args();
  808.  
  809.     return call_user_func_array(array($cache'lang')$args);
  810. }
  811.  
  812. /**
  813.  * Shortcut globale per CACHE::lang_database
  814.  */
  815. function lang_database()
  816. {
  817.     static $cache;
  818.     if (!isset($cache)) $cache CACHE_CLASS_FACTORY::get();
  819.     $args  func_get_args();
  820.  
  821.     return call_user_func_array(array($cache'lang_database')$args);
  822. }
  823.  
  824. /**
  825.  * Shortcut globale per CACHE::lang_load
  826.  */
  827. function lang_load($force)
  828. {
  829.     static $cache;
  830.     if (!isset($cache)) $cache CACHE_CLASS_FACTORY::get();
  831.  
  832.     return call_user_func(array($cache'lang_load')$force);
  833. }
  834.  
  835. function lang_js()
  836. {
  837.     $cache CACHE_CLASS_FACTORY::get();
  838.     $args  func_get_args();
  839.  
  840.     return call_user_func_array(array($cache'lang_js')$args);
  841. }
  842.  
  843. ?>

Documentation generated on Thu, 25 Sep 2008 23:29:04 +0200 by phpDocumentor 1.4.0