Napisałem taką klasę cache:
<?php
define ("CACHE_PATH", "cache/"); class Cache {
private $buffer;
private $mode;
private $file;
private $handle;
private $line = 0;
public function cache_start ($handle = 0, $type) {
$this -> handle = $handle;
if (file_exists(CACHE_PATH
.$type."_cache_".$this -> handle
)) { $this -> mode = 1;
}
else {
$this -> mode = 2;
$this -> file = CACHE_PATH
.$type."_cache_".$this -> handle
; $this -> buffer
= array (); }
}
else
$this -> mode = 0;
}
public function cache_get () {
if ($this -> mode == 1){
if ($this -> line
>= count ($this -> buffer
)) return false;
else {
return $this -> buffer [$this -> line];
$this -> line++;
}
}else
return false;
}
public function cache_set ($value) {
if ($value) {
if ($this -> mode == 2) {
$this -> buffer[] = $value;
}
return $value;
}
}
public function cache_mode () {
return $this -> mode;
}
public function cache_end () {
if($this -> mode == 2)
$this -> mode = 0;
$this -> line = 0;
}
}
?>
I testuje ja na zalążku sterownika bazy danych :
<?php
private $handle;
private $sql_query;
private $cache;
private $rows;
function __construct ($host, $login, $password, $db) {
$this -> cache = new Cache;
}
function &connect ($host = false, $login = false, $password = false, $db = false) {
if ($instance == false)
$instance = new mysql ($host, $login, $password, $db); return $instance;
}
function query ($sql, $handle = 0) {
$this -> cache -> cache_start ($handle, "sql");
if ($this -> cache -> cache_mode() !== 1) {
$this -> sql_query
= mysql_query ($sql, $this -> handle
); }
}
return true;
}
function num_rows () {
}
function real_escape_string ($str) {
}
function fetch_array () {
if ($this -> cache -> cache_mode() == 1) {
return $this -> cache -> cache_get();
}else {
}
}
function free_result () {
$this -> cache -> cache_end ();
}
function close () {
}
}
?>
Tak więc testuje:
TEST.php<?php
$db =& mysql::connect ("localhost", "root", "***", "***"); $db -> query ("SELECT * FROM users", "userzy");
while ($row =$db -> fetch_array ()) {
echo $row['username']." n"; }
$db -> free_result ();
?>
Gdy plik nieistnieje i wywoływana jest funkcja cache_set wszystko działa, ale jeżeli już dane pobierane są za pomocą cache_get już nie :/ (nieskończona pętla)
Jakieś pomysły (IMG:
http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:
http://forum.php.pl/style_emoticons/default/questionmark.gif)
Ten post edytował Levabul 8.08.2005, 12:04:07