| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <?xml version="1.0" encoding="iso-8859-1"?> | 
					
						
							|  |  |  | <!-- $Revision$ --> | 
					
						
							| 
									
										
										
										
											2004-07-16 14:32:31 +00:00
										 |  |  | <!-- EN-Revision: 1.2 Maintainer: andreas Status: ready --> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |   <sect1 id="section.template.cache.handler.func"> | 
					
						
							|  |  |  |    <title>Cache Handler Funktion</title> | 
					
						
							|  |  |  |    <para> | 
					
						
							|  |  |  |     Als Alternative zum normalen dateibasierten Caching-Mechanismus können Sie | 
					
						
							|  |  |  |     eine eigene Cache-Handler Funktion zum lesen, schreiben und löschen von | 
					
						
							|  |  |  |     Cache-Dateien definieren. | 
					
						
							|  |  |  |    </para> | 
					
						
							|  |  |  |    <para> | 
					
						
							|  |  |  |     Schreiben Sie eine Funktion in Ihrer Applikation, die Smarty als | 
					
						
							|  |  |  |     Cache-Handler verwenden soll und weisen Sie deren Name der Variable | 
					
						
							|  |  |  |     <link linkend="variable.cache.handler.func">$cache_handler_func</link> zu. | 
					
						
							|  |  |  |      Smarty wird von da an Ihre Funktion zur Bearbeitung des Caches verwenden. | 
					
						
							|  |  |  |      Als erster Parameter wird die 'action' mit einem der folgendende Werte | 
					
						
							|  |  |  |      übergeben: 'read', 'write' und 'clear'. Als zweiter Parameter | 
					
						
							|  |  |  |      wird das Smarty-Objekt übergeben, als dritter der gecachte Inhalt. Bei einem | 
					
						
							|  |  |  |      'write' übergibt Smarty den gecachten Inhalt, bei 'read' übergibt Smarty die | 
					
						
							|  |  |  |      Variable als Referenz und erwartet, dass Ihre Funktion die Inhalte zuweist. | 
					
						
							|  |  |  |      Bei 'clear' können Sie eine dummy-Variable übergeben. Als vierter Parameter | 
					
						
							|  |  |  |      wird der Template-Name übergeben (verwendet bei 'write'/'read'), als fünfter | 
					
						
							|  |  |  |      Parameter die 'cache_id' (optional) und als sechster die 'compile_id' (auch optional). | 
					
						
							|  |  |  |    </para> | 
					
						
							|  |  |  |    <example> | 
					
						
							|  |  |  |     <title>Beispiel mit einer MySQL Datenbank als Datenquelle</title> | 
					
						
							|  |  |  |     <programlisting> | 
					
						
							| 
									
										
										
										
											2004-07-16 14:32:31 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |      Beispiel Anwendung: | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      include('Smarty.class.php'); | 
					
						
							|  |  |  |      include('mysql_cache_handler.php'); | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      $smarty = new Smarty; | 
					
						
							|  |  |  |      $smarty->cache_handler_func = 'mysql_cache_handler'; | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      $smarty->display('index.tpl'); | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      die Datenbank hat folgendes Format: | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      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) | 
					
						
							|  |  |  |      { | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // Datenbank Host, Benutzer und Passwort festlegen | 
					
						
							|  |  |  |      $db_host = 'localhost'; | 
					
						
							|  |  |  |      $db_user = 'myuser'; | 
					
						
							|  |  |  |      $db_pass = 'mypass'; | 
					
						
							|  |  |  |      $db_name = 'SMARTY_CACHE'; | 
					
						
							|  |  |  |      $use_gzip = false; | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // enmalige 'cache_id' erzeugen | 
					
						
							|  |  |  |      $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': | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // Cache aus der Datenbank lesen | 
					
						
							|  |  |  |      $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_contents = gzuncompress($row["CacheContents"]); | 
					
						
							|  |  |  |      } else { | 
					
						
							|  |  |  |      $cache_contents = $row["CacheContents"]; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |      $return = $results; | 
					
						
							|  |  |  |      break; | 
					
						
							|  |  |  |      case 'write': | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // Cache in Datenbank speichern | 
					
						
							|  |  |  |      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': | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // Cache Informationen löschen | 
					
						
							|  |  |  |      if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // alle löschen | 
					
						
							|  |  |  |      $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: | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      // Fehler, unbekannte 'action' | 
					
						
							|  |  |  |      $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); | 
					
						
							|  |  |  |      $return = false; | 
					
						
							|  |  |  |      break; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |      mysql_close($link); | 
					
						
							|  |  |  |      return $return; | 
					
						
							|  |  |  |       | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |       | 
					
						
							| 
									
										
										
										
											2004-07-16 14:32:31 +00:00
										 |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  | </programlisting> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |    </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 | 
					
						
							| 
									
										
										
										
											2004-07-16 14:32:31 +00:00
										 |  |  | --> |