PHP+Memcache Implementation of wordpress Total Access Statistics (non plug in)

  • 2021-07-07 06:35:00
  • OfStack

I have written a blog post before, using PHP and Memcache to realize the website. See the following link: https://www.ofstack.com/article/51825. htm
Today, this function is used in wordpress, and the number of accesses is saved to the database.

MySQL statement

First, in the parameter table, add the default data of the number of accesses


//  Get all browsing times 
function get_all_visit_number()
{
 $mc = new Memcache ();
 
 //  Use wordpress Bring your own wpdb Class 
 global $wpdb;
 
 //  Parameter table 
 $table = "wp_options";
 
 //  Connect memcache
 $mc->connect ( "127.0.0.1", 11211 );
 
 //  Get the number of browses 
 $visit_number = $mc->get ( 'visit_number' );
 
 // Memcache  Is there a number of accesses in 
 if (!$visit_number) {

 //  Query the database when it does not exist  
 $querystr = "SELECT `option_value` FROM " .$table. " WHERE `option_name`='visit_number'";
 $results = $wpdb->get_results($querystr);
 
 //  Assign the values stored in the database to memcache Variable 
 $visit_number = intval($results[0]->option_value);
 }
 
 //  Set the number of browses 
 $mc->set ( 'visit_number', ++$visit_number);
 
 //  Get the number of browses 
 $visit_number = $mc->get ( 'visit_number' );
 

 //  Every reach 100 Number of visits, update to database 
 if ($visit_number % 100 == 0) {

 //  Use wordpress Bring your own wpdb Class 
 $data_array = array(
  'option_value' => $visit_number
 );
 
 $where_clause = array(
  'option_name' => 'visit_number'
 );
 
 $wpdb->update($table,$data_array,$where_clause);
 }
 
 //  Shut down memcache Connect 
 $mc->close ();
 
 return $visit_number;
}


Related articles: