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ć:
<?php $db -> sql_cache('memberlist'); $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 FROM " . USERS_TABLE . " WHERE user_id <> " . ANONYMOUS . " ORDER BY $order_by"; if( !($result = $db->sql_query($sql)) ) { message_die(GENERAL_ERROR, 'Could not query users', '', __LINE__, __FILE__, $sql); } if ( $row = $db->sql_fetchrow($result) ) { $i = 0; do { $yim = ( $row['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $row['user_yim'] . '&.src=pg">' . $lang['YIM'] . '</a>' : ''; $temp_url = append_sid("search.$phpEx?search_author=" . urlencode($username) . "&showresults=posts"); $search_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_search'] . '" alt="' . $lang['Search_user_posts'] . '" title="' . $lang['Search_user_posts'] . '" border="0" /></a>'; $search = '<a href="' . $temp_url . '">' . $lang['Search_user_posts'] . '</a>'; $row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2']; $row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2']; 'ROW_NUMBER' => $i + ( $start + 1 ), 'ROW_COLOR' => '#' . $row_color, 'U_VIEWPROFILE' => append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$user_id")) ); $i++; } while ( $row = $db->sql_fetchrow($result) ); $db->sql_freeresult($result); } $db -> sql_cache(); ?>
A tutaj podaje jak mniej więcej zmodyfikowałem klase Bazy dabych od phpBB2
<?php { class sql_db { var $db_connect_id; var $query_result; var $num_queries = 0; var $in_transaction = 0; var $rows; # To dodałem - START /** * Obrazuje stan Cache * 0 - Cachowanie wyłączone * 1 - Chace zapytania istnieje * 2 - W trakcie generowania * * @var unknown_type */ var $cache_state =0; var $cache_file; var $cache_buffer; var $cache_ptr; function sql_cache($handle = 0){ { { $this -> cache_state = 1; $this -> cache_ptr = 0; } else { $this -> cache_state = 2; $this -> cache_file = CACHE_DIR.'sql_' . $handle . '.cache'; } } else { if($this -> cache_state == 2) { } $this -> cache_state = 0; } } function file_put_contents($plik, $dane){ } # To dodałem - KONIEC // Tutaj dalej są funkcje... ?>
SQL Query zmodyfikowane
<?php function sql_query($query = "", $transaction = FALSE) { // // Remove any pre-existing queries // if( $query != "" ) { $this->num_queries++; if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction ) { if(!$result) { return false; } $this->in_transaction = TRUE; } if($this -> cache_state != 1) } else { if( $transaction == END_TRANSACTION && $this->in_transaction ) { } } if( $this->query_result ) { if( $transaction == END_TRANSACTION && $this->in_transaction ) { $this->in_transaction = FALSE; { return false; } } return $this->query_result; } else { if( $this->in_transaction ) { $this->in_transaction = FALSE; } return false; } } ?>
I tu SQL Fetchrow który użyty jest w pliku memberlist.php
<?php function sql_fetchrow($query_id = 0) { if( !$query_id ) { $query_id = $this->query_result; } if( $query_id ) { if($this -> cache_state == 1) { // czy koniec bufora? { return 0; } // odczytaj z bufora $this -> rows = $this -> cache_buffer[$this -> cache_ptr]; $this -> cache_ptr++; return 1; } if($this -> cache_state == 2) return $this->row[$query_id]; } else { return false; } } ?>
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";}}