I wrote a blog post before, using PHP and Memcache to implement a website. Check it out at the following link: http://www.jb51.net/article/51825.htm
Today I will use this function in wordpress and save the number of visits to the database.
MySQL statement
First, add the default data of the number of visits in the parameter table
// 获取所有浏览次数 function get_all_visit_number() { $mc = new Memcache (); // 使用wordpress自带wpdb类 global $wpdb; // 参数表 $table = "wp_options"; // 连接memcache $mc->connect ( "127.0.0.1", 11211 ); // 获取浏览次数 $visit_number = $mc->get ( 'visit_number' ); // Memcache 中是否存有访问次数 if (!$visit_number) { // 不存在时,查询数据库 $querystr = "SELECT `option_value` FROM " .$table. " WHERE `option_name`='visit_number'"; $results = $wpdb->get_results($querystr); // 把数据库中存储的值赋予memcache变量 $visit_number = intval($results[0]->option_value); } // 设置浏览次数 $mc->set ( 'visit_number', ++$visit_number); // 获取浏览次数 $visit_number = $mc->get ( 'visit_number' ); // 每达100次访问量,更新到数据库 if ($visit_number % 100 == 0) { // 使用wordpress自带wpdb类 $data_array = array( 'option_value' => $visit_number ); $where_clause = array( 'option_name' => 'visit_number' ); $wpdb->update($table,$data_array,$where_clause); } // 关闭memcache连接 $mc->close (); return $visit_number; }