mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-10-31 04:11:37 +01:00 
			
		
		
		
	
		
			
	
	
		
			158 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
		
		
			
		
	
	
			158 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
|   | <?xml version="1.0" encoding="iso-8859-1"?> | |||
|  | <!-- $Revision$ --> | |||
|  |   <sect1 id="section.template.cache.handler.func"> | |||
|  |    <title>Funzione di gestione della Cache</title> | |||
|  |    <para> | |||
|  |     Come alternativa all'uso del meccanismo di default per la cache basato | |||
|  |     sui file, potete specificare una funzione personalizzata di gestione | |||
|  |     che verr<72> usata per leggere, scrivere ed eliminare i file in cache. | |||
|  |    </para> | |||
|  |    <para> | |||
|  |     Create una funzione nella vostra applicazione che Smarty user<65> come | |||
|  |     gestore della cache. Impostate il nome di questa funzione nella variabile | |||
|  |     di classe <link linkend="variable.cache.handler.func">$cache_handler_func</link>. | |||
|  |     Smarty ora user<65> questa funzione per gestire i dati della cache. Il primo | |||
|  |     parametro <20> l'azione, che pu<70> essere 'read', 'write' o 'clear'. Il | |||
|  |     secondo parametro <20> l'oggetto Smarty. Il terzo parametro <20> il contenuto in | |||
|  |     cache. In una 'write', Smarty passa il contenuto da mettere in cache in | |||
|  |     questo parametro. In una 'read', Smarty si aspetta che la funzione prenda questo | |||
|  |     parametro per riferimento e che lo riempia con i dati della cache. | |||
|  |     In una 'clear', il parametro non viene usato, quindi passate una variabile | |||
|  |     dummy. Il quarto parametro <20> il nome del file del template (necessario | |||
|  |     per le read e le write), il quinto parametro <20> il cache_id (opzionale), e  | |||
|  |     il sesto <20> il compile_id (opzionale). | |||
|  |    </para> | |||
|  |    <para> | |||
|  |     Nota: l'ultimo parametro ($exp_time) <20> stato aggiunto in Smarty-2.6.0. | |||
|  |    </para> | |||
|  |    <example> | |||
|  |     <title>esempio con l'uso di MySQL per la cache</title> | |||
|  |     <programlisting role="php"> | |||
|  | <![CDATA[ | |||
|  | <?php | |||
|  | /* | |||
|  | 
 | |||
|  | esempio: | |||
|  | 
 | |||
|  | include('Smarty.class.php'); | |||
|  | include('mysql_cache_handler.php'); | |||
|  | 
 | |||
|  | $smarty = new Smarty; | |||
|  | $smarty->cache_handler_func = 'mysql_cache_handler'; | |||
|  | 
 | |||
|  | $smarty->display('index.tpl'); | |||
|  | 
 | |||
|  | 
 | |||
|  | il database mysql avr<76> questo formato: | |||
|  | 	 | |||
|  | create database SMARTY_CACHE; | |||
|  | 
 | |||
|  | create table CACHE_PAGES( | |||
|  | CacheID char(32) PRIMARY KEY, | |||
|  | CacheContents MEDIUMTEXT NOT NULL | |||
|  | ); | |||
|  | 
 | |||
|  | */ | |||
|  | 
 | |||
|  | function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) | |||
|  | { | |||
|  | 	// impostiamo i dati d'accesso al db | |||
|  | 	$db_host = 'localhost'; | |||
|  | 	$db_user = 'myuser'; | |||
|  | 	$db_pass = 'mypass'; | |||
|  | 	$db_name = 'SMARTY_CACHE'; | |||
|  | 	$use_gzip = false; | |||
|  | 	 | |||
|  | 	// creiamo un cache id unico | |||
|  | 	$CacheID = md5($tpl_file.$cache_id.$compile_id); | |||
|  | 	 | |||
|  | 	if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) { | |||
|  | 		$smarty_obj->_trigger_error_msg("cache_handler: could not connect to database"); | |||
|  | 		return false; | |||
|  | 	} | |||
|  | 	mysql_select_db($db_name); | |||
|  | 
 | |||
|  | 	switch ($action) { | |||
|  | 		case 'read': | |||
|  | 			// leggiamo la cache dal database | |||
|  | 			$results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'"); | |||
|  | 			if(!$results) { | |||
|  | 				$smarty_obj->_trigger_error_msg("cache_handler: query failed.");			 | |||
|  | 			} | |||
|  | 			$row = mysql_fetch_array($results,MYSQL_ASSOC); | |||
|  | 			 | |||
|  | 			if($use_gzip && function_exists("gzuncompress")) { | |||
|  | 				$cache_content = gzuncompress($row["CacheContents"]); | |||
|  | 			} else { | |||
|  | 				$cache_content = $row["CacheContents"]; | |||
|  | 			} | |||
|  | 			$return = $results; | |||
|  | 			break; | |||
|  | 		case 'write': | |||
|  | 			// salviamo la cache sul database | |||
|  | 			 | |||
|  | 			if($use_gzip && function_exists("gzcompress")) { | |||
|  | 				// compress the contents for storage efficiency | |||
|  | 				$contents = gzcompress($cache_content); | |||
|  | 			} else { | |||
|  | 				$contents = $cache_content; | |||
|  | 			} | |||
|  | 			$results = mysql_query("replace into CACHE_PAGES values( | |||
|  | 							'$CacheID', | |||
|  | 							'".addslashes($contents)."') | |||
|  | 						"); | |||
|  | 			if(!$results) { | |||
|  | 				$smarty_obj->_trigger_error_msg("cache_handler: query failed.");			 | |||
|  | 			} | |||
|  | 			$return = $results; | |||
|  | 			break; | |||
|  | 		case 'clear': | |||
|  | 			// eliminiamo i dati in cache | |||
|  | 			if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { | |||
|  | 				// eliminiamo tutto | |||
|  | 				$results = mysql_query("delete from CACHE_PAGES");			 | |||
|  | 			} else { | |||
|  | 				$results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");			 | |||
|  | 			} | |||
|  | 			if(!$results) { | |||
|  | 				$smarty_obj->_trigger_error_msg("cache_handler: query failed.");			 | |||
|  | 			} | |||
|  | 			$return = $results; | |||
|  | 			break; | |||
|  | 		default: | |||
|  | 			// errore, azione non prevista | |||
|  | 			$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); | |||
|  | 			$return = false; | |||
|  | 			break; | |||
|  | 	} | |||
|  | 	mysql_close($link); | |||
|  | 	return $return; | |||
|  | 	 | |||
|  | } | |||
|  | 
 | |||
|  | ?> | |||
|  | ]]> | |||
|  | </programlisting> | |||
|  |     </example> | |||
|  | </sect1> | |||
|  | <!-- Keep this comment at the end of the file
 | |||
|  | Local variables: | |||
|  | mode: sgml | |||
|  | sgml-omittag:t | |||
|  | sgml-shorttag:t | |||
|  | sgml-minimize-attributes:nil | |||
|  | sgml-always-quote-attributes:t | |||
|  | sgml-indent-step:1 | |||
|  | sgml-indent-data:t | |||
|  | indent-tabs-mode:nil | |||
|  | sgml-parent-document:nil | |||
|  | sgml-default-dtd-file:"../../../../manual.ced" | |||
|  | sgml-exposed-tags:nil | |||
|  | sgml-local-catalogs:nil | |||
|  | sgml-local-ecat-files:nil | |||
|  | End: | |||
|  | vim600: syn=xml fen fdm=syntax fdl=2 si | |||
|  | vim: et tw=78 syn=sgml | |||
|  | vi: ts=1 sw=1 | |||
|  | --> |