Uzylem funkcji Zyxa do cachowania danych:
Sprawa wyglada tak:
Przy pierwszym zaladowaniu strony tworzy sie plik cache i jest wszystko w porzadku. Dane w srodku pliku sa i wygladaja na poprawne. odswiezam strone i ta przestaje sie ladowac w momencie pobierania pliku z katalogu 'sql_cache'. Na pewno nie jest to problem z dostepem do pliku czy katalogu bo jak zmienie nazwe katalogu lub zmneijsze prawa dostepu do utworzonego pliku cache to wyskakuje mi normlany blad z informacja o tym. A w tym przypadku nie wyskakuje mi anwet zaden blad a jedynie strona przestaje sie ladowac w meijscu gdzie jest scachowane zapytanie. Gdy wyrzuca plik, ktory zostal utworozny po pierwszym wywolaniu zapytania to znowu strona sie raz zaladuje,plik sie utowrzy i strona po zaladowaniu zatrzyma sie w meijscu cachowania zapytania. I takw kolko. Probowalem juz ronzych rzeczy pod php 5 i pod php 4 i lipa:( Bylbym nardzo wdzieczny za jakakolwiek pomoc.
Funckja wyglada tak:
<?php
define('CACHE_DIR', './sql_cache/');
class sql{
var $connection;
var $result;
var $rows;
var $queries = 0;
var $cache_state =0;
var $cache_file;
var $cache_buffer;
var $cache_ptr;
function sql_connect($host, $user, $pass, $db){
}
function sql_close(){
}
function sql_cache($handle = 0){
$this -> cache_state = 1;
$this -> cache_ptr = 0;
}else{
$this -> cache_state = 2;
$this -> cache_buffer
= array(); $this -> cache_file = CACHE_DIR.'xxx_'.$handle.'.666';
}
}else{
if($this -> cache_state == 2){
file_put_contents
($this -> cache_file
, serialize($this -> cache_buffer
)); }
$this -> cache_state = 0;
}
}
function sql_cache_remove($handle){
unlink(CACHE_DIR
.'xxx_'.$handle.'.666'); }
}
function sql_query($query){
if($this -> cache_state != 1){
$this -> queries++;
}
return 1;
}
}
function sql_fetch_array(){
if($this -> cache_state == 1){
if(!isset($this -> cache_buffer
[$this -> cache_ptr
])){ return 0;
}
$this -> rows = $this -> cache_buffer[$this -> cache_ptr];
$this -> cache_ptr++;
return 1;
}else{
if($this -> cache_state == 2){
// Dodaj do cache
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
}
function sql_fetch_row(){
if($this -> cache_state == 1){
// czy koniec bufora?
if(!isset($this -> cache_buffer
[$this -> cache_ptr
])){ return 0;
}
// odczytaj z bufora
$this -> rows = $this -> cache_buffer[$this -> cache_ptr];
$this -> cache_ptr++;
return 1;
}else{
if($this -> cache_state == 2){
// Jeśli tworzymy cache, musimy rekord dodatkowo zapisac w buforze
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
}
} // koniec klasy
?>
Tak wyglada scachowane zapytanie:
<?php
include("cache.php");
$sql = new sql;
$sql -> sql_connect('host', 'user', 'haslo', 'baza');
$sql -> sql_cache('uchwyt');
$sql -> sql_query
("SELECT name FROM tabela1 WHERE main = '1' ORDER BY id DESC LIMIT 6") or
die(mysql_error()); while($sql -> sql_fetch_row())
{
$name = $sql -> rows[0];
}
$sql -> sql_cache();
?>
Ten post edytował Bojakki 1.01.2007, 11:24:28