Działa w oparciu o bazowaną na XML prezentację zmiennych.
Funkcje:<br>
* zapis-odczyt nast. tpyów danych: array (wielowymiarowe również), string, float, boolean, integer
* obsługa plików jak i możliwość działania bez ich uzycia (stringi XML, do dopracowania (działa tylko odczyt stringów))
* Usługa czasowej kontroli ważności cache'u (podajesz przez jaki czas cache jest ważny i potem zmienna isValid przyjmuje odpowiednią wartość)
TODO: przerobienie niektórych metod tak aby pasowały do modelu MVC (w szczególności metody save() )
Póki co klasa działa ok, jeżeli chodzi o wykorzystanie klasy z użyciem wbudowanej obsługi plików.
Kod:
<?php /** * Cache management API * Used for storing variables * * @author Artek Wiśniewski <aart3k@gmail.com> * @version $Id: cache.class.php,v 0.7.5 2006/04/12 22:31:48 aart3k Exp $ * @package nsAPI * @todo Cache rebuilds, MVC compatibility */ class Cache{ /** * Stores the reference to SimpleXMLElement * * @var object */ private $XMLElement; /** * XML-formatted variables (obtained/current) * * @var string */ public $XML; /** * Stores the cache-file path * * @var string */ public $file; /** * Array that contains obtained grom cache variables * * @var array */ public $variables; /** * True if cache is still valid * * @var bool */ public $isValid; /** * Number of seconds for which cache is valid * * @var int */ public $expires; /** * Loader of Cache * Loads stored cache (if there is one) and initializes parsing * * @param string $name * @param int $expires * @param string $validType days|hours|minutes|seconds (default) * @param string $XMLdata Ready XML-ed variables (this var has higher prority) */ public function __construct($name,$expires,$expiresUnit = NULL,$XMLdata = NULL){ $this->expires = $expires; if($validType == 'minutes') $this->expires = 60 * $this->expires; if($validType == 'hours') $this->expires = 3600 * $this->expires; if($validType == 'days') $this->expires = 86400 * $this->expires; /* Get saved cache if exists or use gived XML-data in param */ if($XMLdata) $this->XML = $XMLdata; else { file_put_contents($this->file,'<?php $cache=<<<XML <?xml version="1.0"?> <nscache> <validto type="unix">0</validto> <name>'.$name.'</name> <rebuilt file="" function="" /> <variables> <empty /> </variables> </nscache> XML; ?>');// REBUILTS for implement! } include($this->file); $this->XML = $cache; } $this->refresh(); } /** * Gets the current value of $this->XML variable and parses its data to variables * Use when XML var was changed externally * */ public function refresh(){ $this->XMLElement = new SimpleXMLElement($this->XML); foreach ($this->XMLElement->variables->variable as $variable) $this->variables[(string) $variable['name']] = $this->parseVariableXML($variable); $this->isValid = true; else $this->isValid = false; } /** * Saves last obtained / refreshed / putted vars * with new uniqid and time() signature * */ public function save(){ foreach ($this->variables as $name => $value) $dump .= $this->parseVariable($name,$value); '.$dump.'</variables>',$this->XML); $this->XMLElement = new SimpleXMLElement($this->XML); file_put_contents($this->file,'<?php $cache=<<<XML '.$this->XMLElement->asXML().' XML; ?>'); $this->isValid = true; } /** * Stores the variable in current cache (does not save the file) * * @param string $var1Name * @param mixed $var1Value * @param string $var2Name * @param mixed $var2Value * ... * @param string $varnName * @param mixed $varnValue */ public function store(){ $dump = ''; if($key % 2 != 0) $this->variables[$name] = $value; else $name = $value; } } /** * Returns array with last obtained variables * * @return array */ public function getCachedVars(){ return $this->variables; } /** * Used to extract variable from XML-obtained data * * @param XSimpleXMLElement Object $variable * @return mixed */ private function parseVariableXML($variable){ switch ((string) $variable['type']) { case 'array': foreach ($variable->children() as $children){ $parsedVar[(string) $children['name']] = $this->parseVariableXML($children); } break; case 'string': $parsedVar = utf8_decode($variable); break; case 'boolean': $parsedVar = (bool) (string) $variable; break; case 'float' OR 'double' OR 'single': break; case 'integer': $parsedVar = (int) (string) $variable; break; default: $parsedVar = utf8_decode($variable); break; } return $parsedVar; } /** * Makes the variable XML-dump (not complete XML, only this var tag) * * @param string $name Name of variable * @param mixed $value Variable itself * @return string Dump */ private function parseVariable($name,$value){ if($type == 'array'){ $arrayDump = '<variable name="'.$name.'" type="array"> '; foreach ($value as $key => $var) $arrayDump .= $this->parseVariable($key,$var); $arrayDump .= '</variable> '; }else $arrayDump = utf8_encode('<variable name="'.$name.'" type="'.$type.'"><![CDATA['.(string) $value.']]></variable>'); return $arrayDump; } } ?>
Przykłady będą oparte o nast. zmienne:
<?php $putFloat = 4124.3242355; $putString = "aktualśśśśny</a></b>"; $putBoolean = false; $putInteger = 3254235; $putArray[0] = "element zerowy"; $putArray[1][0] = "element 1 :: podelement 0"; $putArray[1][1] = 2144321.213421; ?>
Na końcu każdego dam var_dump() dla pokazania że zadziałało

Dobrze jest wykonywać przykłady po kolei...
Przykład 1: Zapis
<?php $new = new Cache('testCache',50); $new->variables['putFloat'] = $putFloat; $new->variables['putString'] = $putString; $new->variables['putBoolean'] = $putBoolean; $new->variables['putArray'] = $putArray; $new->save(); ?>
Przykład 2: Odczyt
<?php $new = new Cache('testCache',50); /* $new->variables['putFloat'] = $putFloat; $new->variables['putString'] = $putString; $new->variables['putBoolean'] = $putBoolean; $new->variables['putArray'] = $putArray; $new->save(); */ ?>
A że jest już późno jutro napiszę pozostałe przykłady...