Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Własna klasa cache
Forum PHP.pl > Forum > PHP
Levabul
Napisałem taką klasę cache:
  1. <?php
  2. define ("CACHE_PATH", "cache/");
  3. class Cache {
  4. private $buffer;
  5. private $mode;
  6. private $file;
  7. private $handle;
  8. private $line = 0;
  9. public function cache_start ($handle = 0, $type) {
  10.  $this -> handle = $handle;
  11.  if(is_string($this -> handle)) {
  12.  if (file_exists(CACHE_PATH.$type."_cache_".$this -> handle)) {
  13.  $this -> mode = 1;
  14.  $this -> buffer = unserialize (file_get_contents(CACHE_PATH.$type."_cache_".$this -> handle));
  15. }
  16. else {
  17.  $this -> mode = 2;
  18.  $this -> file = CACHE_PATH.$type."_cache_".$this -> handle;
  19.  $this -> buffer = array ();
  20. }
  21.  }
  22.  else
  23.  $this -> mode = 0;  
  24. }
  25. public function cache_get () {
  26.  if ($this -> mode == 1){
  27.  if ($this -> line >= count ($this -> buffer))
  28.  return false;
  29.  else {
  30. return $this -> buffer [$this -> line];
  31. $this -> line++;
  32.  }
  33. }else
  34.  return false; 
  35. }
  36. public function cache_set ($value) {
  37.  if ($value) {
  38.  if ($this -> mode == 2) {
  39. $this -> buffer[] = $value;
  40. }
  41. return $value;
  42.  }  
  43. }
  44. public function cache_mode () {
  45.  return $this -> mode;
  46. }
  47. public function cache_end () {
  48.  if($this -> mode == 2)
  49. file_put_contents ($this -> file, serialize($this -> buffer));
  50.  $this -> mode = 0;
  51.  $this -> line = 0;
  52. }
  53. }
  54. ?>


I testuje ja na zalążku sterownika bazy danych :
  1. <?php
  2. class mysql {
  3. private $handle;
  4. private $sql_query;
  5. private $cache;
  6. private $rows;
  7. function __construct ($host, $login, $password, $db) {
  8.  $this -> handle = mysql_connect ($host, $login, $password);
  9.  mysql_select_db ($db, $this -> handle);
  10.  $this -> cache = new Cache;
  11. }
  12. function &connect ($host = false, $login = false, $password = false, $db = false) {
  13.  static $instance = false;
  14.  if ($instance == false)
  15.  $instance = new mysql ($host, $login, $password, $db);
  16.  return $instance;
  17. }
  18. function query ($sql, $handle = 0) {
  19.  $this -> cache -> cache_start ($handle, "sql");
  20.  if ($this -> cache -> cache_mode() !== 1) {
  21.  $this -> sql_query = mysql_query ($sql, $this -> handle);
  22.  if(mysql_errno() != 0){
  23.  die('Error: '.mysql_error().'<br/>');
  24.  }
  25.  }
  26.  return true;
  27. }
  28. function num_rows () {
  29.  return mysql_num_rows ($this -> sql_query);
  30. }
  31. function real_escape_string ($str) {
  32. }
  33. function fetch_array () {
  34.  if ($this -> cache -> cache_mode() == 1) {
  35. return $this -> cache -> cache_get();
  36.  }else {
  37.  return $this -> cache -> cache_set (mysql_fetch_assoc ($this -> sql_query));
  38.  }
  39. }
  40. function free_result () {
  41.  $this -> cache -> cache_end ();
  42.  mysql_free_result ($this -> sql_query);
  43. }
  44. function close () {
  45.  mysql_close ($this -> handle);
  46. }
  47. }
  48. ?>

Tak więc testuje:
TEST.php
  1. <?php
  2. $db =& mysql::connect ("localhost", "root", "***", "***");
  3. $db -> query ("SELECT * FROM users", "userzy");
  4. while ($row =$db -> fetch_array ()) {
  5. echo $row['username']." n";
  6. }
  7. $db -> free_result ();
  8. ?>


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 questionmark.gifquestionmark.gif
ActivePlayer
print_r($db);
Levabul
Nie wiem po co to ale dobra ...
Kod
mysql Object ( [handle:private] => Resource id #6 [sql_query:private] => Resource id #8 [cache:private] => Cache Object ( [buffer:private] => Array ( [0] => Array ( [user_id] => 1 [username] => Levabul [password] => 38456923456930245790124580649312 ) ) [mode:private] => 0 [file:private] => cache/sql_cache_userzy [handle:private] => userzy [line:private] => 0 ) [rows:private] => )


ps. zależy mi na jak najszybszej odpowiedzi :|
dasko
Ten kawałek kodu jest po prostu boski - metoda cache_get biggrin.gif
  1. <?php
  2.  
  3. return $this -> buffer [$this -> line];
  4. $this -> line++;
  5.  
  6. ?>
Levabul
Sry ale mam dzisiaj ciężkie myślenie - co jest z nim nie tak :/
Spirit86
  1. <?php
  2. $this -> line++; 
  3. return $this -> buffer [$this -> line];
  4. ?>
Levabul
Po 1 nic to nie daje, a po drugie nie miało prawa dać ponieważ tablice zaczynają się od klucza "0" :/
dasko
  1. <?php
  2. return $this -> buffer [$this -> line++];
  3. ?>

Powinno działać.
xardas
  1. <?php
  2. public function sql_fetch_array()
  3. {
  4. if(isset($this -> rows[$this -> ptr]))
  5. {
  6. $this -> ptr++;
  7. return (array)$this -> rows[$this -> ptr -1];
  8. }
  9. else
  10. {
  11. $this -> ptr = 0;
  12.  
  13.  
  14. return (bool)false;
  15. }
  16.  
  17.  
  18. }
  19. ?>


Pomyśl trochę na podstawie tego.

DLA LUDZI OCIŻAŁYCH UMYSŁOWO: Żeby nie bylo nieskończonej pętli, ta funkcja musi kiedyś zwrócić FALSE.
Levabul
Dzięki ci o Dasko biggrin.gif Działa.

A co do ciebie Xardas, przecież funkcja zwraca false więc co świrujesz questionmark.gifquestionmark.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.