Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Cachowanie zapytań SQL...
Forum PHP.pl > Forum > PHP
Pyton_000
Witam... Przeczytalem art: http://webcity.pl/webcity/artykuly.php/t/51
Opisuje klase do Cachowania zapytań SQL.

Postanowiłem ową zintegrować z phpBB w celu zmniejszenia dostępności do Bazy...

No i prawie mi się udało... Dlaczego prawie...
Wykonane zapytania umieszczane są w pliku, ale juz z odczytem jest coś nie teges...

Tak więc...
Mam plik members.php który wyświetla liste userów... W prawdzie obecnie jestem tylko ja na liście ale to szczegół..
Tak się przedstawia kawałek owego kodu który chce zcachować:
  1. <?php
  2.  
  3. $db -> sql_cache('memberlist');
  4. $sql = "SELECT username, user_id, user_viewemail, user_posts, user_regdate, user_fr
    om, user_website, user_email, user_icq, user_aim, user_yim, user_msnm, user_avata
    r, user_avatar_type, user_allowavatar 
  5. FROM " . USERS_TABLE . "
  6. WHERE user_id <> " . ANONYMOUS . "
  7. ORDER BY $order_by";
  8. if( !($result = $db->sql_query($sql)) )
  9. {
  10. message_die(GENERAL_ERROR, 'Could not query users', '', __LINE__, __FILE__, $sql);
  11. }
  12.  
  13. if ( $row = $db->sql_fetchrow($result) )
  14. {
  15. $i = 0;
  16. do
  17. {
  18. $yim = ( $row['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $row['user_yim'] . '&amp;.src=pg">' . $lang['YIM'] . '</a>' : '';
  19.  
  20. $temp_url = append_sid("search.$phpEx?search_author=" . urlencode($username) . "&amp;showresults=posts");
  21. $search_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_search'] . '" alt="' . $lang['Search_user_posts'] . '" title="' . $lang['Search_user_posts'] . '" border="0" /></a>';
  22. $search = '<a href="' . $temp_url . '">' . $lang['Search_user_posts'] . '</a>';
  23.  
  24. $row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
  25. $row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];
  26.  
  27. $template->assign_block_vars('memberrow', array(
  28. 'ROW_NUMBER' => $i + ( $start + 1 ),
  29. 'ROW_COLOR' => '#' . $row_color,
  30.  
  31. 'U_VIEWPROFILE' => append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id"))
  32. );
  33.  
  34. $i++;
  35. }
  36. while ( $row = $db->sql_fetchrow($result) );
  37. $db->sql_freeresult($result);
  38. }
  39. $db -> sql_cache();
  40.  
  41. ?>


A tutaj podaje jak mniej więcej zmodyfikowałem klase Bazy dabych od phpBB2
  1. <?php
  2. if(!defined("SQL_LAYER"))
  3. {
  4.  
  5. define("SQL_LAYER","mysql4");
  6. define('CACHE_DIR', './cache/'); # To dodałem
  7.  
  8. class sql_db
  9. {
  10.  
  11. var $db_connect_id;
  12. var $query_result;
  13. var $row = array();
  14. var $rowset = array();
  15. var $num_queries = 0;
  16. var $in_transaction = 0;
  17. var $rows;
  18. # To dodałem - START
  19. /**
  20.  * Obrazuje stan Cache
  21.  * 0 - Cachowanie wyłączone
  22.  * 1 - Chace zapytania istnieje
  23.  * 2 - W trakcie generowania
  24.  *
  25.  * @var unknown_type
  26.  */
  27. var $cache_state =0;
  28.  
  29. var $cache_file;
  30. var $cache_buffer;
  31. var $cache_ptr;
  32.  
  33.  
  34.  
  35.  
  36. function sql_cache($handle = 0){
  37.  if(is_string($handle))
  38.  {
  39. if(file_exists(CACHE_DIR.'sql_' . $handle . '.cache'))
  40. {
  41.  $this -> cache_state  = 1;
  42.  $this -> cache_ptr = 0;
  43.  $this -> cache_buffer = unserialize(file_get_contents(CACHE_DIR.'sql_' . $handle . '.cache'));
  44. }
  45. else
  46. {
  47.  $this -> cache_state = 2;
  48.  $this -> cache_buffer = array();
  49.  $this -> cache_file = CACHE_DIR.'sql_' . $handle . '.cache';
  50. }
  51.  }
  52.  else
  53.  {
  54. if($this -> cache_state == 2)
  55. {
  56.  $this -> file_put_contents($this -> cache_file, serialize($this -> cache_buffer));
  57. }
  58. $this -> cache_state = 0;
  59.  }
  60. }
  61. function file_put_contents($plik, $dane){
  62. $f = fopen($plik, 'w');
  63. fwrite($f, $dane);
  64. fclose($f);
  65. }
  66. # To dodałem - KONIEC
  67.  
  68. // Tutaj dalej są funkcje...
  69. ?>

SQL Query zmodyfikowane
  1. <?php
  2. function sql_query($query = "", $transaction = FALSE)
  3. {
  4. //
  5. // Remove any pre-existing queries
  6. //
  7. unset($this->query_result);
  8.  
  9. if( $query != "" )
  10. {
  11. $this->num_queries++;
  12. if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
  13. {
  14. $result = mysql_query("BEGIN", $this->db_connect_id);
  15. if(!$result)
  16. {
  17. return false;
  18. }
  19. $this->in_transaction = TRUE;
  20. }
  21. if($this -> cache_state != 1)
  22. $this->query_result = mysql_query($query, $this->db_connect_id);
  23. }
  24. else
  25. {
  26. if( $transaction == END_TRANSACTION && $this->in_transaction )
  27. {
  28. $result = mysql_query("COMMIT", $this->db_connect_id);
  29. }
  30. }
  31.  
  32. if( $this->query_result )
  33. {
  34. unset($this->row[$this->query_result]);
  35. unset($this->rowset[$this->query_result]);
  36.  
  37. if( $transaction == END_TRANSACTION && $this->in_transaction )
  38. {
  39. $this->in_transaction = FALSE;
  40.  
  41. if ( !mysql_query("COMMIT", $this->db_connect_id) )
  42. {
  43. mysql_query("ROLLBACK", $this->db_connect_id);
  44. return false;
  45. }
  46. }
  47.  
  48. return $this->query_result;
  49. }
  50. else
  51. {
  52. if( $this->in_transaction )
  53. {
  54. mysql_query("ROLLBACK", $this->db_connect_id);
  55. $this->in_transaction = FALSE;
  56. }
  57. return false;
  58. }
  59. }
  60. ?>

I tu SQL Fetchrow który użyty jest w pliku memberlist.php
  1. <?php
  2. function sql_fetchrow($query_id = 0)
  3. {
  4. if( !$query_id )
  5. {
  6. $query_id = $this->query_result;
  7. }
  8.  
  9. if( $query_id )
  10. {
  11.  if($this -> cache_state == 1)
  12.  {
  13. // czy koniec bufora?
  14. if(!isset($this -> cache_buffer[$this -> cache_ptr]))
  15. {
  16.  return 0;
  17. }
  18. // odczytaj z bufora
  19. $this -> rows = $this -> cache_buffer[$this -> cache_ptr]; 
  20. $this -> cache_ptr++;
  21. return 1;
  22.  }
  23.  
  24.  if($this -> cache_state == 2)
  25.  $this -> cache_buffer[] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  26.  
  27. $this->row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  28. return $this->row[$query_id];
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. }
  35.  
  36. ?>


A oto zserializowane dane z wyniku cachowania:
Kod
a:1:{i:0;a:15:{s:8:"username";s:5:"Pyton";s:7:"user_id";s:1:"2";s:14:"user_viewemail";s:1:"1";s:10:"user_posts";s:1:"1";s:12:"user_regdate";s:10:"1131881225";s:9:"user_from";s:0:"";s:12:"user_website";s:0:"";s:10:"user_email";s:15:"pyton_www@o2.pl";s:8:"user_icq";s:0:"";s:8:"user_aim";s:0:"";s:8:"user_yim";s:0:"";s:9:"user_msnm";s:0:"";s:11:"user_avatar";s:0:"";s:16:"user_avatar_type";s:1:"0";s:16:"user_allowavatar";s:1:"1";}}
ActivePlayer
  1. <?php
  2.  
  3. //...
  4. if($this -> cache_state == 1)
  5.  {
  6. // czy koniec bufora?
  7. if(!isset($this -> cache_buffer[$this -> cache_ptr]))
  8. {
  9.  return 0;
  10. }
  11. // odczytaj z bufora
  12. $this -> rows = $this -> cache_buffer[$this -> cache_ptr]; 
  13. $this -> cache_ptr++;
  14. return 1;
  15.  }
  16.  
  17.  if($this -> cache_state == 2)
  18.  $this -> cache_buffer[] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  19.  
  20. $this->row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  21. return $this->row[$query_id];
  22. //...
  23.  
  24. ?>

gdzie tu sens ?
Pyton_000
To mógłbyś pomóc questionmark.gif Bo kurcze troche się na php znam ale jakoś z tym sobie nie umiem poradzić :]
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.